Computer Science 252
Problem Solving with Java

Spring 2014, The College of Saint Rose

NudgeBall Demo

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



NudgeBall BlueJ Project

Click here to download a BlueJ project for NudgeBall.


NudgeBall Source Code

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


NudgeBall.java

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

/*
 * Example NudgeBall: draw a ball on the screen and move it
 * to the right each time the mouse is clicked on the ball.
 *
 * Jim Teresco, Siena College, CSCI 120, Spring 2011
 * The College of Saint Rose, Fall 2013
 *
 * $Id: NudgeBall.java 2218 2013-10-18 14:06:39Z terescoj $
 */

public class NudgeBall extends WindowController {

    // a constant defining the size of the ball
    private static final int BALL_DIAMETER = 50;
    // a constant defining the initial location of the ball
    private static final Location BALL_POSITION = new Location(100, 100);
    // a constant defining how far to move the ball when clicked
    private static final int BALL_MOVE = 10;

    // the ball we will be moving
    private FilledOval ball;

    public void begin() {

        ball = new FilledOval(BALL_POSITION, 
            BALL_DIAMETER, BALL_DIAMETER,
            canvas);
    }

    /* Move the ball to the right if we clicked on it */
    public void onMouseClick(Location point) {

        if (ball.contains(point))
            ball.move(BALL_MOVE, 0);
    }
}