CS 1 Laboratory 7
Follow the Bouncing Ball
Due: 5 PM Friday, November 1

Objective: To gain experience using AWT components and listeners.

The Scenario.

In this lab, we would like you to get practice in the use of AWT components. As a result we would like you to create a program which includes two Scrollbars, a Label, a pop-up menu (Choice), and a Button.

A running version of the program we hope you produce is provided below (if your browser handles applets correctly):



As usual, the main part of the screen is the canvas. The canvas contains a bouncing ball. (The starter contains the code to bounce the ball, similar to code you have seen in the past.) When the program begins, a ball will start bouncing around the screen. The user uses the various AWT components to change the appearance of the ball. The changes should take effect immediately.

At the "North" side of the screen is a Label identifying the program as "Bouncy Ball". At the "East" side is a slider (Scrollbar) which controls the vertical velocity of the ball. At the "South" side of the screen is a panel (Panel) which holds a slider (Scrollbar) which controls the horizontal velocity, a pop-up menu (Choice) which allows the user to change the ball's color, and a Button, which is used to show/hide the ball. If the Button is pressed, the ball should be hidden and the button's label should be changed to "Show". Pressing it again should show the bouncing ball and change the label back to "Hide". The ball should continue to move while it is hidden and you can change the ball's properties while it is hidden.

Getting Started

1. First you need to locate a free computer in either Wright 112 or 339. Create a new folder "lab7" on the " Student Z: " drive in the folder "cs061" which you created in lab 1.

2. Start Internet Explorer by Start | Programs | Internet | Internet Explorer and go to http://www.cs.uni.edu/~fienup/cs061f02/labs/lab7/.

3. Download each of the files "objectdraw.jar", "lab_7.gpj", "BouncyBall.java," and "BallController.java" by right-clicking on the name of each file and selecting "Save Target As". Save these files to your newly created "Z:\cs061\lab7" folder.

4. Start jGrasp by double-clicking on the lab_7.gpj project file that you downloaded to your new lab7 folder. By using this project file you sets up the correct Compiler and run-time settings for the objectdraw package.

5. Click on the "Project" tab at the bottom of the upper-left-hand sub-window of jGrasp. Double click the "BouncyBall.java," and "BallController.java" files. Enlarge the "BallController.java" window.

6. When compiling, always compile from the "BallController.java" window. It will compile all of the .java files.

How to Proceed

First a brief AWT review: To display a component, you must create the component and add it to the display. To accept input, you must attach a listener to the component, declare that the listening class implements the listener interface, and define the listening method required by that interface.

Before starting, be sure that you have your GUI cheat sheet in front of you (you can find it at http://www.cs.uni.edu/~fienup/cs061f02/handouts). The project contains 2 classes: BallController.java and BouncyBall.java. BallController is the class to which you will add the AWT components. BouncyBall.java contains the class that implements the bouncing ball. We have already provided the begin and run methods to get the ball to bounce. You will need to add methods that allow its behavior to change as the components are used.

Note that the statements import java.awt.*; and import java.awt.event.*; appear at the top of our starter .java file. These lines inform Java that your program will need access to the Java libraries that support GUI components and events (including event listeners).

To complete your implementation, proceed as follows:

  1. Include code in your begin method to add a label (Label) centered at the "NORTH" end of the program window. The label should say "Bouncy Ball". To set the font of the label, you need to create a new font using:
  2.     Font myFont = new Font (String fontName, int style, int size);

    The style can be Font.BOLD, Font.ITALIC, Font.PLAIN, or Font.BOLD + Font.ITALIC. The font used in the demo is "Arial". You can find the list of font names by going to jGrasp Compile | Computer Settings | Global menu and selecting the Font tab. In the Font tab, uncheck the square next to Font and click on the down arrow in the Font menu to see the font names. Cancel and Discard the changes so you don't change the font of the editor.

    The Label component does not accept input so you do not need a listener.

  3. Now work on a component that accepts user input by adding the vertical speed slider to the "EAST" side of the canvas. The slider is a Scrollbar representing the range of vertical speeds in pixels per second. The demo ranges from 40 to 600 pixels/second with an initial value of 40 pixels/second.
  4. In order to allow your WindowController extension to "listen" to the Scrollbar, make sure that your class header ends with the phrase "implements AdjustmentListener" and that you send the message addAdjustmentListener(this) to your Scrollbar. Define the adjustmentValueChanged method so that every time the Scrollbar is "adjusted", the vertical speed of the ball changes.

    You also need to add a method setVerticalSpeed in the BouncyBall class to make the ball change. Note that BouncyBall expects speeds in pixels/millisecond, but the slider gives you speeds in pixels/second. Divide the slider value by 1000.0 (a double representing the number of milliseconds in a second) to convert the value inside setVerticalSpeed. Look at how the speed parameters are used in the BouncyBall constructor and mimic that code. Recall as well that you need to multiply the speed passed in by the length of the pause (PAUSETIME) to scale the speed appropriately.

    Finally, BouncyBall uses negative speeds to move up (and to move left). The slider always gives you a positive value in the range. Before changing the speed of the ball, remember to check if the ball currently has a negative speed. If it does, multiply the slider speed by -1 so the ball continues to move in the same direction, just at a different speed.

  5. Notice that there is more than one component below the canvas on the display. You need to create a Panel to hold the collection of components. The panel should have the horizontal slider in one row and the remaining components in a second row. To do this, you need to set the new panel's layout (using setLayout) to a GridLayout with 2 rows and 1 column.
  6. You will add the horizontal slider first to the new panel so that it occupies the top row. To allow the second row to contain more than one component, you need to create a panel for the second row. This panel can use the default FlowLayout manager.

  7. Add the horizontal speed slider to the "SOUTH" panel. Its implementation is quite similar to the vertical speed slider.
  8. Add the color menu to the panel created to contain the second row. The color menu is a Choice whose entries list color names. See the GUI cheat sheet for the constructor and the method used to add choices to the Choice item. Be sure to add to the class header that your program "implements" ItemListener. (When you have a class which implements several interfaces, you simply separate the interfaces with commas. Do NOT include the keyword implements more than once in the class header!) Also, don't forget to tell the Choice that your program will be the listener.
  9. Add code to the appropriate method to handle ItemEvents. You will need to add a setColor method to the BouncyBall class to change the ball's color.

  10. Next add the hide/show Button. When the user clicks on the button, the ball should hide and the label should change to "Show" using setLabel. The opposite behavior should occur if the user clicks again. You can find out what the current label is by calling getLabel on the button.

Once you have added all of the AWT features to your program, print a copy of the Applet as it runs.

Submitting Your Work

The submission procedure is basically the same as last week.

Hard-copies, output, and the floppy disk should be turned into the instructor inside the large vanilla envelope. Labs are due at 5 PM on Friday, November 1, for all sections.