Boxball.java
import objectdraw.*;
import java.awt.*;
import java.awt.event.*;
// Name: Mark Fienup
// Date: 10/23/02
// Description: Boxball using Stacked Buttons
public class Boxball extends WindowController implements ActionListener {
// Constants
private static final int PLAY_AREA_WIDTH = 400;
private static final int PLAY_AREA_HEIGHT = 400;
private static final int APPLET_WIDTH = 550;
private static final int APPLET_HEIGHT = 550;
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 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 Line startingLine;
private double startingLineY;
private Box box;
private Text message;
// Buttons for level selection
private Button easy, medium, hard;
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 a Panel and stacks the difficulty-level buttons
Panel buttonPanel = new Panel ();
buttonPanel.setLayout(new GridLayout(3,1));
easy=new Button ("Easy");
medium=new Button ("Medium");
hard=new Button ("Hard");
buttonPanel.add (easy);
buttonPanel.add (medium);
buttonPanel.add (hard);
add (buttonPanel, BorderLayout.EAST);
// request notification if buttons are clicked
easy.addActionListener (this);
medium.addActionListener (this);
hard.addActionListener (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);
validate(); // Helps AWT Components appear at startup
} // 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 button presses
public void actionPerformed (ActionEvent event) {
if (event.getSource() == 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 (event.getSource() == 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 (event.getSource() == 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);
}
} // end actionPerformed
// 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