Computer Science 252
Problem Solving with Java

Spring 2014, The College of Saint Rose

DrawBBalls Demo

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



DrawBBalls BlueJ Project

Click here to download a BlueJ project for DrawBBalls.


DrawBBalls Source Code

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


DrawBBalls.java

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

/*
 * Example DrawBBalls: draw basketballs on mouse dragging
 *
 * Jim Teresco, Siena College, CSIS 120, Spring 2011
 * The College of Saint Rose, Fall 2013
 *
 * $Id: DrawBBalls.java 2218 2013-10-18 14:06:39Z terescoj $
 */

public class DrawBBalls extends WindowController {

    public static final double BBALL_SIZE = 50;

    // draw a nice basketball centered at the mouse point on each drag
    public void onMouseDrag(Location point) {

        new NiceBBall(point.getX() - BBALL_SIZE/2,
            point.getY() - BBALL_SIZE/2,
            BBALL_SIZE, canvas);
    }

    // clean up when we re-enter the window  
    public void onMouseEnter(Location point) {

        canvas.clear();
    }
}

NiceBBall.java

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

/*
 * A class that implements a nice looking basketball.
 *
 * Jim Teresco, Siena College, CSIS 120, Spring 2011
 * The College of Saint Rose, Fall 2013
 * Based on example from Williams College, CSCI 134
 *
 * $Id: NiceBBall.java 2218 2013-10-18 14:06:39Z terescoj $
 */

// Hey, look, no "extends WindowController"!
// That's because this class will not create a canvas or
// respond to any mouse events -- all of that it the
// responsibility of whoever creates a NiceBBall and
// wants to manipulate it.
public class NiceBBall {

    // the color to draw the ball
    private static final Color BALL_ORANGE = new Color(250, 85, 10);

    // size and starting angles for cut arc in degrees
    private static final int CUTSIZE = 100;
    private static final int RIGHTCUTSTART = 90 + (180 - CUTSIZE) / 2;
    private static final int LEFTCUTSTART = 270 + (180 - CUTSIZE) / 2;

    // the orange part of the ball
    private FilledOval body;

    // the border of the ball
    private FramedOval border;

    // the two curves on the sides of the ball
    private FramedArc leftCut, rightCut;

    // the vertical and horizontal lines through the ball
    private Line vert, horiz;

    // to pick new random color for ball
    private Random colorPicker = new Random();

    /*
     * Create a new basketball on the given DrawingCanvas.  Note how this class
     * does not know about the default "canvas", since that's available only in
     * classes that extend the WindowController class.
     */
    public NiceBBall(double left, double top, double size, DrawingCanvas aCanvas) {

        // draw the circles that make it look like a ball
        body = new FilledOval(left, top, size, size, aCanvas);
        body.setColor(BALL_ORANGE);
        border = new FramedOval(left, top, size, size, aCanvas);

        // draw the lines and arcs that make it look like a basketball
        rightCut = new FramedArc(left + size * 2 / 3, top, size, size, RIGHTCUTSTART, CUTSIZE, aCanvas);
        leftCut = new FramedArc(left - size * 2 / 3, top, size, size, LEFTCUTSTART, CUTSIZE, aCanvas);
        vert = new Line(left + size / 2, top, left + size / 2, top + size, aCanvas);
        horiz = new Line(left, top + size / 2, left + size, top + size / 2, aCanvas);

    }

    /*
     * Move the ball by specified offsets.
     */
    public void move(double dx, double dy) {
        // move each part of the ball by the offsets provided
        body.move(dx, dy);
        border.move(dx, dy);
        vert.move(dx, dy);
        horiz.move(dx, dy);
        leftCut.move(dx, dy);
        rightCut.move(dx, dy);
    }

    /*
     * Check to see if the ball contains a specified location.
     */
    public boolean contains(Location point) {
        return body.contains(point);
    }

    /*
     * Move the ball to a specified position. 
     */
    public void moveTo(double x, double y) {
        // determine how far away (x,y) is
        double dx, dy;
        dx = x - body.getX();
        dy = y - body.getY();

        // move each part of the ball by the offset,
        //   using our own move method.
        this.move(dx, dy);
    }

    /*
     * Return the x coordinate of ball's corner.
     */
    public double getX() {
        return body.getX();
    }

    /*
     * Return the y coordinate of ball's corner.
     */
    public double getY() {
        return body.getY();
    }

    /*
     *  method to change the background color of the ball to one
     *  selected completely at random.
     */
    public void changeToRandomColor() {
        Color newBallColor = new Color(colorPicker.nextInt(256),
                colorPicker.nextInt(256), 
                colorPicker.nextInt(256));
        body.setColor(newBallColor);
    }

    /*
     * method to return the ball back to its original color.
     */
    public void backToNormalColor() {

        body.setColor(BALL_ORANGE);
    }
}