Computer Science 202
Introduction to Programming
Fall 2013, The College of Saint Rose
BasketballName Demo
A working demo of BasketballName will appear below. Click inside the applet to interact with it.
BasketballName BlueJ Project
Click here to download a BlueJ project for BasketballName.
BasketballName Source Code
The Java source code for BasketballName is below. Click on a file name to download it.
import objectdraw.*; import java.awt.*; import javax.swing.JOptionPane; /* * A simple basketball program. It adds 2 points to * a displayed score whenever the ball is dragged into * the oval (hoop). * * This version personalizes the messages using the player's * name which is read in through a JOptionPane.showInputDialog. * * Jim Teresco, The College of Saint Rose, Fall 2013 * Based on example from Williams College, CSCI 134 * * $Id: BasketballName.java 2217 2013-10-18 14:00:31Z terescoj $ */ public class BasketballName 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; // player's name private String name; public void begin() { // Use a JOptionPane to prompt for the player's name name = JOptionPane.showInputDialog("Please enter your name"); // draw the court // create scoreboard, adjust font size, center it scoreboard = new Text("OK, " + name + ", 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("Nice shot, " + name + "! Your score is " + score); } else { scoreboard.setText("Oh, no, " + name + ", 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); // move hoop to new position in case canvas was resized hoop.moveTo((canvas.getWidth()- HOOP_WIDTH)/2, canvas.getHeight()*HOOP_FROM_TOP - HOOP_HEIGHT/2); } }