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???
createCanvas()method should instead be calleddrawShape()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).