Archived

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

Telmo_Amaro

[java] Timer that runs in Explorer without any plug-in?

Recommended Posts

I''ve programmed 2 games in java so far (applets) and two are to come. But the use of Timer doesn''t allow Explorer to run the applet if the java plug-in is downloaded. It happens that the plug-in is 11 megs, and most of the java applet players have 56 kb modems... this obviously scare people way from my games. Is there any other way to use a timer without using the swing? www.codingdreams.xrs.net Telmo Amaro

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Ok, for reference Timer is part of JDK1.3+ java.util.* heirarchy, and is not a part of swing (that had me confused for a moment).

As far as I can tell there is no implementation of a timer anywhere else in the API''s so it''s time to write your own.

My first stab is included below, note it uses a callback function to get the class that instigated the timer to call a method.

In practice replace ''Caller'' with your own object and define a
TIMERANOUT() method that does what you want when the timer runs out.

Note: I''m sure there are many problems with this way of doing it, if anyone has a better way please let me know .

public class TimeOut implements Runnable
{

private long time = 0;
private long initialTime = 0;
private Caller CALLER;
private boolean active = true;

public TimeOut(long time, Caller CALLER){
this.time = time;
this.CALLER = CALLER;
initialTime = time;
new Thread(this).start();
}

public void run()
{
while (active){
time = time - 10;
if (time <= 0){
CALLER.TIMERANOUT();
active = false;
}

try{
Thread.currentThread().yield();
Thread.currentThread().sleep(9);
}catch(Exception e){
}
}
}

public void stop(){
active = false;
}

public void reset(){
time = initialTime;
}

public void timeOut(){
time = 0;
}

public void setTimeOut(long t){
initialTime = t;
time = t;
}

}

Share this post


Link to post
Share on other sites
search this forum and javagaming.org. I have posted full source code ofer at javagaming.org for a timing class that runs as a high priority background thread and sends object notifications every n times/secomd. Works well to a point, and it automatically corrects itself for errors. It''s better than the built in function for timing regualr events.

Cheers.

Share this post


Link to post
Share on other sites
snowmoon''s timer will work. only thing you have to
keep in mind is, you will possibly get a security-
exception when setting a thread to high-prio from
inside an untrusted applet. so you may have
to keep the initial priority.

Share this post


Link to post
Share on other sites
perhaps a stupid question, but i get somewhat odd results, probably due to that i''m not using it the way it is intended to be used or something.

so i''d appreciate if someone would write some pseudo code describing how it''s supposed to be used.

oh i''m talking about the mentioned SleepTimer.



_______________________
http://mill.3dfxsweden.net

Share this post


Link to post
Share on other sites