Hi, I have written a solitaire game (a very small one) but I want to add a timer which will run while i'm playing and will give me the time spent on the game. I imagine it will need multithreading. I'm I right?
Timer
Collapse
X
-
Well, you can use the Date class's getTime method, called when you start and when you end. From there, simply subtract and convert to minutes and seconds (there's probably a builtin way to do this, some sort of formatter (DateFormat?), but I'm not entirely sure). No multithreading needed. -
Then i agree....Originally posted by LaharlWell, you can use the Date class's getTime method, called when you start and when you end. From there, simply subtract and convert to minutes and seconds (there's probably a builtin way to do this, some sort of formatter (DateFormat?), but I'm not entirely sure). No multithreading needed.
@ OP, if you also like to automatically update the timer... You can also use multithreading implementation. ... With a reasonable infinite loop....
Take a look at this thread.
I hope you could also get some idea there...
sukatoa....Comment
-
I'm afraid that's vague. If you want to compute the elapsed time, use System.currentT imeMillis:Originally posted by rpm27Hi, I have written a solitaire game (a very small one) but I want to add a timer which will run while i'm playing and will give me the time spent on the game. I imagine it will need multithreading. I'm I right?
[CODE=Java]long start = System.currentT imeMillis();
//do stuff
long elapsed = System.currentT imeMillis() - start;[/CODE]
If, on the other hand, you want to display a clock ticking away in your Swing GUI, use javax.swing.Tim er:
This Swing Java Tutorial describes developing graphical user interfaces (GUIs) for applications and applets using Swing components
If you want to do something else, please try to express yourself more clearly.Comment
Comment