
/*
   This file defines a applet that shows a red square moving
   from left to right.  When it hits the right end of the
   applet, a "boink" sound is played and the square bounces
   back to the beginning.  The square moves on a light
   yellow background with a dark brown border.  It is
   assumed that the width of the applet is substantially
   move than its height.

   This file depends on SimpleAnimationApplet.class, which
   must be in the same directory as the BackAndForth applet.
   The BackAndForth applet also depends on the file boink.au,
   which contains the boink sound.

   David Eck, 19 September 1998
*/


import java.awt.*;

public class BackAndForth extends SimpleAnimationApplet {

   public void init() {
         // This is called once to initialize the applet.
         // Set the number of frames in the animation to
         // be 50.
      setFrameCount(50);
   }

   protected void drawFrame(Graphics g, int width, int height) {
         // This is called every time the frame needs to be
         // drawn.  The graphics context, g, can be used to
         // draw the frame.  The other two parameters specify
         // the size of the frame.  In this program, the frame
         // shows a light yellow background with a dark brown
         // border.  A red sqaure is drawn in a position that
         // depends on the frame number of the particular frame
         // that is being drawn.

      g.setColor(new Color(100,90,30));  // Fill the whole framw with dark brown.
      g.fillRect(0,0,width,height);

      g.setColor(new Color(255,255,200)); // Fill in the middle with light yellow,
      g.fillRect(5,5,width-10,height-10); //   leaving a 5-pixel border of dark brown.


      int size = height - 10;  // Size of the moving square, which is equal to the
                               //   height of the yellow interior of the applet,
                               //   which is the same as the height of the whole
                               //   applet minus 10 pixels of brown border.

      int range = width - 10 - size;   // Range of values through which
                                       //   the left end of the square moves;
                                       //   same as the width of the applet, minus
                                       //   10 pixels of border, minus the width
                                       //   of the square itself.

      int frameNum = getFrameNumber(); // Number of the frame that is
                                       //   being drawn, from 0 to 49.

      int left = 5 + (int)(range * (frameNum / 49.0)); // position of left edge of square,
                                                       //   which depends on the frame number.

      g.setColor(Color.red);         // Draw the red square.
      g.fillRect(left,5,size,size);

      if (frameNum == 49) // Frame 49 is the last, so the square hits the right edge.
        play(getDocumentBase(),"boink.au");

   } // end drawFrame()



} // end class BackAndForth
