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.

Recommended Answers

All 4 Replies

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?

Well, you mentioned "the document", which I have no idea about. It may be that you can override the paintComponent() for that, but you haven't given much detail to go on.

Here's an example of what I'm referring to, if you wish to take a look: http://www.daniweb.com/forums/post951298.html#post951298

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.

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.