Objective: To gain experience using AWT components and listeners.
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.
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.
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:
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.
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.
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.
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.
Once you have added all of the AWT features to your program, print a copy of the Applet as it runs.
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.