wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Disappearing Graphics on panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Disappearing Graphics on panel" Watch "Disappearing Graphics on panel" New topic
Author

Disappearing Graphics on panel

Helen Wallace
Greenhorn

Joined: Jul 02, 2007
Posts: 7
Hi All,

I have a problem with disappearing graphics on a JPanel which I'd like to describe. I have a class that extends JPanel that looks like this:



My Polygon class's draw method looks like this:



The idea is that whenever I draw a polygon to the screen I also add it to the arraylist of polygons (I do this in the constructor of the Polygon class) and so whenever the paintComponent method of the MyPanel class is automatically called I loop through the arraylist calling draw() on each of the polygons.

This is sort of work but there is a problem. Whenever I maximise, minimise or resize the frame in which the panel is a component the polygons disappear and only reappear when the frame is moved around the screen a bit.

Does anyone know what I'm doing wrong and how I might solve this issue?

Many thanks,

Helen
sammaiah kyatham
Ranch Hand

Joined: Aug 03, 2003
Posts: 104
Can you call super.paintComponent(g); in your paint component method and check it.

if it will not work even after that, it is good idea to pass the Graphics Object as a parameter to the draw method.

Hope it helps.

Thnx,
Sam
Helen Wallace
Greenhorn

Joined: Jul 02, 2007
Posts: 7
Hi Sammaiah,

I tried calling super.paintComponent but that didn't work. I have access to the Graphics object in the draw method but I don't know what use it will be.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Helen Wallace
Greenhorn

Joined: Jul 02, 2007
Posts: 7
Hi there,

thanks for that. It works but what I don't understand is why it works. Why by passing the graphics context in does the refresh happen automatically? In my draw method I've had access to the panel and I've been doing something like this:



Why does that not work?

Many thanks,

Helen
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
For an idea of how painting works in swing:
Lesson: Performing Custom Painting
First page links to:
Painting in AWT and Swing
and the conclusion
A Closer Look at the Paint Mechanism.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Disappearing Graphics on panel
 
Similar Threads
How to put image on JPanel
Putting a JPanel from one class into a JFrame of a different class?
Simple JPanel question
How to Add Graphics to Jpanel
string concatenation