Computer Science 252
Problem Solving with Java
Spring 2016, The College of Saint Rose
BallBounceAround BlueJ Project
Click here to download a BlueJ project for BallBounceAround.
BallBounceAround Source Code
The Java source code for BallBounceAround is below. Click on a file name to download it.
import objectdraw.*;
import java.awt.*;
/*
* Example BallBounceAround: demonstration of the effect of
* gravity on a bouncing ball
*
* Jim Teresco, The College of Saint Rose, CSC 252, Spring 2016
*
* $Id: template.java 2329 2014-02-27 02:46:26Z terescoj $
*/
public class BallBounceAround extends WindowController {
public void begin() {
new Text("Press to launch bouncy balls!", 0, 0, canvas);
}
public void onMousePress(Location point) {
// each time the mouse is pressed on the canvas, a new
// bouncy ball appears at that location
new BouncyBall(point, canvas);
}
}
import objectdraw.*;
import java.awt.*;
import java.util.Random;
/**
*
* Example BallBounceAround: the bouncy ball that bounces all around the
* canvas. The ball starts being stationary in y at the given point and is
* subject to gravity. It is given a random x speed.
*
* Jim Teresco, The College of Saint Rose, CSC 252, Spring 2016
*/
public class BouncyBall extends ActiveObject {
// the size of the ball
private static final double SIZE = 20;
// range of speeds in the x direction
private static final double MAX_X_SPEED = 10;
// animation delay time
private static final int PAUSE_TIME = 33;
// amount to add to ySpeed each frame to simulate gravity
private static final double GRAVITY = 0.1;
// we'll create a single instance of Random to be shared among
// all instances of BouncyBall
private static Random r = new Random();
// standard instance variables to remember the canvas
// and the ball itself
private DrawingCanvas canvas;
private FilledOval ball;
public BouncyBall(Location point, DrawingCanvas c) {
// remember the canvas, which we'll need down in run
canvas = c;
// the ball, centered at the specified point, making sure we're not
// creating partially off the canvas
double xPos = point.getX() - SIZE/2;
double yPos = point.getY() - SIZE/2;
if (xPos > canvas.getWidth() - SIZE) {
xPos = canvas.getWidth() - SIZE;
}
if (yPos > canvas.getHeight() - SIZE) {
yPos = canvas.getHeight() - SIZE;
}
ball = new FilledOval(xPos, yPos, SIZE, SIZE, c);
// start it up!
start();
}
public void run() {
// choose a random x speed
double xSpeed = r.nextDouble() * 2 * MAX_X_SPEED - MAX_X_SPEED;
// and start y at 0, and let gravity do its thing
double ySpeed = 0.0;
// the ball bounces forever!
while (true) {
// move
ball.move(xSpeed, ySpeed);
// check for bouncing off the floor, but only reverse if still going down to
// avoid getting "stuck" along the floor
if ((ball.getY() + SIZE >= canvas.getHeight()) && (ySpeed > 0)) {
ball.moveTo(ball.getX(), canvas.getHeight() - SIZE);
ySpeed = -ySpeed;
}
// and check for bouncing off the side walls
if ((ball.getX() <= 0) && (xSpeed < 0)) {
xSpeed = -xSpeed;
}
if ((ball.getX() + SIZE >= canvas.getWidth()) && (xSpeed > 0)) {
xSpeed = -xSpeed;
}
// acceleration due to gravity
ySpeed += GRAVITY;
// color code the ball based on its y speed: color component is 20*speed.
// shades of red for down, blue for up
Color newColor;
if (ySpeed > 0) {
int red = (int)(20 * ySpeed);
if (red > 255) red = 255;
newColor = new Color(red, 0, 0);
}
else {
int blue = (int)(-20 * ySpeed);
if (blue > 255) blue = 255;
newColor = new Color(0, 0, blue);
}
ball.setColor(newColor);
pause(PAUSE_TIME);
}
}
}