#### Archived

This topic is now archived and is closed to further replies.

# [java] Frame Rate control

This topic is 5763 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In the java game programming tutorials I have been reading they have something like this

public void run ()	{
while (true)  {
DrawFrame();
repaint ();
try
}
catch (Exception exc)
{}
}
}

This dos not seem right to me. I think that that would run at deferent speeds on deferent computers. Do I not understand Thread.sleep or is there a better way to do it? Thanks for any help [edited by - Abob on November 3, 2002 10:52:11 PM]

##### Share on other sites
Thread.Sleep( x ) does exactly that, it sleeps the program. It will stop the program from running excessively fast on fast computers but will not guarantee a constant framerate. If you can measure the framerate or speed of the computer it is running on then you could just adjust the amount of time the thread sleeps. This is only neccessary if timing is paramount though, usually using Thread.Sleep() will work well enough.

Hope that helps

##### Share on other sites
Hi,

normally you should try get the highest frame rate possible. To keep your game in sync you should calculate the time which has passed between the frames. In this case for example: a object which travels from point A to B takes 2 sec, it always needs 2 sec for this distance independant if the system is fast or not. The different is that the fast system will draw more frames, than the slow system, thus the animation looks smoother.

If you control your game by framerate the playability depends on the speed of the system and this is not good.

ciao knudde

##### Share on other sites
quote:
Original post by Knudde
Hi,

normally you should try get the highest frame rate possible. To keep your game in sync you should calculate the time which has passed between the frames. In this case for example: a object which travels from point A to B takes 2 sec, it always needs 2 sec for this distance independant if the system is fast or not. The different is that the fast system will draw more frames, than the slow system, thus the animation looks smoother.

If you control your game by framerate the playability depends on the speed of the system and this is not good.

ciao knudde

Yes. I know about that in C++. The thing is that all the Java code I have seen dos not do that. So I was wondering if there was a reason (like no accurate timer).

##### Share on other sites
You hit the nail on the head. The java timer, in win os, has a 50 ms granularity... So if you need to be more accurate the 50 ms, try looking up snowmoon''s timer, it has been posted a hundred times in this forum. Otherwise I believe there is an example of one on the javagaming.org site..

##### Share on other sites
Yeah, and if you lock the frame rate by using sleep or snowmoons timer then by updating graphics by the number of tick counts that have passed you are in essence doing the same thing that knudde said. The only problem would be if you set REFRESHRATE to something like 20 (50 fps) and your applet runs on a really crappy computer that can''t even do that.

Its IMPOSSIBLE to get smooth animation using System.currentTimeMillis() on win 98-ME. The two games on my site use thread.sleep(REFRESHRATE). But even if you use snowmoons timer it only runs at 300-500 ticks per second on my machine wich is way to wide of a range to get smooth animation with updating by passed time. (not sure if that made sense.) Using sleep and notify is the only way to go.

1. 1
2. 2
3. 3
frob
15
4. 4
5. 5

• 20
• 12
• 13
• 14
• 84
• ### Forum Statistics

• Total Topics
632143
• Total Posts
3004417

×