I tried making a Java desktop application to learn Java. So far, a mouse click event draws rectangles in the document and stores the rectangle in an array in ProjectApp.java. This isn't the problem but my rectangles disappear when I minimize and maximize the window. I normally use VS C++ by looping through an array in the doc class and return the rectangle each time and then drawing when OnDraw is called.

So, what's the OnDraw equivalent in a Java desktop application? Is this even possible? I have tried googling this. Making my own paint(Graphics g) method with the super.paint(g) doesn't work.

You need to override paintComponent(Graphics) for whichever component you are drawing them on. Update the state of your rectangle data with the mouse events and let paintComponent() handle the actually drawing.

You need to override paintComponent(Graphics) for whichever component you are drawing them on. Update the state of your rectangle data with the mouse events and let paintComponent() handle the actually drawing.

I tried writing:

public class ProjectView extends FrameView { ...
public void paintComponent() {
super.paintComponent() ....

but this doesn't work either.

I think by your comment "whichever component you are drawing them on", I should be drawing in another class or I should have added a component to draw on?

By document, I only meant where the rectangles are drawn.

If you look in my second post, the class with my GUI code extends FrameView and not JFame. It was automatically created this way. Yours is better. Thanks.

This article has been dead for over six months. Start a new discussion instead.