Speed Control
The files SpeedControl.java and SpeedControlPanel.java
contain an applet (and its associated panel) with a circle that moves on the
panel and rebounds from the edges. (NOTE: the program is derived from Listing
8.19 and 8.20 in the text. You may have used the Circle class in Circle.java
in an earlier lab on animation.) Save the program to your directory and run
it in the appletviewer (you may use the file Speed.html)
to see how it works.
In this lab exercise you will add to the panel a slider that controls
the speed of the animation. Study the code in ViewSliderPanel.java
(Listing 9.17 in the text) to help understand the steps below.
- Set up a JSlider object. You need to
- Declare it.
- Instantiate it to be a JSlider that is horizontal with values
ranging from 0 to 200, initially set to 30.
- Set the major tick spacing to 40 and the minor tick spacing to 10.
- Set paint ticks and paint labels to true and the X alignment to
left.
- Set up the change listener for the slider. A skeleton of
a class named SlideListener is already in SpeedControlPanel.java.
You need to
- Complete the body of the statedChanged function. This function
must determine the value of the slider, then set the timer
delay to that value. The timer delay can be set with the
method setDelay (int delay) in the Timer class.
- Add the change listener to the JSlider object.
- Create a label ("Timer Delay") for the slider and align it
to the left.
- Create a JPanel object and add the label and slider to it
then add your panel to the SOUTH of the main panel (note that it
has already been set up to use a border layout).
- Compile and run the applet. Make sure the speed is changing
when the slider is moved. (NOTE: Larger delay means slower!)
- You should have noticed one problem with the applet. The
ball (circle) goes down behind the panel the slider is on. To fix this
problem do the following: