1

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???

2
  • The createCanvas() method should instead be called drawShape() and use a canvas that has already been added to the GUI. For better help sooner, post an MCVE (Minimal Complete Verifiable Example) or SSCCE (Short, Self Contained, Correct Example). Commented Jan 11, 2015 at 15:13
  • how do i add the new MyCircleCanvas() into the canvas and get the rectangle then? Commented Jan 11, 2015 at 15:16

2 Answers 2

1

You should overwrite the paint method instead of the paintComponent. To add the MyCircleCanvasses to a graphical user interface, I created a JPanel with a BoxLayout. In this panel, the newly added MyCircleCanvasses 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 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); } } 

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(); } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Yes that works fine. But the thing is how to add the new MyCircleCanvas into the JFrame from an ActionListener of a JButton???
@KostasDrakonakis, I edited the example above to include addition of more panels using a button.
0

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

Comments