Computer Science 120
Introduction to Programming

Spring 2012, Siena College

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
 *
 * $Id: NudgeBall.java 1507 2011-01-27 02:20:59Z 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);
    }
}