Making a game, timing problem

I'm about to make a game which uses swing. And when i do animation, on diffrent systems game executes at diffrent speeds. Here is how i solved it :

// private members

privatestaticint fps, frames, seconds;

// constructor

fps = 30;// assagning a default value

frames = 0;

seconds = System.currentTime();// or whatever it's called, i can't remember now

// main loop

frames++;// counting frames

if ( System.currentTime() - seconds > 1000 ){// if a second has passed

fps = frames;// assign fps

frames = 0;// restart value

seconds = System.currentTime();// update the seconds

}

That's how i calculate fps, which then i use in animation calculations. and so on.

I was just wondering is there a better solution to this ?

[1321 byte] By [Sasa_Ivanovic] at [2007-11-26 12:16:17]
# 1
You could use a timer to update your data model frequently and trigger a repaint afterwards, instead of trying to control the speed of your application by the painting speed.
CeciNEstPasUnProgrammeur at 2007-7-7 14:52:06 > top of Java-index,Archived Forums,Socket Programming...
# 2
Can you post a code example, 'cause i haven't used Timer and TimerTask classes only Threads.
Sasa_Ivanovic at 2007-7-7 14:52:06 > top of Java-index,Archived Forums,Socket Programming...
# 3
> Can you post a code example, 'cause i haven't used> Timer and TimerTask classes only> Threads.Code examples: http://javaalmanac.comor: http://www.google.com
prometheuzz at 2007-7-7 14:52:06 > top of Java-index,Archived Forums,Socket Programming...
# 4
Let's say i have a class named Game, which contains calculateData and drawStuff methods. How could I implement this using Timer in the public static void main(String[] args) function. Could someone please post an example ?
Sasa_Ivanovic at 2007-7-7 14:52:06 > top of Java-index,Archived Forums,Socket Programming...
# 5

> Let's say i have a class named Game, which

> contains calculateData and drawStuff

> methods. How could I implement this using Timer in

> the public static void main(String[] args)

> function. Could someone please post an example ?

See reply #3.

prometheuzz at 2007-7-7 14:52:06 > top of Java-index,Archived Forums,Socket Programming...
# 6
I allready saw it. But the problem is that i don't know when and how to call the right methods from the Game class.
Sasa_Ivanovic at 2007-7-7 14:52:06 > top of Java-index,Archived Forums,Socket Programming...
# 7

> I allready saw it. But the problem is that i don't

> know when and how to call the right methods from the

> Game class.

Well, if you don't know, how could we? It's your game, you designed it.

Let the timer task call calculateData and drawStuff, then wait and repeat.

CeciNEstPasUnProgrammeur at 2007-7-7 14:52:06 > top of Java-index,Archived Forums,Socket Programming...
# 8

> I allready saw it. But the problem is that i don't

> know when and how to call the right methods from the

> Game class.

Your question doesn't make much sense. I think it would be a good idea to do a tutorial:

http://javaboutique.internet.com/tutorials/Java_Game_Programming/

prometheuzz at 2007-7-7 14:52:06 > top of Java-index,Archived Forums,Socket Programming...
# 9
ok thanks to both of you. you helped!
Sasa_Ivanovic at 2007-7-7 14:52:06 > top of Java-index,Archived Forums,Socket Programming...