Computer Science 252
Problem Solving with Java

Spring 2015, The College of Saint Rose

BasketballShots Demo

A working demo of BasketballShots will appear below. Click inside the applet to interact with it.



BasketballShots BlueJ Project

Click here to download a BlueJ project for BasketballShots.


BasketballShots Source Code

The Java source code for BasketballShots is below. Click on a file name to download it.


BasketballShots.java

import objectdraw.*;
import java.awt.*;

/*
 * Example BasketballShots: another example of a basketball game
 * but this time with more of a "shot" idea.  The user places the
 * ball with a press, drags to determine a shot vector, and releases
 * to get the ball to the hoop.  If the ball lands in the hoop,
 * points are awarded.
 *
 * Jim Teresco, The College of Saint Rose, CSC 252, Spring 2015
 *
 * $Id: template.java 2329 2014-02-27 02:46:26Z terescoj $
 */

public class BasketballShots 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 = 12; // 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;

    // Dimensions, position, and color 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);

    // 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;
    
    // start mouse position for calculating shot
    private Location pressPoint;

    // current score
    private int score;

    public void begin() {
        // draw the court
        // create scoreboard, adjust font size, center it
        scoreboard = new Text("Press to place the ball, and drag toward 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);

        // score starts at 0, of course
        score = 0;
    }
    
    public void onMousePress(Location point) {
    
        // move the ball to the press point, and remember it
        pressPoint = point;
        lastMouse = point;
        ball.moveTo(point);
        ball.move(-BALL_SIZE/2, -BALL_SIZE/2);
    }
    
    public void onMouseDrag(Location point) {
    
        // dragging is unconditional here -- if we pressed
        // we have a ball to be dragging
        ball.move(point.getX() - lastMouse.getX(),
                  point.getY() - lastMouse.getY());
        lastMouse = point;
    }

    public void onMouseRelease(Location point) {
    
        // here, we use the original press point and
        // the current release point to compute a 
        // trajectory for the ball, launch it, then see
        // if it lands in the hoop to get points.
        
        // we'll move to complete the drag, plus the amount
        // we moved from the original press point
        ball.move(point.getX() - lastMouse.getX(),
                  point.getY() - lastMouse.getY());
        ball.move(point.getX() - pressPoint.getX(),
                  point.getY() - pressPoint.getY());
                  
        // now see if we scored!
        // we'll be more careful here -- we compute the point at
        // the center of the ball
        Location landing = ball.getLocation();
        // that's the upper left, adjust down and to the
        // right to get the center
        landing.translate(BALL_SIZE/2, BALL_SIZE/2);
        
        // now we can check if we are in the hoop
        if (hoop.contains(landing)) {
            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);
    }
}