views:

198

answers:

2

Hi all,

I've been trying to build this small java app. I find it very difficult to design UI in java, tasks that seem very simple become complicated and all these strange misbehaviors occur. In my app I've created a JLayeredPane which contains two layers. One on top on the other, They both contain scrollbars. Here's an explanation of the two layers:

Layer 1: A very big image inside something similar to a scrollpane. The image is scrollable.

Layer 2: A graphics2d object, this object draws an image. Once the image reaches a certain length, the layer gets a scrollpane that advances with the drawing with time.

I'd like to connect both layers. I want layer two to update the scrollbar on layer 1. Meaning that once it reaches a certain length, both scrollbars will advance together. When I try doing that, the two scroll bars really do advance, but ( ! ) this strange flickering occurs. I don't understand what is the reason for the flickering. Is there any other way to implement this in a simple manner? I must have the second layer on top of the first one (drawing on top of image)

since I cannot open a special post for thanking the wonderful people of this forum, I'll do it here. Thank you, you are great help. I hope this problem is solvable as well.

A: 

Without seeing your code it's quite hard to guess where is the problem. Probably you're getting more paint() events than you really need.

Also you can try JXLayer (http://weblogs.java.net/blog/alexfromsun/archive/2008/06/the%5Fnew%5Fjxlayer.html) to show your graphics2d layer.

Carlos Tasada
A: 

It sounds like you're repainting the entire component in some costly way each time - you could try to paint to a BufferedImage to save the image rather than re-generate it each time. Or you could try to mess around with how repaints are handled. I'd suggest this article and this page on Sun's website - both discuss performant painting practices.

Nate