Computer Science 252
Problem Solving with Java
Spring 2014, The College of Saint Rose
FallingBallButtons Demo
A working demo of FallingBallButtons will appear below. Click inside the applet to interact with it.
FallingBallButtons BlueJ Project
Click here to download a BlueJ project for FallingBallButtons.
FallingBallButtons Source Code
The Java source code for FallingBallButtons is below. Click on a file name to download it.
import objectdraw.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; /* * Example FallingBallButtons: adding Swing JButtons to the * falling ball. * * Jim Teresco, The College of Saint Rose, Fall 2013 * Based on example from Williams College CS 134. * * $Id: FallingBallButtons.java 2221 2013-10-22 02:48:35Z terescoj $ */ // First, note above the extra import statements for additional Java API // classes and interfaces we are using here. // Now, notice that we have "implements ActionListener" specified on our // WindowController class. More on that below. public class FallingBallButtons extends WindowController implements ActionListener { private static final int SLOW_SPEED = 2; private static final int MEDIUM_SPEED = 5; private static final int FAST_SPEED = 10; // JButtons to create falling balls of different speeds private JButton slowButton, mediumButton, fastButton; public void begin() { // making the canvas extra big since we're placing some big // buttons along the sides and bottom soon. setSize(600, 600); // First, we need to ask our WindowController class for // the container that holds all of its GUI components. // Until now, we have had just a DrawingCanvas, referred // to as canvas. But we'll be adding more to it. Container contentPane = getContentPane(); // next, we'll create some buttons and add them to our // content pane. Note that JButton constructor just takes // one parameter - the text to be displayed within the // button. We then add each to the content pane in one // of the four cardinal directions. // slow button in the west (left) slowButton = new JButton("Drop Slow Ball"); contentPane.add(slowButton, BorderLayout.WEST); // medium button in the south (bottom) mediumButton = new JButton("Drop Medium Ball"); contentPane.add(mediumButton, BorderLayout.SOUTH); // fast button in the east (right) fastButton = new JButton("Drop Fast Ball"); contentPane.add(fastButton, BorderLayout.EAST); // now, if we want our program to react when someone pushes // one of these buttons, we need to tell it which class // has an actionPerformed method to be called when that button // is pressed. We use "this" to indicate our current class, // as it has such an actionPerformed method, which we indicate // to Java by implementing the ActionListener interface in the // class header. slowButton.addActionListener(this); mediumButton.addActionListener(this); fastButton.addActionListener(this); // put at end of all methods that change the layout contentPane.validate(); } // This is the actionPerformed method, which we promised would be // implemented in this class when we specified "implements // ActionListener" in the class header. This method will be called // each time one of our three buttons is pressed. It is an event // handler just like our onMouse* methods, but takes a different // parameter: an ActionEvent object that tells us information about // the event that triggered this method call. public void actionPerformed(ActionEvent e) { double speed; // Since this same actionPerformed method would be called // for a click on any of our three buttons, we need to check // which one it was by comparing the return of our ActionEvent's // getSource method with the JButton references we remembered // when we created them in the begin method. if (e.getSource() == slowButton) { speed = SLOW_SPEED; } else if (e.getSource() == mediumButton) { speed = MEDIUM_SPEED; } else { speed = FAST_SPEED; } new Ball(new Location(200, 0), 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(); } }