简体   繁体   中英

Does not paint Graphics in Java

Hey guys i am trying to build custom shapes in Java and for some reason it doesn't paint them into my Canvas.

I have created a 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);
    }    

}

Then in My JFrame i Call this Class:

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

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

This works fine. BUT when i try to call the method createCanvas() from an ActionListener of a JButton it does not create the shape i want. Any suggestions???

You should overwrite the paint method instead of the paintComponent . To add the MyCircleCanvas ses to a graphical user interface, I created a JPanel with a BoxLayout . In this panel, the newly added MyCircleCanvas ses are added vertically.

Make sure to overwrite the getPreferredSize method because else the `MyCircleCanvas will not have a visible size when adding it to the user interface.

Also the pack methods makes sure that the JFrame is properly sized to include the new MyCircleCanvas s.

    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);
    }
}

And this frame:

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();
    }
}

Your problem is that you are positioning the Oval too far to be seen. Make the location closer to origin.

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

Here is a rough idea of how it could be done after this:

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);
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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