soccer_programmer
soccer_programmer

Reputation: 3

Trying to draw a circle using paintComponent

I am doing an assignment where i create a GUI that draws a circle or face on it and gives the user options to change the facial features when they click on the button, I have gotten the basic layouts done but now I want to draw a circle in the main panel of my GUI, I created another file that extends JPanel and I created the paintComponent method and called it on the main app but it wont get a circle but instead would get a little square at the top of the main panel, I have played around with the size but still would give me the same thing no matter what I change, I don't know what i am missing if i can get some help I would appreciate it and some advice.. thank you in advance

this is my JPanel main app

public class FaceApp extends JFrame {

    /**
     * 
     */

    JPanel panel,mainPanel;
    Graphics graph;

    public static void main(String[] args) {
        FaceApp frame = new FaceApp();
        frame.setVisible(true);
        frame.setBackground(Color.BLUE);
        frame.setSize(1000,1000);
        frame.setResizable(false);
    }

    private FaceApp() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setTitle("Face");

        panel = new JPanel(new GridLayout(16,1,1,1));
        panel.setBorder(new EmptyBorder(30,30,100,100));

        mainPanel = new JPanel();
        JCheckBox eyes = new JCheckBox("Eyes");
        JCheckBox noes = new JCheckBox("Nose");
        JCheckBox mouth = new JCheckBox("Mouth");
        JLabel label = new JLabel();

        JButton update = new JButton("update");

        label.setText("You choose..");

        panel.add(label);
        panel.add(eyes);
        panel.add(noes);
        panel.add(mouth);
        panel.add(update);

        Face face = new Face();

        mainPanel.setBackground(Color.BLUE);
        mainPanel.add(face);

        add(mainPanel,BorderLayout.CENTER);
        add(panel, BorderLayout.WEST);

    }
}

And here is the Face class that does the painting:

public class Face extends JPanel{

    /**
     * 
     */

    public Face(){
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.fillOval(20, 30, 150, 150);
        g.setColor(Color.red);      
    }
}

Upvotes: 0

Views: 1631

Answers (1)

camickr
camickr

Reputation: 324118

mainPanel.add(face);

You are adding your face component to a panel which uses a FlowLayout. The FlowLayout respects the preferred size of any component added to it. In your case the preferred size is (0, 0) so the layout manager can't do its job properly.

You need to override the getPreferredSize() method of your class to return the preferred size of your component, which in your case would probably be (190, 210) so the oval is centered in the panel.

You can read the section from the Swing tutorial on Custom Painting for a working example that shows how to implement this method. Keep a link to the tutorial handy as it contains examples of many Swing basics.

Upvotes: 2

Related Questions