edu.davidson.tools
Class SClock

java.lang.Object
  |
  +--edu.davidson.tools.SClock
All Implemented Interfaces:
java.lang.Runnable, SDataSource

public final class SClock
extends java.lang.Object
implements java.lang.Runnable, SDataSource

A runnable class designed to provide animation for Physlets. Objects wishing to receive notification of clock ticks must implement the SStepable interface. SStepable objects must register themselves using the addClockListener method. The stepping thread is created by the constructor and put into a wait state. StartClock() will notify the thread to start running. SClock is also a SDataSourceso that other applets can be notified of clock ticks using JavaScript. DataConnections and inter-appletc communication is describted in the book "Teaching with Physlets."


Constructor Summary
SClock()
          Create a new SClock.
SClock(SApplet owner)
          Create a new SClock to animate the given SApplet.
 
Method Summary
 void addClockListener(SStepable cl)
          Add an object to the list of objects that will be stepped at every clock tick.
 void doStep()
          Do one time step and update all clock listeners.
 double getDt()
          Returns the time step.
 double getFPS()
          Estimate the frames per second, FPS, for the animation.
 int getID()
          Get the integer id of the object.
 double getMaxTime()
          Returns the maximum time for cycle and one-shot mode.
 double getMinTime()
          Returns the minimum time for cycle mode.
 SApplet getOwner()
          Get the SApplet owner of the clock.
 double getTime()
          Returns the animation time.
 double getTimeStep()
          Returns the time step.
 double[][] getVariables()
          Get the variables for use by a data connection.
 java.lang.String[] getVarStrings()
          Get the variable string for use by a data connection.
 boolean isContinous()
          Determine if the clock is in continuous mode.
 boolean isCycle()
          Determine if the clock is in cycle mode.
 boolean isOneShot()
          Determine if the clock is in oneShot mode.
 boolean isRunning()
          Determine if the clock is running.
 void panicStopClock()
          Stop the thread at all costs.
 void removeAllClockListeners()
          Remove all objects from the list that will be stepped at every clock tick.
 void removeClockListener(SStepable cl)
          Remove an object from the list of objects that will be stepped at every clock tick.
 void run()
          The run method passed to the thread.
 void setContinuous()
          Set the clock to always increment the time.
 void setCycle(double min, double max)
          Set the clock to cycle between min and max time values.
 void setDt(double newDt)
          Set the time step.
 void setFPS(double fps)
          Set the frames per second, FPS, for the animation.
 void setOneShot(double min, double max)
          Set the clock to run one time from the min time to the max time.
 void setOwner(SApplet owner)
          Set the SApplet owner of the clock.
 void setTime(double t)
          Sets the animation time.
 void setTimeStep(double newDt)
          Set the time step.
 void startClock()
          Notify the thread to start running in order to produce clock ticks.
 void stopClock()
          Notify the thread to stop running.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SClock

public SClock()
Create a new SClock. A clock thread is created but it immediately enters a wait state. A clock will update its clock listeners after the startClock method has been called.

SClock

public SClock(SApplet owner)
Create a new SClock to animate the given SApplet. The normal behavior is for every SApplet to have one clock. The static variable runningID in SApplet is set to the clock's owner when a clock starts running. Other SApplets can check this variable to determine if they should stop their animation in order to improve performance.
Parameters:
owner - the SApplet that uses this clock for animation.
See Also:
SClock()
Method Detail

addClockListener

public void addClockListener(SStepable cl)
Add an object to the list of objects that will be stepped at every clock tick. A clock listener can only be added once.
Parameters:
cl - the clock listener

removeClockListener

public void removeClockListener(SStepable cl)
Remove an object from the list of objects that will be stepped at every clock tick.
Parameters:
cl - the clock listener

removeAllClockListeners

public void removeAllClockListeners()
Remove all objects from the list that will be stepped at every clock tick.
See Also:
removeClockListener(SStepable)

doStep

public void doStep()
Do one time step and update all clock listeners. This method is designed to single step an animation when the clock is stopped. This method will check to see if there are any restrictions on the time, such as oneShot or cycle, before proceeding to step the clock listeners.

getTime

public final double getTime()
Returns the animation time.
Returns:
the time.

getMinTime

public final double getMinTime()
Returns the minimum time for cycle mode.
Returns:
minTime.

getMaxTime

public final double getMaxTime()
Returns the maximum time for cycle and one-shot mode.
Returns:
maxTime.

setTime

public void setTime(double t)
Sets the animation time.
Parameters:
t - the time.

getDt

public final double getDt()
Returns the time step.
Returns:
the time step.

setDt

public void setDt(double newDt)
Set the time step.
Parameters:
newDt - the time step for each clock tick.

getTimeStep

public final double getTimeStep()
Returns the time step.
Returns:
the time step.

setTimeStep

public void setTimeStep(double newDt)
Set the time step.
Parameters:
newDt - the time step for each clock tick.

setContinuous

public void setContinuous()
Set the clock to always increment the time.
See Also:
setCycle(double, double), setOneShot(double, double)

setCycle

public void setCycle(double min,
                     double max)
Set the clock to cycle between min and max time values.
Parameters:
min - the time minimum.
max - the time maximum.
See Also:
setContinuous(), setOneShot(double, double)

setOneShot

public void setOneShot(double min,
                       double max)
Set the clock to run one time from the min time to the max time.
Parameters:
min - the time minimum.
max - the time maximum.
See Also:
setContinuous(), setCycle(double, double)

startClock

public void startClock()
Notify the thread to start running in order to produce clock ticks.
See Also:
stopClock()

panicStopClock

public void panicStopClock()
Stop the thread at all costs. Should only be called by an applet's destroy() method or to clear an error condition.
See Also:
stopClock()

stopClock

public void stopClock()
Notify the thread to stop running. All SStepable objects must complete their last step before the clock can stop.
See Also:
startClock()

getFPS

public double getFPS()
Estimate the frames per second, FPS, for the animation. The frames per second is in real time. Each frame will advance the clock by a time step, dt.
See Also:
getTimeStep()

setFPS

public void setFPS(double fps)
Set the frames per second, FPS, for the animation. The frames per second is in real time. Each frame will advance the clock by a time step, dt.
See Also:
setTimeStep(double)

isCycle

public boolean isCycle()
Determine if the clock is in cycle mode.

isOneShot

public boolean isOneShot()
Determine if the clock is in oneShot mode.

isContinous

public boolean isContinous()
Determine if the clock is in continuous mode.

isRunning

public boolean isRunning()
Determine if the clock is running. That is, are SStepable objects being updated every clock tick.

run

public void run()
The run method passed to the thread. DO NOT access this method.
Specified by:
run in interface java.lang.Runnable

getVariables

public double[][] getVariables()
Get the variables for use by a data connection. SClock has a single variable, t.
Specified by:
getVariables in interface SDataSource
Returns:
the variables.

getVarStrings

public java.lang.String[] getVarStrings()
Get the variable string for use by a data connection. SClock has a single variable string, "t".
Specified by:
getVarStrings in interface SDataSource

getID

public int getID()
Get the integer id of the object.
Specified by:
getID in interface SDataSource
Returns:
the variable strings.

setOwner

public void setOwner(SApplet owner)
Set the SApplet owner of the clock.
Specified by:
setOwner in interface SDataSource
Parameters:
owner - the owner.

getOwner

public SApplet getOwner()
Get the SApplet owner of the clock.
Specified by:
getOwner in interface SDataSource
Returns:
the SApplet.

Copyright Wolfgang Christian

Visit the Davidson CollegeSTools Documentation Page