public class Animator
extends java.lang.Thread
Thread
to provide several features that make
it easier to build animations. These features include a pause
method that does not raise an exception and a requestTermination
method that signals that the execution of this thread should stop at its next
opportunity. It also includes hooks to support a start/stop/single-step model
for algorithm animation.Modifier and Type | Field and Description |
---|---|
static int |
CALLING
Constant indicating that the animator is running until the end of the current
call.
|
static int |
FINISHED
Constant indicating that the animator has finished its
run
method. |
static int |
INITIAL
Constant indicating that the animator has not yet started.
|
static int |
RUNNING
Constant indicating that the animator is running.
|
static int |
STEPPING
Constant indicating that the animator is running in single-step mode.
|
static int |
STOPPED
Constant indicating that the animator is suspended waiting for restart.
|
static int |
STOPPING
Constant indicating that the animator should stop at the next trace point.
|
static int |
TERMINATING
Constant indicating that the animator has been asked to terminate.
|
Constructor and Description |
---|
Animator()
Creates a new
Animator object. |
Animator(java.lang.Runnable runnable)
Creates a new
Animator object with the specified runnable
object. |
Animator(java.lang.ThreadGroup group)
Creates a new
Animator object and assigns it to the specified
thread group. |
Animator(java.lang.ThreadGroup group,
java.lang.Runnable runnable)
Creates a new
Animator object with the specified runnable object
and assigns it to the specified thread group. |
Modifier and Type | Method and Description |
---|---|
protected void |
breakHook()
This method is called before the animator executes a breakpoint.
|
void |
breakpoint()
Suspends the animator until one of the restart actions is triggered.
|
boolean |
buttonAction(java.lang.String actionCommand)
Triggers an action for the action specified by the action command.
|
void |
callAction()
Triggers a
"call" action, as if the Call button is
pushed. |
void |
checkForTermination()
Checks to see whether this
Animator has been asked to terminate. |
protected void |
controllerHook()
This method is called before the animator changes state in a way that might
interest a controller.
|
void |
delay()
Delays the calling thread according to the speed.
|
protected void |
delayHook()
This method is called before the animator enters a timing delay.
|
int |
getAnimatorState()
Returns the state of the animator.
|
double |
getSpeed()
Returns the speed parameter for the animator.
|
java.awt.Component |
getSpeedBar()
Returns the speed bar for the animator, if any.
|
void |
pause(double milliseconds)
Delays this thread for the specified time, which is expressed in
milliseconds.
|
void |
registerSpeedBar(javax.swing.JScrollBar scrollBar)
Registers the specified scroll bar as the delay controller for the animator.
|
void |
registerSpeedBar(javax.swing.JSlider slider)
Registers the specified slider as the delay controller for the animator.
|
void |
requestTermination()
Signals the
Animator that it should stop running at the next
available opportunity, which is when the client next calls pause
or checkForTermination . |
protected void |
resumeHook()
This method is called before the animator starts or restarts after a
breakpoint.
|
void |
run()
Specifies the code for the animator.
|
void |
setSpeed(double speed)
Sets the speed parameter for the animator.
|
void |
start()
Starts the thread.
|
void |
startAction()
Triggers a
"start" action, as if the Start button
is pushed. |
void |
stepAction()
Triggers a
"step" action, as if the Step button is
pushed. |
void |
stopAction()
Triggers a
"stop" action, as if the Stop button is
pushed. |
void |
trace()
Checks the state of the animator and executes any actions have been
requested.
|
void |
trace(int depth)
Checks the state of the animator and executes any actions have been requested
to occur at the specified call stack depth.
|
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, stop, stop, suspend, toString, yield
public static final int INITIAL
public static final int RUNNING
public static final int STEPPING
public static final int CALLING
public static final int STOPPING
public static final int STOPPED
public static final int FINISHED
run
method.public static final int TERMINATING
public Animator()
Animator
object.public Animator(java.lang.ThreadGroup group)
Animator
object and assigns it to the specified
thread group.group
- The ThreadGroup
to which the new thread is assignedpublic Animator(java.lang.Runnable runnable)
Animator
object with the specified runnable
object.runnable
- Any object that implements the Runnable
interfacepublic Animator(java.lang.ThreadGroup group, java.lang.Runnable runnable)
Animator
object with the specified runnable object
and assigns it to the specified thread group.group
- The ThreadGroup
to which the new thread is
assignedrunnable
- Any object that implements the Runnable
interfacepublic void run()
run
in interface java.lang.Runnable
run
in class java.lang.Thread
public int getAnimatorState()
INITIAL
,
RUNNING
,
STEPPING
,
CALLING
,
STOPPING
,
STOPPED
,
FINISHED
, or
TERMINATING
, as defined in this
class.public void pause(double milliseconds)
Thread.sleep
, this method never throws an
exception.milliseconds
- The sleep time in millisecondspublic void startAction()
"start"
action, as if the Start
button
is pushed.public void stopAction()
"stop"
action, as if the Stop
button is
pushed.public void stepAction()
"step"
action, as if the Step
button is
pushed.public void callAction()
"call"
action, as if the Call
button is
pushed.public boolean buttonAction(java.lang.String actionCommand)
actionCommand
- The action command from the buttontrue
if the action command is recognizedpublic void setSpeed(double speed)
double
between 0.0 and 1.0, for which 0.0 is very slow and 1.0 is as fast as the
system can manage.speed
- A double between 0.0 (slow) and 1.0 (fast)public double getSpeed()
double
between 0.0 and 1.0, for which 0.0 is very slow and 1.0
is as fast as the system can manage.public void trace()
public void trace(int depth)
Call
button functionality.depth
- The current call stack depth.public void breakpoint()
public void delay()
public void registerSpeedBar(javax.swing.JSlider slider)
slider
- The slider which will serve as the speed bar for this animatorpublic void registerSpeedBar(javax.swing.JScrollBar scrollBar)
scrollBar
- The scroll bar which will serve as the speed bar for this
animatorpublic java.awt.Component getSpeedBar()
JSlider
or a JScrollBar
; it is the caller's
responsibility to cast the result to the appropriate class.null
if none existspublic void requestTermination()
Animator
that it should stop running at the next
available opportunity, which is when the client next calls pause
or checkForTermination
. Making this check at well-managed times
makes it possible for the client to ensure that objects are left in a
consistent state and thereby avoids the problems that led Sun to deprecate
Thread.stop
.public void checkForTermination()
Animator
has been asked to terminate.
If so, the Animator
stops running, and the call never returns.
If not, other threads are given a chance to run, after which this
Animator
will return to the caller.protected void delayHook()
protected void breakHook()
protected void resumeHook()
protected void controllerHook()
public void start()
start
in class java.lang.Thread