It is a duplicate thread posted in other forum but don't get satisfied answer from their. so Hoping to get a better answer from here.Ambiquity while drawing multiple shapes on Jpanel. 0 down vote favorite
Th code below has few problems :
1) The polygon joins last and first point itself, should not do itself but user should draw it.
2) The polygons lines disappeared after clicking on other shapes.
Now we all know about the cross’post her …
You ought to tell the chap who replied on SO that you have posted here, too.
Why are you using an image? You would not usually use an image to draw on. You draw directly onto the panel. That should allow the shapes to appear over one another in the order they are painted. The suggestion on the other forum of creating Shape objects and keeping them in a List is sound. Far better than trying to do everything in the paintComponent method.
As for closing or not closing polygons, there are ways to do that, but I can’t remember definitely. I think you may need to learn how to use a GeneralPath object. No, not that; it says to use this instead.