Computer Science 252
Problem Solving with Java
Fall 2013, The College of Saint Rose
FallingBallButtonsPanel Demo
A working demo of FallingBallButtonsPanel will appear below. Click inside the applet to interact with it.
FallingBallButtonsPanel BlueJ Project
Click here to download a BlueJ project for FallingBallButtonsPanel.
FallingBallButtonsPanel Source Code
The Java source code for FallingBallButtonsPanel 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; import javax.swing.JPanel; /* * Example FallingBallButtons: adding Swing JButtons to the * falling ball, but in a JPanel all at the bottom of the window * * Jim Teresco, The College of Saint Rose, Fall 2013 * Based on example from Williams College CS 134. * * $Id: FallingBallButtonsPanel.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 FallingBallButtonsPanel 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() { // the canvas doesn't have to be quite as big here. setSize(500, 500); // We still need our content pane Container contentPane = getContentPane(); // But now we'll create a JPanel, which we will then use // to group together our JButtons. JPanel southPanel = new JPanel(); // next, we'll create some buttons and add them to our // JPanel instead of to the content pane. Note that // we do not tell them to go into a particular place // in the JPanel, just add them and let the JPanel // choose a layout in this case. slowButton = new JButton("Drop Slow Ball"); southPanel.add(slowButton); mediumButton = new JButton("Drop Medium Ball"); southPanel.add(mediumButton); // fast button in the east (right) fastButton = new JButton("Drop Fast Ball"); southPanel.add(fastButton); // now, we add the JPanel (which contains our JButtons) // to the content pane, in the south contentPane.add(southPanel, BorderLayout.SOUTH); // add our action listeners as before slowButton.addActionListener(this); mediumButton.addActionListener(this); fastButton.addActionListener(this); // put at end of all methods that change the layout contentPane.validate(); } // Same actionPerformed method as before. public void actionPerformed(ActionEvent e) { double speed; 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(); } }