Computer Science 252
Problem Solving with Java

Spring 2014, The College of Saint Rose

FallingBallComboReact Demo

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



FallingBallComboReact BlueJ Project

Click here to download a BlueJ project for FallingBallComboReact.


FallingBallComboReact Source Code

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


FallingBallComboReact.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 FallingBallComboReact: create a falling ball whose speed
 * depends on the value of a JComboBox, but also have the most recently
 * created ball change its speed when the menu selection changes.
 *
 * Jim Teresco, The College of Saint Rose, CSC 252, Fall 2013
 *
 * $Id: FallingBallComboReact.java 2227 2013-10-24 03:37:54Z terescoj $
 */

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

    // the ball, so we can tell it to change speeds
    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 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);
        speedMenu.addActionListener(this);

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

    // actionPerformed this time needs to differentiate between
    // an event triggered by a button press vs. one triggered by
    // the combo box selection changing
    public void actionPerformed(ActionEvent e) {

        // first, we do some things that have to happen
        // regardless of why we were called
        
        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;
        }
        
        // now why are we here?
        if (e.getSource() == speedMenu) {
            // menu item changed - change the speed of the last ball
            // if we have one
            if (lastLaunch != null) {
                lastLaunch.setSpeed(speed);
            }
        }
        else {
            // we must have pressed the button for a new ball, so
            // let's create one.
            lastLaunch = new Ball(new Location(200, 0), speed, canvas);
        }
    }
}