Computer Science 252
Problem Solving with Java
Spring 2015, The College of Saint Rose
FallingBallSlider Demo
A working demo of FallingBallSlider will appear below. Click inside the applet to interact with it.
FallingBallSlider BlueJ Project
Click here to download a BlueJ project for FallingBallSlider.
FallingBallSlider Source Code
The Java source code for FallingBallSlider 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.JSlider; import javax.swing.JPanel; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; /* * Example FallingBallSlider: a slider for finer control over * ball speeds. * * Jim Teresco, The College of Saint Rose, CSC 252, Fall 2013 * * $Id: FallingBallSlider.java 2227 2013-10-24 03:37:54Z terescoj $ */ // Note here that our class serves as both an ActionListener and // a ChangeListener, so we specify that it implements both, and // separate by commas. A class can implement as many interfaces // as we wish, so long as we provide all of the required methods // for each interface. public class FallingBallSlider extends WindowController implements ActionListener, ChangeListener { private static final int MIN_SPEED = 2; private static final int MAX_SPEED = 10; // using a JSlider this time private JSlider speedControl; // the ball, so we can set its speed private Ball lastLaunch; public void begin() { setSize(500, 500); // We still need our content pane Container contentPane = getContentPane(); // A JPanel, in which we will group together our JButton // and JComboBox JPanel northPanel = new JPanel(); // Next, we'll create just one button and add it to our // JPanel. JButton createBallButton = new JButton("Drop Ball"); northPanel.add(createBallButton); // construct a JSlider: // the first parameter is orientation, we choose horizontal // the remaining parameters are the values associated with // the minimum, maximum, and initial values of the slider speedControl = new JSlider(JSlider.HORIZONTAL, MIN_SPEED, MAX_SPEED, MIN_SPEED); // and put our JSlider into our JPanel northPanel.add(speedControl); // now, we add the JPanel (which contains our JButton // and JPanel) to the content pane, this time in the // north, why not? contentPane.add(northPanel, BorderLayout.NORTH); // add our action listener for the button createBallButton.addActionListener(this); // and our change listener for the slider speedControl.addChangeListener(this); // put at end of all methods that change the layout contentPane.validate(); } // actionPerformed this time only deals with creating a new // ball, but it needs to query the current slider state // to decide on the ball's initial speed. public void actionPerformed(ActionEvent e) { // get the speed from the JSlider double speed = speedControl.getValue(); // go ahead and create the new ball lastLaunch = new Ball(new Location(200, 0), speed, canvas); } // our slider generates ChangeEvents, delivered to // the stateChanged method, as required by the ChangeListener // interface public void stateChanged(ChangeEvent e) { // if we've launched a ball, set its speed. if (lastLaunch != null) { lastLaunch.setSpeed(speedControl.getValue()); } } }
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 2227 2013-10-24 03:37:54Z 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(); } // this program requires that our ball's speed can change public void setSpeed(double newSpeed) { ySpeed = newSpeed; } }