java.lang.Objectobjectdraw.EventTimer
public class EventTimer
EventTimer
will repeatedly send a message to an object by calling
one of its methods with a preset list of parameters. The target object, method
to call, and values for the parameters are set when the EventTimer
is created. It can then be told to start()
sending events, with
some delay before the first event and then a different delay between each later
event. The timer will then continue to call that method until it is told to
stop()
. It is also possible to have the timer fire once and stop
automatically, by giving it a delay that is less than or equal to zero.
Field Summary | |
---|---|
static Object |
SELF
This is a special object that may be included among the target parameters to have this timer pass itself as a parameter. |
Constructor Summary | |
---|---|
EventTimer(Object target,
String methodName)
Create a timer that will call target.methodName() every time it
fires. |
|
EventTimer(Object target,
String methodName,
Object arg1)
Create a timer that will call target.methodName(arg1) every time it
fires. |
|
EventTimer(Object target,
String methodName,
Object[] args)
Create a timer that will call target.methodName(args[0], args[1], ...)
every time it fires. |
|
EventTimer(Object target,
String methodName,
Object arg1,
Object arg2)
Create a timer that will call target.methodName(arg1, arg2) every time it
fires. |
|
EventTimer(Object target,
String methodName,
Object arg1,
Object arg2,
Object arg3)
Create a timer that will call target.methodName(arg1, arg2, arg3) every
time it fires. |
Method Summary | |
---|---|
void |
fire()
Manually fire the timer. |
double |
getDelay()
Get the timer's delay. |
double |
getStartDelay()
Get the timer's start delay. |
double |
getTimeSinceInitialFiring()
Return the number of seconds since this timer's initial firing. |
double |
getTimeSincePreviousFiring()
Return the number of seconds since this timer's previous firing. |
double |
getTimeSinceStart()
Return the number of seconds since this timer was started. |
boolean |
isInitialFiring()
Check whether this is the initial firing of this timer. |
boolean |
isRunning()
Return true if the timer is currently running, false |
void |
setDelay(double startDelay,
double delay)
Set this timer to pause for startDelay before it first
fires, and then fire then and every delay seconds
after that. |
void |
setTarget(Object target,
String methodName)
Set this timer to call target.methodName() every time it
fires. |
void |
setTarget(Object target,
String methodName,
Object arg1)
Set this timer to call target.methodName(arg1) every time it
fires. |
void |
setTarget(Object target,
String methodName,
Object[] args)
Set this timer to call target.methodName(args[0], args[1], ...)
every time it fires. |
void |
setTarget(Object target,
String methodName,
Object arg1,
Object arg2)
Set this timer to call target.methodName(arg1, arg2) every time it
fires. |
void |
setTarget(Object target,
String methodName,
Object arg1,
Object arg2,
Object arg3)
Set this timer to call target.methodName(arg1, arg2, arg3) every
time it fires. |
void |
start()
Start the timer, using whatever delay values have been set. |
void |
start(double startDelay,
double delay)
Start the timer. |
void |
stop()
Stop the timer. |
static void |
stopAll()
Stop all active timers. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final Object SELF
EventTimer
.
Constructor Detail |
---|
public EventTimer(Object target, String methodName)
target.methodName()
every time it
fires. The method must not take any parameters.
target
- Object whose method will be calledmethodName
- Name of method to be calledpublic EventTimer(Object target, String methodName, Object arg1)
target.methodName(arg1)
every time it
fires. The method to be called must have a single parameter matching the type of
arg1
. If the parameter is a primitive type (int
,
double
, etc.) you can use the appropriate "wrapper" class for its
value (as in, new Integer(2)
instead of just 2).
target
- Object whose method will be calledmethodName
- Name of method to be calledarg1
- Object to pass as a parameter.public EventTimer(Object target, String methodName, Object[] args)
target.methodName(args[0], args[1], ...)
every time it fires. The method to be called must have the same number of parameters
as there are objects in args
, and the types must match. If one of the
parameters is a primitive type (int
, double
, etc.) you
can use the appropriate "wrapper" class for its value (as in,
new Integer(2)
instead of just 2).
target
- Object whose method will be calledmethodName
- Name of method to be calledargs
- Array of objects to pass for the method's parameterspublic EventTimer(Object target, String methodName, Object arg1, Object arg2)
target.methodName(arg1, arg2)
every time it
fires. The method to be called must have exactly two parameters, and they must match
the types of arg1
and arg2
. If one of the parameters is a
primitive type (int
, double
, etc.) you can use the appropriate
"wrapper" class for its value (as in, new Integer(2)
instead of just 2).
target
- Object whose method will be calledmethodName
- Name of method to be calledarg1
- Object to pass as the first parameter.arg2
- Object to pass as the second parameter.public EventTimer(Object target, String methodName, Object arg1, Object arg2, Object arg3)
target.methodName(arg1, arg2, arg3)
every
time it fires. The method to be called must have exactly three parameters, and they
must match the types of arg1
, arg2
and arg3
.
If one of the parameters is a primitive type (int
, double
,
etc.) you can use the appropriate "wrapper" class for its value (as in,
new Integer(2)
instead of just 2).
target
- Object whose method will be calledmethodName
- Name of method to be calledarg1
- Object to pass as the first parameter.arg2
- Object to pass as the second parameter.arg3
- Object to pass as the third parameter.Method Detail |
---|
public void fire()
public double getDelay()
public double getStartDelay()
public double getTimeSinceInitialFiring()
start()
was last called, then 0 will be returned.
public double getTimeSincePreviousFiring()
start()
was last called, then 0 will be returned.
public double getTimeSinceStart()
start()
. If start()
has never been called, this method will return 0.
public boolean isInitialFiring()
start()
ed.
public boolean isRunning()
true
if the timer is currently running, false
otherwise.
- Returns:
- whether the timer is running
public void setDelay(double startDelay, double delay)
startDelay
before it first
fires, and then fire then and every delay
seconds
after that. If delay
is less than or equal to zero, the
timer will fire only once, after startDelay
. If the timer
is currently running, it will try to adjust its schedule so that its
next firing will be as close as possible to delay
seconds
after when it last fired. If it is stopped, this method will not
start it.
startDelay
- Number of seconds to wait before first firingdelay
- Number of seconds to wait in between firingpublic void setTarget(Object target, String methodName)
target.methodName()
every time it
fires. The method must not take any parameters.
target
- Object whose method will be calledmethodName
- Name of method to be calledpublic void setTarget(Object target, String methodName, Object arg1)
target.methodName(arg1)
every time it
fires. The method to be called must have a single parameter matching the type of
arg1
. If the parameter is a primitive type (int
,
double
, etc.) you can use the appropriate "wrapper" class for its
value (as in, new Integer(2)
instead of just 2).
target
- Object whose method will be calledmethodName
- Name of method to be calledarg1
- Object to pass as a parameter.public void setTarget(Object target, String methodName, Object[] args)
target.methodName(args[0], args[1], ...)
every time it fires. The method to be called must have the same number of parameters
as there are objects in args
, and the types must match. If one of the
parameters is a primitive type (int
, double
, etc.) you
can use the appropriate "wrapper" class for its value (as in,
new Integer(2)
instead of just 2).
target
- Object whose method will be calledmethodName
- Name of method to be calledargs
- Array of objects to pass for the method's parameterspublic void setTarget(Object target, String methodName, Object arg1, Object arg2)
target.methodName(arg1, arg2)
every time it
fires. The method to be called must have exactly two parameters, and they must match
the types of arg1
and arg2
. If one of the parameters is a
primitive type (int
, double
, etc.) you can use the appropriate
"wrapper" class for its value (as in, new Integer(2)
instead of just 2).
target
- Object whose method will be calledmethodName
- Name of method to be calledarg1
- Object to pass as the first parameter.arg2
- Object to pass as the second parameter.public void setTarget(Object target, String methodName, Object arg1, Object arg2, Object arg3)
target.methodName(arg1, arg2, arg3)
every
time it fires. The method to be called must have exactly three parameters, and they
must match the types of arg1
, arg2
and arg3
.
If one of the parameters is a primitive type (int
, double
,
etc.) you can use the appropriate "wrapper" class for its value (as in,
new Integer(2)
instead of just 2).
target
- Object whose method will be calledmethodName
- Name of method to be calledarg1
- Object to pass as the first parameter.arg2
- Object to pass as the second parameter.arg3
- Object to pass as the third parameter.public void start()
public void start(double startDelay, double delay)
startDelay
seconds,
then every delay
seconds after that. If delay
is
less than or equal to zero, the timer will only fire once, and then will
stop running. If the timer was currently running, it will restart, pausing
for the initial delay amount all over again.
startDelay
- Number of seconds to delay before first firingdelay
- Number of seconds to delay between subsequent firingspublic void stop()
public static void stopAll()
EventTimer
s
from just merrily churning away with no applet left to draw on.