不在 Java 中绘制图形

Does not paint Graphics in Java

大家好,我正在尝试在 Java 中构建自定义形状,但出于某种原因,它没有将它们绘制到我的 Canvas 中。

我创建了一个 Class:

public class MyCircleCanvas extends JComponent{
    private void doDrawing(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;

        g2d.setColor(new Color(150, 150, 150));

        //g2d.fillRect(30, 20, 50, 50);
        //g2d.fillRect(120, 20, 90, 60);
        //g2d.fillRoundRect(250, 20, 70, 60, 25, 25);

        //g2d.fill(new Ellipse2D.Double(10, 100, 80, 100));
        //g2d.fillArc(120, 130, 110, 100, 5, 150);
        g2d.fillOval(270, 130, 50, 50);   
   } 

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        doDrawing(g);
    }    

}

然后在我的 JFrame 中调用这个 Class:

private void initComponents() {
        createMenu();
        createToolBar();
        createCenterPanel();
        //createCanvas();
    }

private void createCanvas() {
        //c = new Canvas();
        //c.setBackground(Color.white);
        // this.add(c);
        add(new MyCircleCanvas());
    }

这很好用。但是当我尝试从 JButton 的 ActionListener 调用方法 createCanvas() 时,它不会创建我想要的形状。有什么建议吗???

您应该覆盖 paint 方法而不是 paintComponent。为了将 MyCircleCanvasses 添加到图形用户界面,我创建了一个带有 BoxLayoutJPanel。在此面板中,新添加的 MyCircleCanvasses 是垂直添加的。

确保覆盖 getPreferredSize 方法,否则将 `MyCircleCanvas 添加到用户界面时将没有可见的大小。

此外,pack 方法确保 JFrame 的大小合适以包含新的 MyCircleCanvass。

    public class MyCircleCanvas extends JComponent {
    private void doDrawing(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(new Color(150, 150, 150));
        g2d.fillOval(25, 25, 50, 50);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        doDrawing(g);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(100, 100);
    }
}

而这一帧:

public class CircleCanvasFrame extends JFrame implements ActionListener {
    private JPanel circlePanel = new JPanel();

    public CircleCanvasFrame() {
        setVisible(true);
        setSize(400, 400);
        circlePanel.setLayout(new BoxLayout(circlePanel, BoxLayout.Y_AXIS));
        JButton button = new JButton("add a circle canvas");
        button.addActionListener(this);
        add(button, BorderLayout.NORTH);
        add(circlePanel, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        new CircleCanvasFrame();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        circlePanel.add(new MyCircleCanvas());
        pack();
    }
}

您的问题是您将椭圆定位得太远以至于看不见。使位置更接近原点。

private void doDrawing(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.YELLOW);
        g2d.fillOval(0, 0, 50, 50);   
   } 

以下是在此之后如何完成的粗略想法:

EventQueue.invokeLater(new Runnable() {
            public void run(){
                final JFrame frame = new JFrame();
                JButton button = new JButton("test");
                frame.setLayout(new FlowLayout());

                button.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {

                        MyCircleCanvas circle = new MyCircleCanvas();
                        frame.add(circle);
                        frame.pack();

                    }
                });

                frame.add(button);
                frame.setSize(100,100);
                frame.setVisible(true);
            }
        });