0

I am using java.util.timer for a game that I am programming to increment the location of a JLabel but it usually goes much slower than I need it to. Sometimes it goes the correct speed but then for no reason the next time I execute the program it will be slow again. I used the following code for the timer.

java.util.Timer bulletTimer= new java.util.Timer();
bulletTimer.schedule(new bulletTimerTask(), 0, 2);

I also tried javax.swing.timer and had the same problem. Any help would be appreciated.

edit: it works fine with another timer where I set the delay to 2000 ms

  • 2
    Don't use a timer for this -- at least not for each bullet/object. Rather, have *one* timer (manual, e.g. a loop with a yield, or otherwise) and do all the work in that *based on the delta of elapsed time*. –  Dec 03 '11 at 02:51
  • 1
    http://stackoverflow.com/questions/2861933/best-way-to-implement-game-loop-without-freezing-ui-thread, http://stackoverflow.com/questions/7161981/how-to-get-time-delta-independently-from-system-time, http://stackoverflow.com/questions/6847167/some-questions-regarding-game-loop-tick-and-real-time-programming, http://stackoverflow.com/questions/5274619/investigation-of-optimal-sleep-time-calculation-in-game-loop –  Dec 03 '11 at 02:55

2 Answers2

2

Since you are moving a JLabel, I would actually continue to use (a single) swing.Timer. The reason for this is that the callback will always happen "on the EDT" and thus it is okay to access Swing components. (If you are using util.Timer then the update should be posted/queued to the EDT, but this is a little more involved.)

Now, bear in mind that util.Timer and swing.Timer do not have guaranteed timings (other than "will be at least X long") and, to this end, it is important to account for the "time delta" (how long since it was since the last time the update occurred).

This is discussed in the article Fix Your Timestep! While the article was written about a simple game-loop and not a timer, the same concept applies. To get a consistent update pattern for a fixed velocity (no acceleration), simply use:

distance_for_dt = speed * delta_time
new_position = old_position + distance_for_dt

This will account for various fluctuations on a given system -- different system load, process contention, CPU power throttle, moon phase, etc. -- as well as make the speed consistent across different computers.

Once you are familiar with the basic position update, more "advanced" discrete formulas can be used for even more accurate positioning, including those that take acceleration into account.

Happy coding.


As BizzyDizzy poined out, System.nanoTime can be used to compute the time-delta. (There are a few subtle issues with System.currentTimeMillis and clock changes.)

0

You can use System.nanoTime() which came with Java 5 and it is the most high resolution timer in Java. It provides value in nanoseconds.

BizzyDizzy
  • 279
  • 1
  • 4
  • 9
  • How does this make a timer more accurate? Answer: it doesn't. And in fact this doesn't help the OP since he'd be happy if his timing were accurate to microseconds. The solution was posted by pst. – Hovercraft Full Of Eels Dec 03 '11 at 03:08