Search code examples
javamultithreadingswingswingworker

How to implement a count down timer in swing


I would like to know how should I implement a count down timer in swing.
I'm currently developing a net game with count down timer, and I need the Timer to be shown all the time in the corner.
The problem is I don't know how to implement it in away that the game screen will be responsive all the time for the user in the same time the timer is on..

1.I'm asking it because I know every update to the view is being held through the event dispatch thread but it can also freeze my game if there is something which run there consistently (like timer).

2.Design aspect :Do you know some nice ways of how to show the timer in an attractive and maybe animated way?

Thank you very much in advance


Solution

  • Use the javax.swing.Timer class. This will fire action events at fixed intervals, which will prevent the GUI from becoming unresponsive.

    As for animation, see the 2D Graphics tutorial.