
/**
 *  This applet shows an animation of a square that
 *  shrinks down to a line.  Then the line rotates
 *  about one of its endpoints.  Finally, the line
 *  grows back into a square.  This cycle then repeats.
 *  It is assumed that the applet is 200-by-200 pixels.
 */

import java.awt.*;

public class Anim1 extends SimpleAnimationApplet {

   /**
    *   The init() method is called when the applet first
    *   starts.  Here, I use it to set some values that
    *   affect the animation.  The frame count is set to
    *   85.  This means that the frame numbers will be
    *   0,1,...,84, and then back to 0.  Setting the
    *   millisecondsPerFrame to 30 means that the computer
    *   will try to display a frame every 30 milliseconds.
    */
   public void init() {
      setFrameCount(85);
      setMillisecondsPerFrame(30);
   }
   

   /**
    *  The drawFrame() method is called when one frame of
    *  the animation needs to be drawn.  The function
    *  getFrameNumber() is called to find out the frame
    *  number of the frame that needs to be drawn.
    *  Depending on the frame number, this draw either
    *  a rectangle or a line.  It is assumed that the frame
    *  is 200 pixels wide and 200 pixels high.
    */
   public void drawFrame(Graphics g) {
   
      int frame;   // The number of the frame that is drawn.
      int width;   // The width of the rectangle that is drawn.
      int height;  // The height of the rectangle that is drawn.
      int i;       // A loop control variable for the for loop.

      // First, I fill the entire picture with a magenta-colored
      // rectangle.  This is not done automatically!

      g.setColor(Color.magenta);
      g.fillRect(0,0,200,200);

      // Draw a 5-pixel wide border around the edge of the
      // applet.  This is done by drawing 5 rectangles of
      // decreasing size.

      g.setColor(Color.black);
      for (i = 0; i < 5; i++)
         g.drawRect(i, i, 200 - 2*i -1, 200 - 2*i - 1);
      
      // Get the frame number of the frame that has to be drawn.
      // This will be a number in the range 0 to 84, inclusive.

      frame = getFrameNumber();
      
      // Now, draw the frame.  There are three cases:  For a frame
      // number in the range 0-29, a cyan-colored rectangle is drawn
      // that shrinks vertically from one frame to the next.
      // For a frame between 30 and 55, a line is shown that 
      // gradually rotates from horizontal to vertical.  For a
      // frame number between 56 and 85, a cyan rectangle is again
      // drawn that grows horizontally from one frame to the next.
      
      if (frame < 30) {
           // Draw a cyan-colored rectangle, with a black border
           // to make it look nicer.  The height of the rectangle
           // depends on the frame number.  The smaller the frame
           // number, the smaller the height.  For frame 0, the
           // height is 150.  For frame 29, the height is 5.  (In
           // frame 30, the height would be 0 if we used this code.)
         width = 150;
         height = 150 - 5*frame;
         g.setColor(Color.cyan);
         g.fillRect(25,25,width,height);
         g.setColor(Color.black);
         g.drawRect(25,25,width-1,height-1);
      }
      else if (frame <= 55) {
            // Draw a line from the point (25,25) to a point on the
            // circle of radius 150 that has (25,25) as its center.
            // The coordinates of the other point depend on the
            // frame number.  The higher the frame number, the bigger
            // the angle that the line makes with the horizontal.
            // The calculation uses some trigonometry and the
            // mathematical constant Math.PI.  Math.PI/2 is the 
            // angular measure of a quarter-circle.  It is divided
            // by 25 because there are 25 frames.
         frame = frame - 30; // Adjust the frame number to be
                             // between 0 and 25 instead of 30 and 55.
         double x,y;
         x = 150*Math.cos( frame*((Math.PI/2)/25) );
         y = 150*Math.sin( frame*((Math.PI/2)/25) );
         g.drawLine(25, 25, 25 + (int)x, 25 + (int)y);
      }
      else { 
            // The frame number is between 56 and 84.
            // Once again, a cyan rectangle is drawn.  This time,
            // the bigger the frame number, the wider the rectangle.
         frame = frame - 55; // Adjust the frame number to be
                             // between 1 and 29 instead of 56 and 84.
         width = 5*frame;
         height = 150;
         g.setColor(Color.cyan);
         g.fillRect(25,25,width,height);
         g.setColor(Color.black);
         g.drawRect(25,25,width-1,height-1);
      }
      
   } // end drawFrame();
   

} // end class Anim1
