objectdraw
Class EventTimer

java.lang.Object
  extended by objectdraw.EventTimer

public class EventTimer
extends Object

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.

Author:
Russell Zahniser (russell@zahniser.net)

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 otherwise.
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SELF

public static final Object SELF
This is a special object that may be included among the target parameters to have this timer pass itself as a parameter. The parameter which will receive it should have type EventTimer.

Constructor Detail

EventTimer

public EventTimer(Object target,
                  String methodName)
Create a timer that will call target.methodName() every time it fires. The method must not take any parameters.

Parameters:
target - Object whose method will be called
methodName - Name of method to be called

EventTimer

public EventTimer(Object target,
                  String methodName,
                  Object arg1)
Create a timer that will call 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).

Parameters:
target - Object whose method will be called
methodName - Name of method to be called
arg1 - Object to pass as a parameter.

EventTimer

public EventTimer(Object target,
                  String methodName,
                  Object[] args)
Create a timer that will call 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).

Parameters:
target - Object whose method will be called
methodName - Name of method to be called
args - Array of objects to pass for the method's parameters

EventTimer

public EventTimer(Object target,
                  String methodName,
                  Object arg1,
                  Object arg2)
Create a timer that will call 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).

Parameters:
target - Object whose method will be called
methodName - Name of method to be called
arg1 - Object to pass as the first parameter.
arg2 - Object to pass as the second parameter.

EventTimer

public 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. 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).

Parameters:
target - Object whose method will be called
methodName - Name of method to be called
arg1 - 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

fire

public void fire()
Manually fire the timer. This will call the target method, just as if the timer had fired normally.


getDelay

public double getDelay()
Get the timer's delay. This method will return 0 if the timer was set to fire only once.

Returns:
the delay

getStartDelay

public double getStartDelay()
Get the timer's start delay.

Returns:
the start delay

getTimeSinceInitialFiring

public double getTimeSinceInitialFiring()
Return the number of seconds since this timer's initial firing. If the timer is currently performing its initial firing, or if it has not fired since start() was last called, then 0 will be returned.

Returns:
Time since initial firing

getTimeSincePreviousFiring

public double getTimeSincePreviousFiring()
Return the number of seconds since this timer's previous firing. If the timer is currently performing its initial firing, or if it has not fired since start() was last called, then 0 will be returned.

Returns:
Time since previous firing

getTimeSinceStart

public double getTimeSinceStart()
Return the number of seconds since this timer was started. This will report time since the most recent call to start(). If start() has never been called, this method will return 0.

Returns:
Time since this timer was started.

isInitialFiring

public boolean isInitialFiring()
Check whether this is the initial firing of this timer. This method will return true if the timer has not yet completed a firing since it was last start()ed.

Returns:
Whether this is the initial firing

isRunning

public boolean isRunning()
Return true if the timer is currently running, false otherwise.

Returns:
whether the timer is running

setDelay

public 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. 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.

Parameters:
startDelay - Number of seconds to wait before first firing
delay - Number of seconds to wait in between firing

setTarget

public void setTarget(Object target,
                      String methodName)
Set this timer to call target.methodName() every time it fires. The method must not take any parameters.

Parameters:
target - Object whose method will be called
methodName - Name of method to be called

setTarget

public void setTarget(Object target,
                      String methodName,
                      Object arg1)
Set this timer to call 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).

Parameters:
target - Object whose method will be called
methodName - Name of method to be called
arg1 - Object to pass as a parameter.

setTarget

public void setTarget(Object target,
                      String methodName,
                      Object[] args)
Set this timer to call 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).

Parameters:
target - Object whose method will be called
methodName - Name of method to be called
args - Array of objects to pass for the method's parameters

setTarget

public void setTarget(Object target,
                      String methodName,
                      Object arg1,
                      Object arg2)
Set this timer to call 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).

Parameters:
target - Object whose method will be called
methodName - Name of method to be called
arg1 - Object to pass as the first parameter.
arg2 - Object to pass as the second parameter.

setTarget

public 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. 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).

Parameters:
target - Object whose method will be called
methodName - Name of method to be called
arg1 - Object to pass as the first parameter.
arg2 - Object to pass as the second parameter.
arg3 - Object to pass as the third parameter.

start

public void start()
Start the timer, using whatever delay values have been set. The default is to fire every second, starting one second after the timer is started. If the timer was currently running, it will restart, pausing for the initial delay amount all over again.


start

public void start(double startDelay,
                  double delay)
Start the timer. It will fire first after 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.

Parameters:
startDelay - Number of seconds to delay before first firing
delay - Number of seconds to delay between subsequent firings

stop

public void stop()
Stop the timer. This method has no effect if the timer is not running.