Step 5


Overview

The changes for this step are mostly in the GraphicsApplet class. There, you will write a helper method for creating slider controls, add instance variables for x and y sliders, add the sliders to the control panel, and add two getter methods for the slider values. Finally, in the DrawPanel class, you will add two statements to the updateDrawing() method to get the x and y values from the controller. The following five sections describe these changes in greater detail. At the end of each section, you can recompile the affected class to check for errors.

A Helper Method for Creating Slider Controls

This helper method is similar to the createMethodControl() and createStringControl() helper methods. One important difference is that it will eventually be used to create 8 sliders, so it needs parameters so that you can specify their differences. Thus the form of the method is the following.
    JSlider createSlider(String ttl, int min, int max, int strt) {
	JSlider control = new JSlider(min, max, strt);
	inititialization statements
    }

The min, max, and strt parameters specify the minimum, maximum, and starting slider values. As you can see, they are just passed on to the JSlider constructor. The ttl parameter is the title that will be placed on a border around the slider.

For the initialization statements, you need statements to set the preferred and maximum sizes of control to 160 by 40, set its border with a titled border with title ttl, and add a listener. The listener is a ChangeListener. To use this class, you will need to add the following import.

    import javax.swing.event.*;
A ChangeListener has a stateChanged() method with a ChangeEvent parameter. The only statement in it sends an updateDrawing() message to the draw panel instance variable.

Instance Variables for X and Y Sliders

You need to add two JSlider instance variables, xControl and yControl to the Graphics applet class. They are are initialized by invoking the createSlider() helper methods. The declarations for these variables are similar to the ones for the methodControl and stringControl variables. One difference is that you need to specify four parameters for the helper method: the title for the slider, its minimum and maximum values, and its starting value. Use the values from the following table.
Variable Name Min Value Max Value Init Value
xControl X 0 200 100
yControl Y 0 200 100

The slider variable declarations should be placed before the declaration for the drawPanel variable.

Add the Sliders to the Control Panel

The createControlPanel() helper method constructs the control panel. You need to add two more statements there to add in the xControl and the yControl. These statements are similar to the ones for the methodControl and stringControl.

Getter Methods for the Slider Values

Now you need two methods, getX() and getY() that the draw panel ca use to determine where to place its drawing. These methods are similar to the getMethod() and getString() methods. One difference is that they have an int return value instead of a String. The other main difference is that they send a getValue() message to the appropriate control instead of a getText() message.

Changes in the DrawPanel Class

In the DrawPanel class, you need to add two statements to the updateDrawing() method to get the x and y values from the controller. These statements should invoke the getX() and getY() getter methods that you just wrote, assigning their values to the DrawPanel x and y instance variables. The statements are similar to the ones you already have for the string and method instance variables.

That completes this step. After you have recompiled the classes, run your applet. It should look like the following demonstration applet. When you drag the slider knobs, the displayed text should move horizontally for the X control, vertically for the Y control.

Demonstration Applet