import objectdraw.*; // a basketball simulator that allows dragging the ball to the hoop public class BestBasketball extends FrameWindowController { // placement of the text display private static final int TEXT_X = 100; private static final int TEXT_Y = 300; private static final int TEXT_SIZE = 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; // the number of points private int score = 0; // the ball itself private FilledOval ball; // Last position of mouse while dragging private Location lastPoint; // remembers whether the ball was touched when the button was pressed private boolean ballGrabbed = false; // initialize the counter and the text message public void begin() { display = new Text("Take a shot.", TEXT_X, TEXT_Y, canvas); display.setFontSize(TEXT_SIZE); hoop = new FramedOval(HOOP_X, HOOP_Y, HOOP_WIDTH, HOOP_HEIGHT, canvas); ball = new FilledOval(BALL_X, BALL_Y, BALL_SIZE, BALL_SIZE, canvas); } public void onMousePress(Location point){ lastPoint = point; ballGrabbed = ball.contains(point); } // Move the basketball as the mouse is dragged public void onMouseDrag(Location point){ if (ballGrabbed) { ball.move(point.getX() - lastPoint.getX(), point.getY() - lastPoint.getY()); lastPoint = point; } } // increment the counter and update the text if player scores public void onMouseRelease(Location point) { if (ballGrabbed && hoop.contains(point)) { score = score + 2; display.setText("You have scored " + score + " points."); } else { display.setText("Sorry, you missed!"); } ball.moveTo(BALL_X, BALL_Y); } }