import objectdraw.*; import java.awt.Color; // This program allows the user to drag a simple basketball around the screen. // Points are awarded when the ball is placed in the basket. public class TimedBasketballGame extends FrameWindowController { // Location of the display private static final int TEXT_X = 140; private static final int TEXT_Y = 320; private static final int DISPLAYSIZE = 16; // placement and sizing of the hoop private static final int HOOP_X = 150; private static final int HOOP_Y = 60; private static final int HOOP_WIDTH = 100; private static final int HOOP_HEIGHT = 60; // placement and sizing of the ball private static final int BALL_X = 180; private static final int BALL_Y = 230; private static final int BALL_SIZE = 35; // the Text object which displays the count private Text display; // the oval that represent the hoop private FramedOval hoop; // whether or not player grabbed the ball with the mouse private boolean ballGrabbed; // the number of points private int score = 0; // the last previous known location of the mouse private Location lastMouse; // the basketball object private FancyBasketball ball; // display of how long it took to make the shot private Text timeDisplay; // the stopwatch to time shots private Stopwatch timer; // initialize the counter and the text message public void begin() { timer = new Stopwatch(); display = new Text("Your score is 0", TEXT_X, TEXT_Y, canvas); display.setFontSize(DISPLAYSIZE); timeDisplay = new Text("Take a shot!", TEXT_X, TEXT_Y - 25, canvas); timeDisplay.setFontSize(DISPLAYSIZE); hoop = new FramedOval(HOOP_X, HOOP_Y, HOOP_WIDTH, HOOP_HEIGHT, canvas); ball = new FancyBasketball(BALL_X, BALL_Y, BALL_SIZE, canvas); } public void onMousePress(Location point) { ballGrabbed = ball.contains(point); lastMouse = point; } // Move the ball public void onMouseDrag(Location point) { if (ballGrabbed) { ball.move(point.getX() - lastMouse.getX(), point.getY() - lastMouse.getY()); lastMouse = point; } } public void onMouseRelease(Location point) { if (ballGrabbed && hoop.contains(point)) { // local variables! int shotPoints; double shotTime = timer.read(); if (shotTime < 2000) { // should use a constant here! shotPoints = 5; } else { shotPoints = 2; } score = score + shotPoints; timeDisplay.setText(shotPoints + " points (" + shotTime + " ms)!"); display.setText("Your score is " + score ); timer.reset(); } ball.moveTo(BALL_X, BALL_Y); } }