AnimationTest.java


Below is the syntax highlighted version of AnimationTest.java from § Standard Draw 3D.


/******************************************************************************
 *  Compilation:  javac AnimationTest.java
 *  Execution:    java AnimationTest
 *
 * Animates a red sphere to move in a circle using simple geometry.
 * 
 * Keywords: Animation
 ******************************************************************************/

public class AnimationTest {
    public static void main(String[] args) {
        
        // Sets the scale
        StdDraw3D.setScale(-1, 1);
        
        // Radius of the sphere
        double radius = 0.2; 
        
        // Radius of the circle of rotation
        double R = 0.8; 
        
        // Angle of the sphere on the circle
        double angle = 0; 
        
        // Amount of milliseconds per frame
        int time = 20;
        
        // The animation loop
        while(true) {
            
            // Calculates the new position using geometry
            double x = R * Math.cos(angle);
            double y = R * Math.sin(angle);
            double z = 0;
            
            // Increments the angle
            angle += 0.02;
            
            // Clears the screen
            StdDraw3D.clear();
            
            // Draws the shape
            StdDraw3D.setPenColor(StdDraw3D.RED);
            StdDraw3D.sphere(x, y, z, radius);
            
            // Renders the 3D scene
            StdDraw3D.show(time);
        }
    }
}


Copyright © 2000–2011, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 25 14:46:50 EDT 2016.