Computer Science 120
Introduction to Programming
Fall 2011, Siena College
Basketball2 Demo
A working demo of Basketball2 will appear below. Click inside the applet to interact with it.
Basketball2 BlueJ Project
Click here to download a BlueJ project for Basketball2.
Basketball2 Source Code
The Java source code for Basketball2 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, Fall 2011 * Based on example from Williams College, CSCI 134 * * $Id: Basketball2.java 1516 2011-01-31 21:08:19Z terescoj $ */ public class Basketball2 extends WindowController { // Location of the display private static final double DISPLAY_FROM_TOP = 0.5; // display is half way down private static final int DISPLAY_SIZE = 16; // in points // Location and dimensions of the hoop private static final double HOOP_FROM_TOP = 0.25; private static final int HOOP_WIDTH = 100; private static final int HOOP_HEIGHT = 60; // Initial location and dimensions of the ball private static final double BALL_FROM_TOP = 0.75; 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, adjust font size, center it scoreboard = new Text("Drag the ball to the hoop to score points!", 0, DISPLAY_FROM_TOP * canvas.getHeight(), canvas); scoreboard.setFontSize(DISPLAY_SIZE); scoreboard.moveTo(canvas.getWidth()/2 - scoreboard.getWidth()/2, DISPLAY_FROM_TOP * canvas.getHeight() - scoreboard.getHeight()/2); // draw the hoop hoop = new FramedOval(canvas.getWidth()/2 - HOOP_WIDTH/2, canvas.getHeight() * HOOP_FROM_TOP - HOOP_HEIGHT/2, HOOP_WIDTH, HOOP_HEIGHT, canvas); // draw the ball ball = new FilledOval(canvas.getWidth()/2 - BALL_SIZE/2, canvas.getHeight() * BALL_FROM_TOP - BALL_SIZE/2, 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); } scoreboard.moveTo(canvas.getWidth()/2 - scoreboard.getWidth()/2, DISPLAY_FROM_TOP * canvas.getHeight() - scoreboard.getHeight()/2); } ballGrabbed = false; ball.moveTo(canvas.getWidth()/2 - BALL_SIZE/2, canvas.getHeight() * BALL_FROM_TOP - BALL_SIZE/2); } }