aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes previously drawn shapes disappear when new one is created Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "previously drawn shapes disappear when new one is created" Watch "previously drawn shapes disappear when new one is created" New topic
Author

previously drawn shapes disappear when new one is created

Veronique Kus
Ranch Hand

Joined: Jun 13, 2010
Posts: 41
I'm trying to make an application that allows the user to draw shapes. It works apart from the fact that when I draw a new shape, the previous one I drew disappears. I want to allow the user to draw as many shapes as they want...
Below I am posting code of two classes, MapTab class which contains the DrawingCanvas class in which the shapes are drawn, and the Shape class which contains instructions on how to draw 2 kinds of shapes. the drawShape() method in MapTab is called from my main GUI class.





I would appreciate some help with this... I'm guessing the source of the problem is the repaint() method but I'm not sure what exactly is wrong abt it.

Additionally, I have a small problem with the DrawingCanvas' background - it doesn't show. When i set it to, say, yellow, you can still see the MapTab's white background instead. What could cause this?

Thank you in advance.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Your code is doing exactly what you're telling it to do: draw one shape. If you want to draw more than one, you have to tell it to do this specifically. Perhaps you wish to create a List<Shape> and add shapes to this list. Then you can iterate through the list in your paintComponent method drawing each shape that is held by the list.
Veronique Kus
Ranch Hand

Joined: Jun 13, 2010
Posts: 41
Thanks for the reply.
Just to clarify, do you mean that every time a new shape is drawn by the user, the application will draw all the shapes contained in the List (incl. the new one) from scratch? If so, isn't that going to be a bit time consuming?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Veronique Kus wrote:Thanks for the reply.
Just to clarify, do you mean that every time a new shape is drawn by the user, the application will draw all the shapes contained in the List (incl. the new one) from scratch? If so, isn't that going to be a bit time consuming?


Yes, that's exactly what I mean. For simple drawings such as yours, no, this shouldn't take up too much time, but if later you have more complex graphics that you fear are slowing down the app, then it may be wise to draw any persisting graphics into a BufferedImage, and then display the BufferedImage in your app. There are lots of examples of this in threads in this forum, so I recommend that you search and experiment, and then please keep coming back if you have any questions or problems.

Much luck, and oh by the way, welcome to JavaRanch!
Veronique Kus
Ranch Hand

Joined: Jun 13, 2010
Posts: 41
Thanks for the useful advice and the welcome :-)

Just two little things before I carry on with working on this:
1) do you (or anyone else) have any idea why I can't set my DrawingCanvas' class background color?
2) on the design side, since the Shape classes don't make sense in any other context than my DrawingCanvas class, would it be a good practice to put them inside the DrawingCanvas (or MapTab) class as inner classes?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Veronique Kus wrote:
1) do you (or anyone else) have any idea why I can't set my DrawingCanvas' class background color?

Are you setting the DrawingCanvas's opaque property to false anywhere? That's the most likely cause.
[edit: yes you are. You're also not calling super.paintComponent(g) in your paintComponent method]


2) on the design side, since the Shape classes don't make sense in any other context than my DrawingCanvas class, would it be a good practice to put them inside the DrawingCanvas (or MapTab) class as inner classes?

I'm no pro at this, but if you're sure that no other classes would need to have access to the Shape objects, then yes, it may be a good idea to hide them in the very least in the package or perhaps as a private inner class if it doesn't make the containing class overly large and complex. Also, are you aware that the Java core libraries already have a Shape class whose name is clashing with yours.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
Custom Painting Approaches
Veronique Kus
Ranch Hand

Joined: Jun 13, 2010
Posts: 41
Awesome, all is fixed, thanks! I set my drawingCanvas to opaque and I can see it's background now. Also implemented drawing multiple shapes from a list but I think that in the future, when my application gets more complex, I'll try out the BufferedImage solution to see which one will be more efficient in my case. The last link looks good, I'll definitely have a read.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: previously drawn shapes disappear when new one is created