Java Reference
In-Depth Information
startAnimation that will be executed in response to the button presses. The former
will simply need to stop the Timer object, but the latter will need to check whether
it is the fi rst time that the animation is being started. If it is, then the Timer object
will have to be created and started; if not, then the Timer method restart will have to
be called if the animation is not currently running. The code for these two methods
is shown below (and would be incorporated into bean AnimBean 3 ).
public void startAnimation()
{
//Check whether this is fi rst time during current
//run of program that animation is being run…
if (animTimer == null)
{
//First run of animation, so set current frame
//to fi rst one in sequence, create Timer and
//start Timer running…
currentImage = 0;
animTimer = new Timer(delay,this);
animTimer.start();
}
else
//Not fi rst time that animation is being run,
//so check that it is not still running…
if (!animTimer.isRunning())
//Not currently running, so safe to restart…
animTimer.restart();
}
public void stopAnimation()
{
animTimer.stop();
}
As well as adding these two methods, we shall need to replace lines
animTimer = new Timer(delay,this);
animTimer.start();
in the constructor with the following line:
startAnimation();
10.4
Using JavaBeans Within an Application
Once a bean has been created and compiled, we can use it as we would any GUI
component (though the program using it need not be a GUI). We should import the
bean class explicitly, of course.
Search WWH ::




Custom Search