简体   繁体   English

尝试使用paintComponent画一个圆

[英]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 我正在做一个作业,我在其中创建一个GUI来在其上绘制圆或面,并为用户提供了单击按钮时更改面部特征的选项,我已经完成了基本布局,但现在我想绘制一个圆在GUI的主面板中,我创建了另一个扩展JPanel文件,并创建了paintComponent方法,并在主应用程序上对其进行了调用,但是它不会出现一个圆形,而是会在主面板的顶部得到一个小正方形,已经尝试过大小​​,但是无论我进行什么更改,仍然会给我相同的东西,如果我能得到一些帮助,我将不胜感激,我将不胜感激,并提供一些建议。

this is my JPanel main app 这是我的JPanel主应用

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: 这是进行绘画的Face类:

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);      
    }
}
mainPanel.add(face);

You are adding your face component to a panel which uses a FlowLayout. 您正在将面部组件添加到使用FlowLayout的面板中。 The FlowLayout respects the preferred size of any component added to it. FlowLayout遵守添加到其中的任何组件的首选大小。 In your case the preferred size is (0, 0) so the layout manager can't do its job properly. 在您的情况下,首选大小为(0,0),因此布局管理器无法正确执行其工作。

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. 您需要重写类的getPreferredSize()方法以返回组件的首选大小,在您的情况下,该大小可能是(190,210),因此椭圆在面板中居中。

You can read the section from the Swing tutorial on Custom Painting for a working example that shows how to implement this method. 您可以阅读Swing教程“ 自定义绘画”中的这一部分,以获取一个示例,该示例演示了如何实现此方法。 Keep a link to the tutorial handy as it contains examples of many Swing basics. 请保留本教程的链接,因为它包含许多Swing基础知识的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM