Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing Timer  RSS feed

 
j shap
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi-

I'm using a Swing Timer to run an animation in an applet. In every cycle a calculation is done and a figure drawn on a BufferedImage, and that image is drawn to my JPanel subclass via PaintComponent, via repaint(). The timing is sometimes extremeley ragged. I've measured elapsed time at various points in the cycle by printing out Date.getTime(), and my code takes 10 or 20 or 30 ms to run at each cycle (I don't know why such round numbers!). But when I have the timer set to 50 ms or less, the timer seems to often delay firing so that cycles will take 50-100 ms. The timer works perfectly when the delay is 100ms, but not dependably for delay <= 50ms, which I need. (I'm running AppletViewer under WinXP, 1.6GHz PentiumIV)



startRot() is called in response to a button push. As I said, updateimage() and paintComponent() usually finish withing 30ms of the timer firing. Any clues as to why the timer won't just fire every 50ms? Thanks.

JS
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JS,
I'm not sure, but it may be due to the Windows clock granularity.
Here are the results of a search I did of the "comp.lang.java*" newsgroups. It may be helpful.

Good Luck,
Avi.
 
j shap
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avi,

Thanks for the search. The consensus seems to be that clock granularity on WinXP is 10ms. I don't see how this quite explains the timer sometimes firing 30-50ms late. And then there are stretches where it performs fine. No doubt this has to do with my OS sometimes eating up a lot of time elsewhere, but shouldn't the timer still fire within 10ms of the specified 50ms delay? Anyway, thanks for your efforts.

Jon
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!