0

I am implementing a demo paint application in Java. I am able to draw a single shape right now. When I try to draw again, the earlier shape is vanished and a new shape comes up. I am embedding JPanel on an internal frame with BorderLayut.CENTER.

Please help me how to draw multiple shapes in this internal frame.

    public class InternalFrame extends JInternalFrame{

    public InternalFrame(String string, boolean b, boolean c, boolean d,
            boolean e) {
        super(string,b,c,d);

        MyShape myShape2 = new MyRectangle();
        add(myShape2, BorderLayout.NORTH);

        MyShape myShape1 = new MyCircle();
        add(myShape1, BorderLayout.SOUTH);      
}   
}

public class MyRectangle extends MyShape {


    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    int temp = 0;

System.out.println("rect");
    // draw circle
    g.setColor(Color.RED);
    g.fillRect(startX, startY, endX - startX, endY - startY);
    g.setColor(Color.GREEN);
    g.drawRect(startX, startY, endX - startX, endY - startY);

}


}


public abstract class MyShape extends JPanel {

    protected int startX;
    protected int startY;
    protected int endX;
    protected int endY;

    MyShape(){
        addMouseListener(new MouseAdapter() {

            public void mousePressed(MouseEvent event) {

//              To initialisise the starting and ending point 
                setStart(0,0);
                setEnd(0,0);
//              To set starting point
                setStart(event.getX(), event.getY());
            }

            // handle mouse release event
            public void mouseReleased(MouseEvent event) {
                setEnd(event.getX(),event.getY());

            }

        });
    }

    public void setStart(int x, int y) {

        startX = x;
        startY = y;

        repaint();
    }

    public void setEnd(int x, int y){

        endX = x;
        endY = y;
        repaint();
    }




}
Ravindra
  • 95
  • 2
  • 10
  • 1
    Please show your code, it will greatly enhance your chance to get an answer. Without code, we have to speculate. – fvu Sep 20 '13 at 21:12

2 Answers2

1

Painting in Swing is destructive, that is, each time paintComponent is called, you are expected to repaint the entire componet state from scratch.

The best solution is to generate a List of Shapes or paint commands that you can call/draw/fill as per your requirements, each time paintComponent is called

See Incremental graphics in Swing for some more details and ideas

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you. That was helpful. I am now creating new object for every new shape. But I am stuck up with the layout issue. With the following code I can paint the Rectangle in the north and the circle in the south. Any ideas to solve this problem? – Ravindra Sep 20 '13 at 22:24
  • That depends, what do you want to achieve? – MadProgrammer Sep 21 '13 at 00:03
1

Custom Painting Approaches shows two common ways to do this. The approach you use depends on your exact requirement.

Edit:

Maybe Playing With Shapes has more information/ideas to help you out.

camickr
  • 321,443
  • 19
  • 166
  • 288