Java Reference
In-Depth Information
LISTING 10.16
continued
//--------------------------------------------------------------
// Gets the value of each slider, then updates the labels and
// the color panel.
//--------------------------------------------------------------
public void
stateChanged (ChangeEvent event)
{
red = rSlider.getValue();
green = gSlider.getValue();
blue = bSlider.getValue();
rLabel.setText ("Red: " + red);
gLabel.setText ("Green: " + green);
bLabel.setText ("Blue: " + blue);
colorPanel.setBackground (
new
Color (red, green, blue));
}
}
}
constructor specifies the slider's initial value. In our example, the initial value of
each slider is zero, which puts the slider knob to the far left when the program
initially executes.
The panel called
colorPanel
is used to display the color specified by the sliders
by setting its background color. Initially, the settings of the sliders are all zero,
which correspond to the initial color displayed (black).
The
JSlider
class has several methods that allow the programmer to tailor
the look of a slider. Major tick marks can be set at specific intervals using the
setMajorTickSpacing
method. Intermediate minor tick marks can be set using
the
setMinorTickSpacing
method. Neither is displayed, however, unless the
setPaintTicks
method, with a parameter of
true
, is invoked as well. Labels
indicating the value of the major tick marks are displayed if indicated by a call to
the
setPaintLabels
method.
Note that in this example, the major tick spacing is set to 50. Starting at zero,
each increment of 50 is labeled. The last label is therefore 250, even though the
slider value can reach 255.
Search WWH ::
Custom Search