File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes GlassPane displays flickering Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "GlassPane displays flickering" Watch "GlassPane displays flickering" New topic
Author

GlassPane displays flickering

Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 311
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.


Your help will be greatly appreciated,
Alejandro Barrero
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2179
    
    7
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

Joined: Aug 01, 2005
Posts: 311
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

Joined: Jun 13, 2009
Posts: 2179
    
    7
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

Joined: Aug 01, 2005
Posts: 311
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.
 
Consider Paul's rocket mass heater.
 
subject: GlassPane displays flickering