BestBasketball


Your browser is ignoring the <APPLET> tag!

This program allows the user to drag a basketball. If the user drags the basketball into the hoop, "Yea" appears. If the user misses the hoop, "Boo" appears.

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

/** A silly basketball game.  Cheer when the user drags
 *  the basketball into the hoop.  Boo when the user drags
 *  the ball anyplace else.
 */
public class BestBasketBall extends WindowController{

    // Displays the score
    private Text cheerDisplay;

    // The basketball hoop
    private FramedOval hoop;

    // Number of pixels cheer should be from the bottom.
    private static final int BOTTOM_MARGIN = 50;
    
    // Number of pixels cheer should be from the top.
    private int cheerTop;
    
    // Size of the basketball hoop.
    private static final int HOOP_WIDTH = 100;
    private static final int HOOP_HEIGHT = HOOP_WIDTH * 6 / 10;
    
    // Number of pixels hoop should be from the top
    private static final int TOP_MARGIN = 50;
    
    // The basketball
    private FilledOval ball;
    
    // The ball's diameter
    private static final int BALL_SIZE = HOOP_WIDTH / 2;
    
    // Space between ball and score
    private static final int BALL_SPACING = BALL_SIZE + 30;
    
    // The ball's starting height
    private int ballTop;
    
    // Remembers if the user is dragging the ball
    private boolean ballGrabbed = false;
    
    // Location of the mouse at the beginning of the current drag
    private Location lastPoint;

    // initialize the counter and the text message
    public void begin()
    {
        // Create the scoreboard.  Initially place it at the left edge of the window.
        cheerTop = getHeight() - BOTTOM_MARGIN;
        cheerDisplay = new Text("", 0, cheerTop, canvas);
        
        // Use a large font.
        cheerDisplay.setFontSize(16);
        
        // Create the basketball hoop centered near the top of the window.
        hoop = new FramedOval( (canvas.getWidth()-HOOP_WIDTH)/2, TOP_MARGIN,
                                HOOP_WIDTH, HOOP_HEIGHT, 
                                canvas);
                                
        // Create the ball
        ballTop = cheerTop - BALL_SPACING;
        ball = new FilledOval ( (canvas.getWidth() - BALL_SIZE) / 2, ballTop, 
                                BALL_SIZE, BALL_SIZE, canvas);
        ball.setColor (Color.ORANGE);
    }
    
    // Grab the ball if the user presses the mouse button down while the mouse is over the ball.
    public void onMousePress (Location point) {
        if (ball.contains (point)) {
            ballGrabbed = true;
            lastPoint = point;
        }
    }
    
    // Move the ball as the user drags the mouse.
    public void onMouseDrag (Location point) {
        if (ballGrabbed) {
            // Figure out how far the mouse has moved.
            double deltaX = point.getX() - lastPoint.getX();
            double deltaY = point.getY() - lastPoint.getY();
        
            // Move the ball the same amount
            ball.move (deltaX, deltaY);
            
            // Remember this mouse location as the start for the next drag.
            lastPoint = point;
        }
    }
            
    // Score 2 points on each click.
    public void onMouseRelease(Location point)
    {
        if (ballGrabbed) {
            // The user has dropped the ball
            ballGrabbed = false;
            
            if (hoop.contains (point)) {
                // The user scored.
                cheerDisplay.setText("Yea!");
            }
            else {
                // The user did not score
                cheerDisplay.setText ("Boo!");
            }
        
            // Re-center the score.  Its width might change as the cheer changes
            cheerDisplay.moveTo( (canvas.getWidth()-cheerDisplay.getWidth())/2, cheerTop );
        }
        
    }
    
    // Move the ball back to its starting point when the mouse is moved.
    public void onMouseMove (Location point) {
        ball.moveTo ((canvas.getWidth() - BALL_SIZE) / 2, ballTop);
    }

}