We create a timer and when we need the animation, the timer is started, and the a block of code for animation executes which changes the look and position of object on the timer interval. After the animation is completed, we can reset the look or position of the object. The stopping condition of animation would be when timer is stopped or when some required state is reached by the object through animation.
We create a class for animation which does the task of animation as follows:
private class Animator implements ActionListener{
public void actionPerformed(ActionEvent e){//Do Animation}
}
And inside the object, we created a timer as follows:
private Timer animationTimer=new Timer(delay,new Animator());
//To start animation
animationTimer.start();
//To Stop animation
animationTimer.stop();
Thus, the value of delay can be changed according to our requirement what type of animation we are needing. The animator class should be accessible to the object to be animated to get state of the object. Suppose we want to create an animation with changing the position of an object, then the animator class should get the current position of the object, calculates the new position and sets the new position as current position. This process is repeated with some delay defined in timer, thus gives the feeling of smoothly moving object.
No comments:
Post a Comment