How to use delay in a swing app

I am creating a rotation application. At some point I have to start the "animation":

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

Delay(milli)   

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

      

etc.

The gui itself and all of its logic works. This time the color change depended, which is not. I 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 app should work like this:

  • config files are read, jframe is set up.
  • some simple questions asked.
  • a dialog opens that explains the animation.
  • after the user clicks "ok", the animation - some color flashes - starts. color and delay between color change depends on configuration
  • another dialog opens and the program continues -> new jpanel inside jframe, buttons, etc.

online game not working is delays between color change. Now I understand why this doesn't work and I am trying to create a timer that activates the actionlister, which then changes color and stops the timer ... it just seems like so much work for a simple delay ... and I have to reorganize the whole animation in the application.

+2


a source to share


4 answers


Take a look at: https://timingframework.dev.java.net/

and samples which are included in http://filthyrichclients.org/

They provide some very good information on how animation works and how Timer is used. You will have a good idea of ​​how it works.

I made a sample animation here with Swing after reading these:

reverse demo app http://img580.imageshack.us/img580/742/capturadepantalla201004wd.png Java application hosting blog.stackoverflow.com page (click image to see demo video)

But I'm not even sure what you want to achieve.



EDIT

I read about temporal structure to better understand what it is, but I haven't actually used it (it's useful to create animations without linear times - i.e. every second like mine, but things like 1, 5, 3, 2 seconds)

The code I'm using in the demo above is exactly :

final Timer imageTimer = new Timer();
imageTimer.schedule( new TimerTask() {
    public void run() {
        changeImage();
    }
}, 0, 10000 ); //<-- every 10 seconds. 

      

The animation for "stackoverflowing" and countdown takes a similar approach.

+1


a source


You don't want to stop the GUI thread even if you want to have a blinking effect. This is because other basic actions such as repainting when the GUI is hidden by other windows will get stuck. Take a look at Timer

. This will allow you to fire the event at an interval and you can handle this on the GUI thread in your method actionPerformed

.



+1


a source


You want to use a class javax.swing.Timer

and not a class java.util.Timer

. The later version is preferred when you need a total time, the former is preferred for updating / changing the UI.

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

You can also look at https://timingframework.dev.java.net/ .

+1


a source


Make the timer on a different thread, and when the timer goes out, it can send an update message for the animation to draw the next frame.

Another consideration is the delay itself. Don't choose a fixed delay interval. Older games used this and became unplayable on faster computers. Instead of what new games do, the speed of the current cpu is used to figure out how many event updates they need per second at runtime, call it a "latency factor" and set when the program starts. The timer uses a latency factor so the animation is displayed correctly even on machines with different clock speeds.

0


a source







All Articles