Java Color alpha values

Status
Not open for further replies.

gumbo

Ars Tribunus Angusticlavius
6,107
Has anyone else noticed that in an application where the component cannot get repainted immediately, the alpha value is not changed for a period of time?

In my current app, a JLabel has Color(1.0f, 0.0f, 0.0f, .20f) applied to the background. The red is noticably pink when set, but there is ~1 second delay between that pinkish color and the correct translucent dark red color. This is due mostly to the immense TCP/IP traffic going on, but I have seperate threads running just about everywhere I can to help eliminate that.

This is not a critical problem, but it is noticable and ugly. This effect was much more visible when an incorrect listener fired, recusing into the method that changed the background ~20 times a sec. The JLabel flashed more pink than the correct color,

Would doing this in another thread help? I assume so, but I already have around 5 running to avoid screen-update deadlock.

gubmo
 

gumbo

Ars Tribunus Angusticlavius
6,107
I would, but it is quite long. I can't create a good short example, because the entire effect is dependent on the massive amount of background TCP/IP traffic going on.

Anyway, I'll try to describe it better.

The main display panel has an image with 6 JLabels (actually my extended class) added to it, with it all in a JLayeredPane. The image is in the default layer, and the JLabels are also added to the default layer. The labels move to the palette layer when they need to be visible.

The main class calls the activate() method, and that label moves to the palette layer and shows an indicator, if necessary. The background red color has been applied on construction.

Sometime during either the move to the palette layer, or when leaving that layer, the label displays a pink color. A repaint is called, and all seems to be fine. However, having 6 simultaneous lables wanting to repaint causes flickering, so they are set to not repaint as much.

If you really want the code, come back in a few minutes and I'll have a hopefully working zip available, but it should be quite large.

Edit: The file is up here. Some instructions:
Wherever you unzipped the file to, there will be a simulator file called cur.sim Open it up and edit the paths, since you can't do that from a dialog yet (You should, but anyway). Run it wil "java simInterface.simInterface" from the created simlink directory. If you have class location errors, add directories to your classpath, starting with ./simlink I don't know how far you will have to go, as doing development screws with the available classes. Load the provided simulaton with File-|Load, then move it to the right hand side of the monitor. Goto Tools-|Run, and watch the yellow RCX image as the robot runs. If it happens to you, it should turn pink then red.

gubmo

[This message was edited by gubmo on April 28, 2002 at 09:00.]
 

Nimdok

Ars Scholae Palatinae
1,214
I downloaded the code and managed to get it to run. I take it the labels you're referring to are the red/blue arrows below the LCD?

I'm still confused by what the labels are used for. When are they supposed to be red? When pink? How is TCP coming into play? How exactly would a correct label behave?

Some vague thoughts:

The image is in the default layer, and the JLabels are also added to the default layer. The labels move to the palette layer when they need to be visible.

Why are the JLabel's present in the layered pane when they are not supposed to be visible? Perhaps they could just be added to the palette layer as soon as they need to be shown?

Are you allocating new JLabel's every time? If so, is the background color set before adding them to the view? When do you call repaint() afterwards? Perhaps you should call repaint(1) (repaint in 1 millisecond) or something along those lines.

Maybe JLabel's are not the solution to your problem. Would it be easier to make a custom JPanel? I don't think they're meant to be used in such a dynamic manner.

Maybe the pinkness is coming from using the red label backgrounds over the regular grey background of the widget? So when you add or remove items to the layer, that pinkness stays till you cover it up. Maybe you need to move the items in a single operation?
 

gumbo

Ars Tribunus Angusticlavius
6,107
Originally posted by Nimdok:
I downloaded the code and managed to get it to run. I take it the labels you're referring to are the red/blue arrows below the LCD?

:D Yeah, it can be hard to install. Deployment has never been my strong point. Anyway, there are labels above and below the LCD - the ones with arrows are them, but there are 3 in the gray strip above.

I'm still confused by what the labels are used for. When are they supposed to be red? When pink? How is TCP coming into play? How exactly would a correct label behave?
The labels are used to indicate when the port that the label covers is accessed - whether read, changed state, etc. They ware always supposed to be red, and never pink. The TCP is how the main screen runs the robot simulation, not by my design. A correct label would simply display the red color, and appear/disappear on command.

Why are the JLabel's present in the layered pane when they are not supposed to be visible? Perhaps they could just be added to the palette layer as soon as they need to be shown?
They are used many times, perhaps many times a second (although the delay keeps them from thrashing with refresh). I can delay the addition once, then they must be controlled entirely from the label itself - it controls all the state.

Are you allocating new JLabel's every time? If so, is the background color set before adding them to the view? When
do you call repaint() afterwards? Perhaps you should call repaint(1) (repaint in 1 millisecond) or something along those lines.
6 JLabels are created - one for each port. The background is set on component construction. A repaint occurs whenever the label is made visible again - trusting Java to auto-repaint after a UI change doesn't work for some reason.

Maybe JLabel's are not the solution to your problem. Would it be easier to make a custom JPanel? I don't think they're meant to be used in such a dynamic manner.
A custom JPanel would only change a few lines of code, I'll try that. JPanels or JLabels are not meant to be used this way?

Maybe the pinkness is coming from using the red label backgrounds over the regular grey background of the widget? So when you add or remove items to the layer, that pinkness stays till you cover it up. Maybe you need to move the items in a single operation?
The transparency part works fine, assuming it is repainted correctly. If I take the setOpaque(true) and add it to the constructor of simShowActive, the labels display fine. I cannot move them in a single op, as each label can be manipulated independently. Logic that caches a change when more changes are expected to occur could be used, but it would most likely break easily and provide little benifit.

Edit: JPanel suffers the same effects. repaint() seems to be called when the window manager feels like it - when another component is repainted, the entire display corrects itself. I'm changing them to repaint(1) now.

gubmo
 

gumbo

Ars Tribunus Angusticlavius
6,107
Oh my god. I can't believe it was this all along. :mad: When this first occurred I suspected it was an issue of the UI being changed outside of the event thread. Turns out I was right. :rolleyes: The UI modification starts in result to an event being registered on a listener.

Through a series of method calls in the class hierarchy, the painting/modification methods are finally called. However, it appears that somewhere in that path, I lose the event-thread execution guarantee. Editing the 2 layer changes that occur outside of another event listener (the activation and deactivation events) fixes the problem, and the components display with no pink.

Ironically, this would all have never been an issue had I implemented my orignal choice, adding event listeners then firing events, instead of calling the straight method. Oh well. Thanks a lot for all the help you have given me Nimdok, without your last post I would never have searched for "JComponent fast switching", which lead me to the relevant Sun pages.

Thanks again,
gubmo
 
Status
Not open for further replies.