• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Disappearing Graphics on panel

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Helen Wallace
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic