Boxball.java

   import objectdraw.*;
   import java.awt.*;
   import java.awt.event.*;

   // Name: Mark Fienup
   // Date: 10/14/02
   // Description: Boxball using Buttons

   public class Boxball extends WindowController implements ItemListener {
      // Constants
      private static final int PLAY_AREA_WIDTH = 400;
      private static final int PLAY_AREA_HEIGHT = 400;
      private static final int APPLET_WIDTH = 500;
      private static final int APPLET_HEIGHT = 600;
      private static final int PLAY_AREA_TOP = 50;
      private static final int PLAY_AREA_LEFT = (APPLET_WIDTH - PLAY_AREA_WIDTH) / 2;
      private static final int BUTTON_TOP = PLAY_AREA_TOP + PLAY_AREA_HEIGHT + 50;
      private static final int BUTTON_WIDTH = 75;
      private static final int BUTTON_HEIGHT = 50;
      private static final int BUTTON_SPACING = 40;
      private static final int LEFT_BUTTONS_LEFT = (APPLET_WIDTH - 3*BUTTON_WIDTH
                        -2*BUTTON_SPACING) / 2;
   
      private static final int BALL_SIZE = 30;
   
      private static final double EASY_LINE_Y = PLAY_AREA_TOP + PLAY_AREA_HEIGHT * 0.75;
      private static final double MEDIUM_LINE_Y = PLAY_AREA_TOP + PLAY_AREA_HEIGHT * 0.5;
      private static final double HARD_LINE_Y = PLAY_AREA_TOP + PLAY_AREA_HEIGHT * 0.25;
   
      private static final double BOX_HEIGHT = BALL_SIZE*1.25;
      private static final double EASY_BOX_WIDTH = BALL_SIZE*2.0;
      private static final double MEDIUM_BOX_WIDTH = BALL_SIZE*1.5;
      private static final double HARD_BOX_WIDTH = BALL_SIZE*1.1;
   
      // Instance variables
      private FramedRect playArea;
      private FramedRect easyButton;
      private FramedRect mediumButton;
      private FramedRect hardButton;
      private FilledRect easyButtonBackground;
      private FilledRect mediumButtonBackground;
      private FilledRect hardButtonBackground;
      private Line startingLine;
      private double startingLineY;
      private Box box;   
      private Text message;
   
    // Choice object to select level
    private Choice level;
   
      public void begin() {
         Text title = new Text("Boxball", 0, 0, canvas);
         title.setFontSize(24);
         double titleWidth = title.getWidth();
         title.moveTo((APPLET_WIDTH - titleWidth)/2, 10);
         playArea = new FramedRect(PLAY_AREA_LEFT, PLAY_AREA_TOP,
                              PLAY_AREA_WIDTH, PLAY_AREA_HEIGHT, canvas);
      
        // creates the game-level buttons
        level = new Choice();

        level.add ("Easy");
        level.add ("Medium");
        level.add ("Hard");
        
        add (level, BorderLayout.NORTH);
            
        // Ask to be notified if the level menu is used
        level.addItemListener (this);

      
         // Display starting line from the easy level
         startingLine = new Line (PLAY_AREA_LEFT, EASY_LINE_Y, 
                              PLAY_AREA_LEFT + PLAY_AREA_WIDTH, EASY_LINE_Y, canvas);
         startingLineY = EASY_LINE_Y;
      
         // Create the box
         box = new Box(PLAY_AREA_LEFT, PLAY_AREA_LEFT + PLAY_AREA_WIDTH, 
                      PLAY_AREA_TOP + PLAY_AREA_HEIGHT - BOX_HEIGHT,
                      EASY_BOX_WIDTH, BOX_HEIGHT, canvas); 
      
         message = new Text(" ", PLAY_AREA_LEFT+PLAY_AREA_WIDTH/2,
                            PLAY_AREA_TOP+PLAY_AREA_HEIGHT+5, canvas);
         message.setFontSize(16);              
      } // end begin
      
      // Handles mouse click in playing area above the line by dropping a ball.
      // Clicks anywhere else are ignored.
      public void onMouseClick(Location point) {
           
         if (point.getY() < startingLineY && playArea.contains(point)) {
            new Ball(point.getX(), point.getY(), BALL_SIZE, PLAY_AREA_TOP+PLAY_AREA_HEIGHT - BALL_SIZE,
                    this, canvas);
         } // end if
      
      } // end onMouseClick
    // Handle changes in the Choice controlling the level
    public void itemStateChanged (ItemEvent event) {
        
        if (level.getSelectedItem().equals ("Easy")) {
            startingLine.setEndPoints(new Location(PLAY_AREA_LEFT, EASY_LINE_Y), 
                                 new Location(PLAY_AREA_LEFT + PLAY_AREA_WIDTH, EASY_LINE_Y));
            startingLineY = EASY_LINE_Y;
            box.setSize(EASY_BOX_WIDTH);
        }
        else if (level.getSelectedItem().equals ("Medium")) {
            startingLine.setEndPoints(new Location(PLAY_AREA_LEFT, MEDIUM_LINE_Y), 
                                 new Location(PLAY_AREA_LEFT + PLAY_AREA_WIDTH, MEDIUM_LINE_Y));   
            startingLineY = MEDIUM_LINE_Y;
            box.setSize(MEDIUM_BOX_WIDTH);
         }
        else if (level.getSelectedItem().equals ("Hard")) {
            startingLine.setEndPoints(new Location(PLAY_AREA_LEFT, HARD_LINE_Y), 
                                 new Location(PLAY_AREA_LEFT + PLAY_AREA_WIDTH, HARD_LINE_Y));
            startingLineY = HARD_LINE_Y;
            box.setSize(HARD_BOX_WIDTH);
        }
    }
   
      // Checks to see if ball landed in the box
      public void onBallHitBottom(double ballsX) {
         if (box.getLeft() < ballsX && ballsX+BALL_SIZE < box.getRight()) {
            message.setText("You got it in!");
            box.moveBox();
         } 
         else {
            message.setText("Missed try again!");
         } // end if
      } // end onBallHitBottom  
   } // end Boxball class