Hi there,

I want to implement undo logic which deletes the last drawn shape. To test this logic i had cleared shapes list and redrawn all shapes but still last shape does not delete.

    public void drawAllShapes(Graphics2D g)
    {

        //draw all triangles
        for (DrawTriangle t : newTriangles)
        {
            t.drawTriangle(g);
        }

        for(DrawRect r: rectangles)
        {
            r.drawRectangle(g);
        }

        //draw all circles
        for (DrawCircle c : circles)
        {
            c.drawEllipse(g);
        }

        System.out.println("Rect size: " +rectangles.size());
        System.out.println("Circle size: " +circles.size());

    }//end drawAllShpapes

//undo logic
@Override
public void paintComponent(Graphics graphics){

 Graphics2D graphics2d = (Graphics2D)graphics;

if(UndoCommand.undoPressed) {
    rectangles.clear();

    drawAllShapes(g);
    UndoCommand.undoPressed = false;    
}
}

Please tell me what I am doing wrong here. Thanks !

Normally for an undo you would get rid of the last thing you added - ie remove the last Triangle, Rectangle, or Circle from its corresponding list, then redraw.
Your code deletes all the Rectangles in the list - regardless of how many there are, and regardless of whether the last thing you added was a Triangle, Rectangle, or Circle.

Hint:
You can make this a whole lot simpler by recognising that triangle, rectangle, and circle are all kinds of shapes. You could create a DrawShape class, with an abstract draw(Graphics2D g) method. Let your three Draw... classes extend it, and each implement its own version of draw(Graphics2D g).
Now you only need one List<DrawShape> that you can add all three shapes to. You can draw them all with a single loop - Java will call the correct draw method for each type of shape.
And (here's the punch line) the last thing you created will always be the last DrawShape in the list, so to undo you just delete that one.

I really liked the idea of using polymorphism. But please can you tell me why drawAllShapes() function is not working in if condition.

@Override
public void paintComponent(Graphics graphics){
 Graphics2D graphics2d = (Graphics2D)graphics;
if(UndoCommand.undoPressed) {
    rectangles.clear();
    drawAllShapes(g);
    UndoCommand.undoPressed = false;    
}
}

g is undefined at line 6 - maybe you intended graphics2d (see line 3) ?

sorry its my mistake, please consider it graphics2d. but still having problem while calling drawAllShapes().

It delete shape when draw new shape on canvas. I want it delete shape on pressing undo button.

Any solution ?

I would not put the code lines 4-8 in paintComponent. They don't belong there, and don't forget that paintComponent may be called by Swing at any time.
I would just have paintComponent do the painting, nothing else.

In the listener for the undo button I would simply delete the latest entry from the shapes list and call repaint()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.