• Post Reply Bookmark Topic Watch Topic
  • New Topic

A clock counting downwards  RSS feed

 
And Green
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm writing a program that needs to display a clock that counts downwards from 20, in seconds (in realtime) and another clock that counts down in double time ie it decreases by two say every second.

Any pointers on what classes/methods I should look at first?

Thanks in advance.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming you only want psuedo real time I suggest you read this article on Swing timers.

Actual Real Time is difficult to achieve because of all the background tasks etc that are happening and using CPU time so there inconsistenecies in the delay time. To handle this there are 2 basic strategies for performing repeating tasks:

1: Try and make the delay between tasks as consistant as possible.
2: Try to make the combined time of the tasks as close to real time as possible.

The first strategy is good for tasks where smoothness is more important than the actual timing, such as moving graphics. The second strategy is good for tasks where the timing of taks is more important than the smoothness, such as a clock.

Having said that, for a 20 second count down it will probably make little difference whichever strategy you use. If, however, do need strategy 2 you could use java.util.Timer and call it's scheduleAtFixedRate(..) method.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!