import objectdraw.*; import java.awt.Color; // a better basketball simulator public class SimpleBasketballGame 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 ball itself private Basketball ball; // the number of points private int score = 0; private Location lastPoint; private boolean ballGrabbed; // 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 Basketball(BALL_X, BALL_Y, BALL_SIZE, canvas); } public void onMouseDrag(Location point) { if (ballGrabbed) { ball.move(point.getX() - lastPoint.getX(), point.getY() - lastPoint.getY()); lastPoint = point; } } public void onMousePress(Location point) { lastPoint = point; ballGrabbed = ball.contains(point); } public void onMouseRelease(Location point) { if (ballGrabbed) { System.out.println("Ball position: x = " + ball.getX() + ", y = " + ball.getY()); if (hoop.contains(ball.getCenter())) { score = score + 2; display.setText("You have scored " + score + " points."); } else { display.setText("Sorry, you missed!"); } } ball.moveTo(BALL_X, BALL_Y); } }