Computer Science 252
Problem Solving with Java
Spring 2016, The College of Saint Rose
FallingBallUglyButtons BlueJ Project
Click here to download a BlueJ project for FallingBallUglyButtons.
FallingBallUglyButtons Source Code
The Java source code for FallingBallUglyButtons is below. Click on a file name to download it.
import objectdraw.*; import java.awt.*; /* * Example FallingBallUglyButtons: the way NOT to make buttons * * Jim Teresco, The College of Saint Rose, Fall 2013, Spring 2016 * * $Id: FallingBallUglyButtons.java 2221 2013-10-22 02:48:35Z terescoj $ */ public class FallingBallUglyButtons extends WindowController { private static final int SLOW_SPEED = 2; private static final int MEDIUM_SPEED = 5; private static final int FAST_SPEED = 10; // three difficulty buttons private FramedRect slow, medium, fast; // constants for the difficulty box sizes and left corners private final static int BUTTON_WIDTH = 100; private final static int BUTTON_HEIGHT = 50; private final static int BUTTON_LEFT_Y = 400; private final static int SLOW_LEFT_X = 75; private final static int MEDIUM_LEFT_X = 200; private final static int FAST_LEFT_X = 325; // location of text box and indent distances within box private final static int TEXT_BOX_X = 230; private final static int TEXT_BOX_Y = 450; private final static int TEXT_INDENT_LEFT = 30; private final static int TEXT_INDENT_DOWN = 10; public void begin() { setSize(500,500); new FilledRect(SLOW_LEFT_X, BUTTON_LEFT_Y, BUTTON_WIDTH, BUTTON_HEIGHT, canvas).setColor(Color.lightGray); slow = new FramedRect(SLOW_LEFT_X, BUTTON_LEFT_Y, BUTTON_WIDTH, BUTTON_HEIGHT, canvas); new Text("Slow", SLOW_LEFT_X + TEXT_INDENT_LEFT, BUTTON_LEFT_Y + TEXT_INDENT_DOWN, canvas).setFontSize(24); new FilledRect(MEDIUM_LEFT_X, BUTTON_LEFT_Y, BUTTON_WIDTH, BUTTON_HEIGHT, canvas).setColor(Color.lightGray); medium = new FramedRect(MEDIUM_LEFT_X, BUTTON_LEFT_Y, BUTTON_WIDTH, BUTTON_HEIGHT, canvas); new Text("Med.", MEDIUM_LEFT_X + TEXT_INDENT_LEFT, BUTTON_LEFT_Y + TEXT_INDENT_DOWN, canvas).setFontSize(24); new FilledRect(FAST_LEFT_X, BUTTON_LEFT_Y, BUTTON_WIDTH, BUTTON_HEIGHT, canvas).setColor(Color.lightGray); fast = new FramedRect(FAST_LEFT_X, BUTTON_LEFT_Y, BUTTON_WIDTH, BUTTON_HEIGHT, canvas); new Text("Fast", FAST_LEFT_X + TEXT_INDENT_LEFT, BUTTON_LEFT_Y + TEXT_INDENT_DOWN, canvas).setFontSize(24); } public void onMousePress(Location point) { if (slow.contains(point)) { new Ball(new Location(200, 0), SLOW_SPEED, canvas); } else if (medium.contains(point)) { new Ball(new Location(200, 0), MEDIUM_SPEED, canvas); } else if (fast.contains(point)) { new Ball(new Location(200, 0), FAST_SPEED, canvas); } } }
import objectdraw.*; import java.awt.*; /* /* * Example FallingBallUglyButtons: * A Ball that will drop to the bottom of the canvas. * * Jim Teresco, The College of Saint Rose, Fall 2013 * * $Id: Ball.java 2221 2013-10-22 02:48:35Z terescoj $ */ public class Ball extends ActiveObject { private static final int BALL_SIZE = 30; private static final int PAUSE_TIME = 30; // The representation of the ball on the screen private FilledOval ball; // The bottom of the playing area private int bottomHeight; private double ySpeed; // Construct a new Ball with the given starting location and y velocity. public Ball(Location point, double speed, DrawingCanvas aCanvas) { ball = new FilledOval(point, BALL_SIZE, BALL_SIZE, aCanvas); ySpeed = speed; bottomHeight = aCanvas.getHeight(); start(); } public void run() { while (ball.getY() < bottomHeight) { ball.move(0, ySpeed); pause(PAUSE_TIME); } ball.removeFromCanvas(); } }