开发者

How to use a delay in a swing application

开发者 https://www.devze.com 2022-12-29 13:12 出处:网络
I am building a swing application. At some point, I have to start an \"animation\": ... jpanel1.setBac开发者_C百科kground(Color.Black);

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

...
jpanel1.setBac开发者_C百科kground(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.


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

and the samples that come in http://filthyrichclients.org/

They provide some very good information on how animation work and using the Timer framework. You'll have a good understanding of how it works.

I did a sample animation here with Swing after reading those:

count down demo app http://img580.imageshack.us/img580/742/capturadepantalla201004wd.png Java application featuring blog.stackoverflow.com page ( click on image to see the demo video )

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

EDIT

I read about the timing framework to understand better what is all about, but I actually didn't use it ( it is useful to create animations with no linear times - ie no every second as mine, but things like 1, 5, 3, 2 seconds )

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

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

The animation for the "stackoverflowing" and the count down use a similar approach.


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.


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/.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消