Computer Science 252
Problem Solving with Java

Spring 2014, The College of Saint Rose

FallingBallComboButton Demo

A working demo of FallingBallComboButton will appear below. Click inside the applet to interact with it.



FallingBallComboButton BlueJ Project

Click here to download a BlueJ project for FallingBallComboButton.


FallingBallComboButton Source Code

The Java source code for FallingBallComboButton is below. Click on a file name to download it.


FallingBallComboButton.java

import objectdraw.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;

/*
 * Example FallingBallComboButton: just one Swing JButton
 * to create a ball, but select speed based on the contents
 * of a JComboBox.
 *
 * Jim Teresco, The College of Saint Rose, Fall 2013
 * Based on example from Williams College CS 134.
 *
 * $Id: FallingBallComboButton.java 2221 2013-10-22 02:48:35Z terescoj $
 */

public class FallingBallComboButton 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;

    // we'll be using a JComboBox in begin and in
    // actionPerformed, so we need to remember it.
    private JComboBox speedMenu;
    
    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 southPanel = new JPanel();

        // Next, we'll create just one button and add it to our
        // JPanel.
        JButton createBallButton = new JButton("Drop Ball");
        southPanel.add(createBallButton);

        // construct a JComboBox
        speedMenu = new JComboBox();
        // and some menu entries therein
        speedMenu.addItem("Slow");
        speedMenu.addItem("Medium");
        speedMenu.addItem("Fast");

        // which one is selected when we start?
        speedMenu.setSelectedItem("Medium");
        
        // and put our JComboBox into our JPanel
        southPanel.add(speedMenu);

        // now, we add the JPanel (which contains our JButton
        // and JComboBox) to the content pane, in the south
        contentPane.add(southPanel, BorderLayout.SOUTH);

        // add our action listener
        createBallButton.addActionListener(this);

        // put at end of all methods that change the layout
        contentPane.validate();
    }

    // actionPerformed method now doesn't need to check
    // which button was pressed - there's just the one.
    // But it does need to check the currently selected
    // menu item
    public void actionPerformed(ActionEvent e) {

        double speed;
        // this will return the selected item from the JComboBox
        Object selectedSpeed = speedMenu.getSelectedItem();

        // now we see which one it is by comparing to the Strings
        // we used when we added the menu items above.
        if (selectedSpeed.equals("Slow")) {
            speed = SLOW_SPEED;
        } 
        else if (selectedSpeed.equals("Medium")) {
            speed = MEDIUM_SPEED;
        } 
        else {  // "Fast" is the only other possibility
            speed = FAST_SPEED;
        }
        new Ball(new Location(200, 0), speed, canvas);
    }
}

Ball.java

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();
    }
}