Search code examples
javaswingdrawingawtfactory-pattern

Drawing multiple Shapes on the same Canvas when pressed a button


I need to draw a square,line,circle when pressed the corespondent button. Also I need to do this using FactoryMethod design pattern. I simply don't get how to draw on the same canvas, and because I have a class for every shape, how do i get the corresponding paint(Graphics g) method? This is what I have so far:

public interface Shape

{

          public void draw();

}

Square class

public class Square extends Canvas implements Shape
{
    Graphics g;

    Canvas c;

    public Canvas getCanvas()
    {
        return c;
    }
    public void setCanvas(Canvas c)
    {
        this.c=c;
    }
    @Override
    public void draw() 
    {
        g.drawRect(20, 30,100,100);

    }
    public void paint(Graphics g)
    {
        g.drawRect(20, 30,100,100);
        g.setColor(Color.BLUE);
    }


}

Factory

public class ClassFactory extends Canvas{

    JButton patrat;
    Figura d;
    String nameButon;
    Graphics g;
    Canvas c;
    public Canvas getCanvas()
    {
        return c;
    }
    public void setCanvas(Canvas c)
    {
        this.c=c;
    }
    public ClassFactory()
    {
        super();
        this.setBounds(0,0,500,450);
        this.setBackground(Color.CYAN);
        JButton square=new JButton("square");
        patrat.setBounds(510, 10, 80,25);

        JPanel panel=new JPanel();
        panel.setLayout(null);
        panel.setBounds(0,0,600,500);
        panel.setBackground(Color.GRAY);
        panel.add(this);
        this.addComponentListener(p);
        panel.add(square);

        JFrame f=new JFrame("Draw");
        f.setLayout(null);
        f.setBounds(50,50,700,600);
        f.getContentPane().setBackground(Color.DARK_GRAY);
        f.setResizable(false);
        f.add(panel);

        f.show();
           }

    public Shape getFigure()
    {
        Shape d=null;
        if(nameButton.equals("square"))
        {
            d=new Square();
        }
        return d;

    }   
}

Solution

  • Suggestions:

    • The factory should not create a GUI, should not extend Canvas, or really extend anything, it should not create a JFrame or do anything of the sort. It should concern itself only with creating objects of Shape child classes. The GUI creation code should be elsewhere.
    • Likely the factory's getFigure(...) method will be the one to produce this. It should likely accept a parameter, perhaps a String or an enum, that tells it what sub-class of Shape to produce.
    • Shape's draw method should likely accept a Graphics parameter so that its children can use it to draw with.
    • You shouldn't mix AWT components (i.e., Canvas) and Swing components together unnecessarily. Instead, just draw in a JPanel's paintComponent(Graphics g) method, not in a Canvas's paint(Graphics g) method.
    • In that JPanel have a Shape variable that is not initialized, perhaps called shape.
    • Inside of paintComponent(...) check if shape is null. If not, draw it by calling shape.draw(g).
    • In your JButton ActionListeners, have the Factory create a Shape child class object and assign it to the shape variable
    • Then call repaint() on the JPanel that does the drawing.