views:

108

answers:

4

I am building a swing application. At some point, I have to start an "animation":

...
jpanel1.setBackground(Color.Black);

Delay(milli)   

jpanel1.setBackground(Color.White);
...

and so on.

The gui itself and all the logic behind it work.It is just this time depended color-changing that does not. I have read, that swing is not thread safe, but all the examples I found showed me how to start another thread (for example in the background) but never how to stop the current swing-gui thread.

Edit:

The application should work as following:

  1. configuration files are read, jframe is set up.
  2. some simple questions are beeing asked
  3. a dialogue is opened, which explains the animation.
  4. after the user clicked "ok" the animation - some color flashing - is started. the color and the delay between the color-changing is depended on the configuration
  5. another dialogue is opened and the programm continues -> new jpanel inside the jframe, buttons and so on.

the online thing that does not work are the delays between the color-changing. I understand now why it does not work and I am trying to build a timer, which activates a actionlister, which then changes the color and stops the timer... it just seems so much work for a simple delay... and I have to reorganize the entire animation in the application.

+1  A: 

You do not want to stop the GUI thread, even if you want to have a flashing effect. This is because other basic actions, like repainting when the GUI is hidden by other windows, will be stalled. Take a look at Timer. It will allow you to have an event fired on an interval and you can handle that, in the GUI thread, in your actionPerformed method.

akf
A: 

Do the timer on another thread and when the timer goes off it can send an update message for the animation to draw the next frame.

Another consideration is the delay itself. Don't pick a fixed delay-interval. Old games used to do that and they become unplayable on faster computers. Instead what the newer games do is use the speed of the current CPU to figure out how many update events they need a second at runtime, call it a 'delay-factor', and is set when the program starts up. . The timer uses the delay factor so the animation displays correctly even on machines of different clock-speed.

Kelly French
It is not a game. I just need to do some different gui changes, which must happen at a configured timespan with a configured delay between the single steps. The user needs to watch the animation without interaction. The interaction is resumed after the animation. Right now i am trying to implement the Timer from akf, but it takes some time, because I basically have to change everything around the animation to make it work..also there are two different delays...
M.R.
+1  A: 

You will want to use the javax.swing.Timer class and not the java.util.Timer class. The later is preferred when you need general timing the former is preferred for UI updating/changes.

See http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html

You may also want to look at https://timingframework.dev.java.net/.

Avrom
+2  A: 
OscarRyz
The delay between the different pages is exactly what I need. I think the timing framework will solve my problem. Need to take a look at it tommorow. Thx.
M.R.
The timing framework has been left by its author as "on going work" and has been dormant since then (at least 2 years). A better alternative (also open source) is trident (http://kenai.com/projects/trident), also written by a "Swing guru".
jfpoilpret