Spawn an animation thread
This is where this program really departs from the previous programs. Listing 11 instantiates an object of an inner Thread class named Animate and saves that object's reference in a local variable named animate . Then Listing 11 uses that variable to call the start method on the Animate object.
I won't attempt to go into all of the technical details involved in this operation. Suffice it to say that this eventually causes a method named run belonging to the Animate object to be executed.
The Animate class and the run method
The Animate class and the run method begin in Listing 12 .
Listing 12 . The inner Thread class named Animate. |
---|
class Animate extends Thread{
public void run(){//Compute the incremental distances that the
// geometric object will move during each iteration.double xInc = vector.getData(0)/100;
double yInc = vector.getData(1)/100; |
The run method begins by computing incremental x and y displacement values that will be required to move the geometric object fromits initial position as shown in Figure 4 to its final position as shown in Figure 5 in 100 equal steps.
Do the animated move
Then Listing 13 executes 100 iterations of a for loop to cause the geometric object to actually move through those 100 incremental steps and to bedrawn on the screen once during each incremental step.
Listing 13 . Do the animated move. |
---|
for(int cnt = 0;cnt<100;cnt++){
vector.setData(0,cnt*xInc);vector.setData(1,cnt*yInc);//Draw a new off-screen image based on the
// incremental displacement vector and other user// inputs.
drawOffScreen(g2D);//Copy off-screen image to canvas.
myCanvas.repaint();//Sleep for ten milliseconds.try{
Thread.currentThread().sleep(10);}catch(InterruptedException ex){
ex.printStackTrace();}//end catch
}//end for loop}//end run
}//end inner class named Animate |
During each iteration of the for loop...
During each iteration of the for loop in Listing 13 , an incremental displacement vector is created with X and Y component values that are equal to1/100 of the user-specified displacement vector multiplied by the loop counter value. The incremental displacement vector is used by the code in the drawOffScreen method to translate the geometric object to a new location on the off-screen image. Then the repaint method is called on the canvas to cause the off-screen image to be copied onto the canvas as described inconjunction with Listing 10 earlier.
(Note that the object is translated from its original location, not its current location, during each iteration of the for loop. A different approach would be to save the current location and translate it fromthe current location during each iteration of the for loop.)
At the end of each iteration of the for loop, the animation thread sleeps for ten milliseconds before starting the next iteration.
The bottom line is that this code causes the geometric object to move in incremental steps from its initial location to a new location based on a displacement vector specified by the user. In other words,the geometric object reaches its final location in a straight-line animated manner, taking 100 steps to get there.