This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Disappearing Graphics on panel

 
Helen Wallace
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Helen Wallace
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic