
/*
   "Bouncer" is a simple animation applet that shows a red
   circle bouncing around in a box.

   David Eck, 26 September 1998
*/


import java.awt.*;

public class Bouncer extends SimpleAnimationApplet {

   double redCenterX, redCenterY;  // Position of the center of the
                                   // red circle.

   double redVelocityX, redVelocityY;  // The amount by which the
                                       // position of the red circle
                                       // changes from one frame to the next.

   boolean starting = true;  // This variable is used in drawFrame() to distinguish
                             // the first frame from all the other frames.  The value
                             // is changed from true to false when the first frame is
                             // drawn.



   protected void drawFrame(Graphics g, int width, int height) {
          // This routine is called when it is time to draw a frame.
          // of the animation.  It draws the current frame, and updates
          // the data in the member variables to get ready for the
          // next frame.

        g.setColor(Color.white);  // fill in the whole background in white
        g.fillRect(0,0,width,height);

        if (starting) {
           initializeRedCircle(width,height);  // This is the first frame, so
                                               //   call the initialization routine.
           starting = false;  // set starting = false, so this if statement
                              //   will only be executed in the first frame.
        }

        g.setColor(Color.red);
        g.fillOval((int)redCenterX - 25, (int)redCenterY - 25, 50, 50);  // draw the red circle

        updateRedCircle(width,height);  // Update red circle data to be ready for the next frame.

        g.setColor(Color.black);  // Draw a black border around the edges
        g.drawRect(0,0,width-1,height-1);

   } // end drawFrame()


   void initializeRedCircle(int width, int height) {
           // This is called before drawing the first frame.  Set the initial
           // position and velocity of the red circle so that it is in the
           // center of the applet with a random velocity.  When choosing
           // the random velocity, make sure that redVelocityX and
           // redVelocityY are both >= 3 in absolute value.  The
           // parameters width and height tell the size of the applet;
           // this information is necessary for telling if the circle
           // has hit the edge of the applet.

        redCenterX = width / 2;
        redCenterY = height / 2;

        do {
           redVelocityX = 12 * (Math.random() - 0.5);
        } while (Math.abs(redVelocityX) < 3);

        do {
           redVelocityY = 12 * (Math.random() - 0.5);
        } while (Math.abs(redVelocityY) < 3);

   } // end initializeRedCircle()


   void updateRedCircle(int width, int height) {
          // Update the position of the red circle by adding the
          // X and Y velocities to the X and Y positions.
          // If the circle hits one of the edges of the square,
          // change the velocity to make the circle move away
          // from the edge.  This makes the circle seem to bounce
          // off the edge.

       redCenterX += redVelocityX;
       redCenterY += redVelocityY;

       if (redCenterX - 25  <=  0)  // Circle has hit left edge.
          redVelocityX = Math.abs(redVelocityX);  // Make sure circle is moving right.

       if (redCenterX + 25  >=  width) // Circle has hit the right edge.
          redVelocityX = - Math.abs(redVelocityX); // Make sure circle is moving left.

       if (redCenterY - 25  <=  0)  // Circle has hit top edge.
          redVelocityY = Math.abs(redVelocityY);  // Make sure circle is moving down.

       if (redCenterY + 25  >=  height) // Circle has hit the bottom edge.
          redVelocityY = - Math.abs(redVelocityY); // Make sure circle is moving up.

   }


} // end class Bouncer

