
/* 
   This file defines an applet that shows bouncing balls
   moving in a big black rectangle, bouncing off the edges.
   There are some buttons and a check box for the user
   to click to affect the animation.

   MORE IMPORTANT, this applet is meant as an example of
   a simple, but useful layout technique.  It shows how
   to lay out an applet with a large component occupying
   most of the applet, with a line of buttons and/or other
   smaller components below it.  This basic technique is
   sufficient for a lot of applets.
*/

import java.awt.*;
import java.applet.*;

public class BouncingBallsApplet extends Applet {

   final static int borders = 3;  // Number of pixels to leave around
                                  // and between components.  (The applet's
                                  // background color shows through.)

   // --- Declare the compoents that will appear in the applet ---

   Button fasterButton;       // a button to make the balls move faster
   Button slowerButton;       // a button to make the balls move slower
   Checkbox runningCheckbox;  // a button to turn the motion on and off

   BBCanvas canvas;  // The large "canvas" that displays the moving balls.
                     // Note: most of the actual programming for the applet
                     // is in the BBCanvas class.  This applet class simply
                     // sets up the applet layout, and then responds to
                     // events by calling methods in the canvas object.


   public void init() {  // The init() method is called by the system when
                         // the applet is first created.  It is used here
                         // to set up the applet's layout and appearance


      setBackground(Color.blue);  // Background color will show through
                                  // in the borders around and between
                                  // the components.


      // First, set up the applet's layout.  Specify a "BorderLayout", and
      // add the canvas in the "Center".  A BorderLayout can also have
      // components at the "North", "South", "East", and "West" edges of
      // the applet.  Here, a Panel is created and added at the "South" edge.
      // This Panel is used as a container for the other components of the
      // applet.  I've also set a different background color in the Panel.
      // Note that the two parameters to the BorderLayout constructor
      // specify space to be left between components in the layout.

      setLayout( new BorderLayout(borders,borders) );

      canvas = new BBCanvas();
      add("Center",canvas);

      Panel bottom = new Panel();
      bottom.setBackground( new Color(180,180,255) ); // Light blue.
      add("South",bottom);

      
      // Now, create the small components and add them to the bottom Panel,
      // so that they will line up along the bottom of the applet.

      fasterButton = new Button("Faster");
      bottom.add(fasterButton);

      slowerButton = new Button("Slower");
      bottom.add(slowerButton);

      runningCheckbox = new Checkbox("Running");
      runningCheckbox.setState(true);  // The box should be checked at the start.
      bottom.add(runningCheckbox);     //        (The default state is unchecked.)

   } // end init()


   public Insets insets() {
         // This method specifies how much space is to be left around the
         // edges of the applet, outside the components that it contains.
         // There should be no need to change this.
      return new Insets(borders,borders,borders,borders);
   }


   public void start() {
         // Called by the system when the applet is started;
         // just call the canvas' startCanvas() method.
      canvas.startCanvas();
   }


   public void stop() {
         // Called by the system when the applet is about to be stopped;
         // just call the canvas' stopCanvas() method.
      canvas.stopCanvas();
   }


   public void destroy() {
         // Called by the system when the applet is about to be destroyed;
         // just call the canvas' destroyCanvas() method.
      canvas.destroyCanvas();
   }


   public boolean action(Event evt, Object arg) {

          // The action method is called by the system when the user
          // performs certain types of actions, such as clicking on
          // a button or checkbox.  The evt parameter contains information
          // about the event that the user generated.  In particular,
          // evt.target tells from which component the event originated.
          // Often this is all you need to know to respond to the
          // event.  Here, I respond to clicks on the buttons or
          // checkbox in the applet by calling methods in the canvas
          // object.

      if (evt.target == fasterButton) {  // user clicked on fasterButton
         canvas.faster();  // Tell canvas that the balls should move faster.
         return true;
      }
      else if (evt.target == slowerButton) {  // user clicked on slowerButton
         canvas.slower();  // Tell canvas that the balls should move slower.
         return true;
      }
      else if (evt.target == runningCheckbox) {  // user clicked on the checkbox
            // runningBox.getState() is used to determine whether or not the
            // checkbox is checked.  This is a boolean value, which is used
            // here as a parameter to canvas.setRunning().  This effectively
            // tells the canvas whether or not to let the animation run.
         canvas.setRunning( runningCheckbox.getState() );
         return true;
      }
      else
         return super.action(evt,arg);  // let the superclass handle the event
   } // end action()


} // end of class Bouncing Balls Applet