Class ThreadedAnimation
- java.lang.Object
-
- vmm3d.core.ThreadedAnimation
-
- All Implemented Interfaces:
- Animation
- Direct Known Subclasses:
- OsculatingCircleAnimation, ParallelCurveAnimation
public abstract class ThreadedAnimation extends java.lang.Object implements Animation
An animation that runs in its own thread, separate from the Swing user interface thread. Because Swing is not thread-safe and the animation runs in its own thread, it is not safe to call arbitrary Swing and org.freehep.graphics2d.VectorGraphics methods. (If it is necessary to call them, theinvokeAndWaitorinvokeLatermethods from classjavax.swing.SwingUtilitiescan be used.) However, callingrepaintis OK. Since setting parameter values and adding or removing decorations generate calls torepaint, they are also safe. Note that an animation does not start running automatically, but only when itsstart()method is called.To create a ThreadedAnimation, it is usually only necessary to create a subclass and implement
runAnimation(), the single abstract method defined in this class. This method is a script for the animation, which is run from beginning to end. When this method returns, the animation ends; if the method never returns, then the animation must be canceled by some external agent that calls itscancelmethod.The
runAnimationmethod can call thepause(int)method to insert a pause into the animation. Behind the scenes, this method also checks to see whether the animation has been canceled. If so, it throws an exception that aborts the animation. To avoid delays between the time when the animation'scancelmethod is called and tha time when the animation actually stops, it is important thatpausebe called regularly. If no delay is desired, pause can be called with a parameter of zero.Some animations will have some "clean-up" to do when the animation ends, whether it ends because the
runAnimationmethod returns or because it has been canceled. to make sure that the clean-up is done in all cases, it is advisable to do the clean-up in afinallyclause in therunAnimationmethod. For a simple example of this, seeOsculatingCircleAnimation.runAnimation().A ThreadedAnimation emits ChangeEvents when it is started and when it ends. A ChangeListener can tell which event generated the ChangeEvent by calling the
isRunningmethod of the animation. Note that in some circumstances, an alternative method for doing set-up and clean-up for the animation is to install a ChangeListener that does the set-up/clean-up in response to ChangeEvents.
-
-
Constructor Summary
Constructors Constructor and Description ThreadedAnimation()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidaddChangeListener(javax.swing.event.ChangeListener listener)Add a ChangeListener to this animation.voidcancel()Cancels the animation.java.lang.StringgetStatusText(boolean running)Returns null to indicate that the default text ("Animation Running" or "Animation Paused" in the English version) should be shown in the display's status bar.doublegetTimeDilation()Returns the time dilation factor that is currently set for this animation.booleanisPaused()Tests whether the animation is paused.booleanisRunning()Tests whether the animation is running.voidremoveChangeListener(javax.swing.event.ChangeListener listener)Remove a ChangeListener from the animation, if it is currently registered as a listener.voidsetPaused(boolean paused)Pauses or unpauses a running animation.voidsetTimeDilation(double dilationFactor)Slows down or speeds up the animation by multiplying all delay times (as specified in the parameter thepausemethod) by a time dilation factor.voidstart()This method must be called to start the animation running.booleanwasCanceled()Tests whether the animation has been canceled.
-
-
-
Method Detail
-
start
public void start()
This method must be called to start the animation running. An animation does not start automatically, but only when this method is called. If the animation is already running, this has no effect. If the animation has already run and ended, this will restart thea animation from the beginning. The animation will run until therunAnimationends or until the animation is canceled.
-
setPaused
public void setPaused(boolean paused)
Pauses or unpauses a running animation. If the animation is not running, this has no effect.
-
isPaused
public boolean isPaused()
Tests whether the animation is paused. Only a running animation can be paused.
-
cancel
public void cancel()
Cancels the animation. The animation will be stopped as soon as possible (that is, the next time thepause(int)method is called byrunAnimation()). If the animation is not running, this has no effect.
-
isRunning
public boolean isRunning()
Tests whether the animation is running.
-
wasCanceled
public boolean wasCanceled()
Tests whether the animation has been canceled. This can be called after the animation ends to determine whether the animation ended on its own or because it was canceled.
-
setTimeDilation
public void setTimeDilation(double dilationFactor)
Slows down or speeds up the animation by multiplying all delay times (as specified in the parameter thepausemethod) by a time dilation factor. Note that the dilation applies only to delay times, not to processing times, so the animation speed is only approximately multiplied by the dilation factor. In particular, only a limited amout of speed-up can be obtained, no matter how close to zero you make the dilation factor. The default value of the time dilation factor is 1, which corresponds to normal run speed.- Specified by:
setTimeDilationin interfaceAnimation- Parameters:
dilationFactor- delay times for thepausemethod are multiplied by this factor to give the actual time delay. A dilationFactor less than zero is treated as zero.- See Also:
pause(int)
-
getTimeDilation
public double getTimeDilation()
Returns the time dilation factor that is currently set for this animation.- Specified by:
getTimeDilationin interfaceAnimation- See Also:
setTimeDilation(double)
-
addChangeListener
public void addChangeListener(javax.swing.event.ChangeListener listener)
Add a ChangeListener to this animation. Change events are sent when the animation stops and when it stops for any reason. The ChangeListener can distinguish the two events by calling theisRunningmethod, which will retrun true if the animation is starting and false if the animation has ended.- Specified by:
addChangeListenerin interfaceAnimation- Parameters:
listener- a ChangeListener that is registered to receive change events from this animation.
-
removeChangeListener
public void removeChangeListener(javax.swing.event.ChangeListener listener)
Remove a ChangeListener from the animation, if it is currently registered as a listener.- Specified by:
removeChangeListenerin interfaceAnimation- Parameters:
listener- to be de-registered as a ChangeListener from this animation.
-
getStatusText
public java.lang.String getStatusText(boolean running)
Returns null to indicate that the default text ("Animation Running" or "Animation Paused" in the English version) should be shown in the display's status bar. This can be overridden in a subclass to show a different status message.- Specified by:
getStatusTextin interfaceAnimation- Parameters:
running- tells whether the animation is currently running.- Returns:
- the text to displayed, or null to use the default text "Animation Running" or "Animation Paused"
-
-
DMelt 3.0 © DataMelt by jWork.ORG