Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GlassPane displays flickering

 
Alejandro Barrero
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a simple example drawing a string to a glass pane:


The problem is that the second string displays flickering. Setting the glass pane visible doesn't work either.
 
Rob Camick
Ranch Hand
Posts: 2612
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should NEVER invoke repaint() like that inside a paintComonent() method. This will cause an infinite loop.

You should also not reference the glass pane like that.



I don't know what you are attempting to do so all I can do is suggest you read the section from the Swing tutorial on How to Use Root Panes for an example that shows how to use a Glass Pane.

Also, that setFrame(...) method is not needed. If you want to know the frame a component has been added to you can use:

 
Alejandro Barrero
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Rob. I coded my example too hastily; I should get the parent in another way. What I am trying to do is draw strings in a JPanel over the image of a JPG file; I can do it by drawing the strings on the graphics of the panel, but it takes a long time to redraw the JPG image. I want to draw the strings on the glass pane and repaint it.
 
Rob Camick
Ranch Hand
Posts: 2612
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Drawing a string on a glass pane will not be any faster than drawing strings on the panel directly.

If you want the image below the glass pane to be visible then the glass pane must be non-opaque. Therefore every time you repaint the glass pane, Swing must also repaint the component below the glass pane first.

If you want to display a string on your image then you can just add labels to the image. Something like:

 
Alejandro Barrero
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again Rob. I had read the How to Use Root Panes tutorial and I learned how to capture mouse clicks, but I didn't learn all I should about glass panes. If I cannot repaint the glass pane independently then I must give up.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic