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. // Extra points are awarded for shots made in less than a second. public class TimedBasketballGame extends FrameWindowController { // Location of the display private static final int TEXT_X = 140; private static final int TEXT_Y = 300; private static final int TIME_TEXT_Y = 325; 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; // the last previous known location of the mouse private Location lastMouse; // the basketball oval private Basketball ball; // black frame around the ball private FramedOval border; // display of how long it took to make the shot private Text timeDisplay; // stopwatch to measure how long a shot took private Stopwatch timer; // initialize the counter and the text message public void begin() { timer = new Stopwatch(); score = 0; ballGrabbed = false; display = new Text("Your score is 0", TEXT_X, TEXT_Y, canvas); display.setFontSize(DISPLAYSIZE); timeDisplay = new Text("Take a shot!", TEXT_X, TIME_TEXT_Y, canvas); timeDisplay.setFontSize(DISPLAYSIZE); hoop = new FramedOval(HOOP_X, HOOP_Y, HOOP_WIDTH, HOOP_HEIGHT, canvas); ball = new Basketball(BALL_X, BALL_Y, BALL_SIZE, canvas); } public void onMousePress(Location point) { ballGrabbed = ball.contains(point); lastMouse = point; if (ballGrabbed) { timer.reset(); } } // Move the ball public void onMouseDrag(Location point) { if (ballGrabbed) { ball.move(point.getX() - lastMouse.getX(), point.getY() - lastMouse.getY()); lastMouse = point; } } // increment the counter and update the text public void onMouseRelease(Location point) { double shotTime; // local, not instance variable if (ballGrabbed && hoop.contains(point)) { shotTime = timer.getElapsedMilliseconds(); if (shotTime < 3000) { score = score + 5; } else { score = score + 2; } timeDisplay.setText("Your shot took " + shotTime + " milliseconds"); display.setText("Your score is " + score ); } ball.returnToStart(); } }