Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timer variable

Status
Not open for further replies.

infinitysquadron

Programmer
Aug 2, 2002
20
CA
Hi all. I'm new to this forum and fairly new to Java. I've been trying to write a game in the QBASIC language, but QB has way to many drawbacks. I'm re-writing it in Java.
This is going ok, but I can't seem to find something that works like the TIMER variable in QB. TIMER is a variable that keeps track of the number of seconds that have passed since midnight. I need it for some real-time delays. Is there a method of some kind that does something similar? Is there maybe another way create a delay accurately on systems of different speeds?

 
To be honest with you, if you think you're comfortable enough with Java, I believe that the Timer object would best suit your needs. It works in real time, and uses the common mechanism of listeners/events. The documentation is at
If, however, you really want to get the number of seconds that have passed since midnight, you would want to use the Calendar class and do something like:

Code:
Calendar now = java.util.Calendar.getInstance();
int seconds = now.get(SECOND) + 60 * (now.get(MINUTE) + 60 * now.get(HOUR));

Ed
[afro]
 
Thanx! The timer object looks great with the millisecond accuracy. QB never had that kind of precision. I'll play around with it a bit and see how it works.
 
I played around with the Timer class for a bit, but I couldn't get it to work. But I don't think it was really what I needed in the first place. What I really need is a simple delay method, that's accuracte in milliseconds. I don't need it to actually do anything. Just do nothing for a few milliseconds. I could just use a recursion method to waste time like some programms do, but that's not very accuracte, especially when I'm giving this to 2 people; one of which has one of the best computers I've ever seen and the other that is being looked at by paleantologists (...spelled wrong I think).
 
Okay, you want this method it sounds like, it's a little odd to explain without knowledge of threading, anyways, try:
Thread.sleep(number of milliseconds);

that should be what you want it sounds.

JavaDude32
 
Thanx :) Worked perfectly as soon as I figured out how to
Code:
catch
it properly. That was the last of all of the major things I had to learn before I could really start my reprogramming. Now comes the fun part...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top