Computer Science 252
Problem Solving with Java
Spring 2014, The College of Saint Rose
Basketball Demo
A working demo of Basketball will appear below. Click inside the applet to interact with it.
Basketball BlueJ Project
Click here to download a BlueJ project for Basketball.
Basketball Source Code
The Java source code for Basketball is below. Click on a file name to download it.
import objectdraw.*; import java.awt.*; /* * A simple basketball program. It adds 2 points to * a displayed score whenever the ball is dragged into * the oval (hoop). * * Jim Teresco, Siena College, CSIS 120, Spring 2011 * The College of Saint Rose, Fall 2013 * Based on example from Williams College, CSCI 134 * * $Id: Basketball.java 2218 2013-10-18 14:06:39Z terescoj $ */ public class Basketball extends WindowController { // Location of the display private static final int DISPLAY_X = 80; private static final int DISPLAY_Y = 200; private static final int DISPLAY_SIZE = 16; // in points // Location and dimensions of the hoop private static final int HOOP_TOP = 50; private static final int HOOP_LEFT = 160; private static final int HOOP_WIDTH = 100; private static final int HOOP_HEIGHT = 60; // Initial location and dimensions of the ball private static final int BALL_X = 190; private static final int BALL_Y = 300; private static final int BALL_SIZE = 40; private static final Color BALL_COLOR = new Color(250, 115, 10); // instance variables // court elements private FramedOval hoop; private FilledOval ball; private Text scoreboard; // keep track of whether we are dragging the ball private boolean ballGrabbed; // last mouse position for dragging private Location lastMouse; // current score private int score; public void begin() { // draw the court // create scoreboard scoreboard = new Text("Drag the ball to the hoop to score points!", DISPLAY_X, DISPLAY_Y, canvas); // draw the hoop hoop = new FramedOval(HOOP_LEFT, HOOP_TOP, HOOP_WIDTH, HOOP_HEIGHT, canvas); // draw the ball ball = new FilledOval(BALL_X, BALL_Y, BALL_SIZE, BALL_SIZE, canvas); ball.setColor(BALL_COLOR); // initialize score to 0 score = 0; } public void onMousePress(Location point) { // check if we pressed the mouse on the ball if (ball.contains(point)) { // if so, set up for dragging the ball ballGrabbed = true; lastMouse = point; } } public void onMouseDrag(Location point) { // check if we are dragging the ball if (ballGrabbed) { // if so, update position to current ball.move(point.getX() - lastMouse.getX(), point.getY() - lastMouse.getY()); lastMouse = point; } } public void onMouseRelease(Location point) { // check if we are dragging the ball if (ballGrabbed) { // if so, date position to current ball.move(point.getX() - lastMouse.getX(), point.getY() - lastMouse.getY()); // check if the ball is in the hoop if (hoop.contains(point)) { score = score + 2; scoreboard.setText("Basket! Your score is " + score); } else { scoreboard.setText("You missed! Your score is " + score); } ballGrabbed = false; ball.moveTo(BALL_X, BALL_Y); } } }