CS 051 Homework Laboratory # 6
Follow The Bouncing Ball
Objective: To gain experience using GUI components and listeners.
In this lab, we would like you to get practice in the use of Swing GUI components. So we would like you to create a program that includes two JSliders, a JLabel, a pop-up menu (JComboBox), and a JButton.
A running version of the program we hope you produce is provided below (if your browser handles applets correctly):
At the "North" side of the screen is a JLabel identifying the program as "Bouncy Ball". At the "East" side is a slider (JSlider) which controls the vertical velocity of the ball. At the "South" side of the screen is a panel (JPanel) which holds a slider (JSlider) which controls the horizontal velocity, a pop-up menu (JComboBox) which allows the user to change the ball's color, and a JButton, which is used to show/hide the ball. If the JButton 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.
Before starting be sure that you have your GUI cheat sheet in front of you (you can find it on the course web site if you don't have it with you). As a quick reminder, remember the basic steps in displaying components:
To react to events generated by the user interacting with a GUI component, remember these steps:
Note that the statements import java.awt.*;, import java.awt.event.*;, import javax.swing.*;, and import javax.swing.event.*; need to appear at the top of your class. These lines inform Java that your program will need access to the standard Java libraries that support GUI components and events (including event listeners).
When you run your program from Eclipse, set the width to 400 and the height to 500.
Here is a suggestion on how to decompose the development of this program into simpler steps.
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 "Sand", which unfortunately is not available in our Linux installation. You can find the list of font names by going to the Application menu and selecting Desktop Preferences. In the Preferences window, select "Font" and click on any one of the top four items that allow you to choose fonts. Find one that is interesting and use that as your font.
This component does not accept input so you do not need a listener.
In order to allow your WindowController extension to "listen" to the JSlider, make sure that your class header ends with the phrase "implements ChangeListener" and that you send the message addChangeListener(this) to your JSlider. Define the stateChanged method so that every time the JSlider 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 SPEED_SCALING_FACTOR (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 ActionEvents. You will need to add a setColor method to the BouncyBall class to change the ball's color.
When your work is complete you should deposit in the appropriate dropoff folder a copy of the entire Eclipse project. Make sure the folder name includes your name and the phrase "Lab 6". Also make sure that your class includes a comment containing your name.
Also, before turning in your work, be sure to double check both its logical organization and your style of presentation. Make your code as clear as possible and include appropriate comments describing major sections of code and declarations.
This lab is due Thursday at 11 p.m., though I expect you to complete this program during your lab period.
Grading Point AllocationsValue | Feature |
Style (6 pts total) | |
1 pts. | Descriptive comments |
1 pts. | Good names and formatting |
1 pts. | Good use of constants |
1 pts. | Good use of private and local variables |
1 pt. | Good use of boolean expressions, ifs, and whiles |
1 pts. | Parameters used appropriately |
Correctness (4 pts total) | |
1 pt. | Changing ball speed |
1 pt. | Changing ball color |
1 pt. | Hide/show |
1 pt. | Changing ball size |
Computer Science
051
Department of Math & Computer Science
Pomona College