MathBits.com
Return to Unit Menu | Java Main Page | MathBits.com | Terms of Use  | Java Resource CD


 
Animation

In order to produce the effect of simple animation, we must be able to control the time that a figure remains on the screen and be able to erase a figure.

To PAUSE the screen display:
     Pausing the screen display can be accomplished with the use of the following method:

public static void pause (int time)
{
     try                           
//opens an exception handling statement
     {
          Thread.sleep(time);
     }
     catch(InterruptedException e) 
//captures the exception
     {
     }
}

                         Thread.sleep(5000) will sleep for approximately 5 seconds.

Alternate coding:
public static void sleep( long
millis, int nanos ) throws InterruptedException

Parameters: millis - the length of time to sleep in milliseconds.
nanos -  0-999999 additional nanoseconds to sleep.
Description: Causes the currently executing thread to sleep (cease execution) for the specified number of milliseconds plus the specified number of nanoseconds.

To ERASE a figure:
     To erase a figure, simply draw the figure on top of itself in the background color.

 

Formula Process for Animation

1.  Draw the figure to be animated in its starting position.

2.  Determine the smallest x (or y) value contained in the drawing.

3.  Rewrite the "formula" to create the figure by substituting x (or y) for the smallest value and adjusting the subsequent values.

4.  Determine the x (or y) coordinate of the last position of the figure after its movement.

5.  Determine if the object will move one or more pixels at a time.

6.  Establish a for loop (using the variable x (or y) to place the figure in its various locations during movement.

7.  Inside of the loop, draw the figure in its original color by setting COLOR.  Determine the length of the "pause" that will show the figure before it moves to its next location.  Change the COLOR to match the background and draw the figure again to erase it.

Figure in Starting Position: Figure Written with Formula:

Record the coordinates of the figure in its starting position before the animation begins. This arrow is going to move horizontally to the right.  The smallest x-value is 10.  The number 10 was replaced with x, and all other x coordinates were written in terms of x.
It was decided for this arrow that the x-values would range from a starting value of 10 to and ending value of 110 with the figure being redrawn every 5 pixels. 
                             for (int i = 20; i <= 110, i = i + 5)

 


Return to Unit Menu | Java Main Page | MathBits.com | Terms of Use  | Java Resource CD