
/* 
   This applet is an animation showing a set of moving, nested
   rectangles that seem to move perpetually towards the center
   of the applet. The rectangles are drawn in black on a red background.
   This applet depends on SimpleAnimationApplet.java.
   
   David Eck
   July 1998
*/

import java.awt.*;

public class MovingRects extends SimpleAnimationApplet {


   public void init() {
         // initialize the applet by setting up the animation to have
         // 15 frames.
      setFrameCount(15);
   }
   
   
   protected void drawFrame(Graphics g, int width, int height) {

         // Draw one frame in the animation by filling in the background
         // with a solid red and then drawing a set of nested black
         // rectangles.  The frame number tells how much the first 
         // rectangle is to be inset from the borders of the applet.

      g.setColor(Color.red);           // fill the frame with red
      g.fillRect(0,0,width,height);
      
      g.setColor(Color.black);         // switch color to black

      int inset = getFrameNumber();    // Gap between borders of applet and
                                       // outermost rect.  This goes from 0 to
                                       // 14 then back to 0, and so on.
                                       
      int rectWidth = width - 2*inset - 1;    // size of the rect
      int rectHeight = height - 2*inset - 1;
      
      while (rectWidth >= 0 && rectHeight >= 0) {
         g.drawRect(inset,inset,rectWidth,rectHeight);
         inset += 15;       // rects are 15 pixels apart
         rectWidth -= 30;   // width decreases by 15 pixels on left and 15 on right
         rectHeight -= 30;  // height decreases by 15 pixels on top and 15 on bottom
      }
      
   }  // end drawFrame()

}  // end class MovingRects
