|
Example -- Applet with three sliders
In this example, the applet has three sliders all of which take values in the range 0 to 255.
The initial value of the slider is specified at the time the sliders are created in this case.
The three sliders are used to show the various color combinations that are possible. A color
combination is specified by the three values representing the Red, Green, and Blue components (R G B value).
For example, the color combination (0,0,0) specifies the color black, (255,0,0) the color red, (0,255,0) the color green,
(0,0,255) the color blue, (255,255,255), the color white.
Listing 2.1.4 shows the source code for the above program.
The current values of the three sliders are extracted in the stateChanged
method. This method is called whenever any of the slider values change. We
can even check using the getSource method on the ChangeEvent object to see
which slider actually changed. In the above code, we get the three values
without making this check. With the new values from the sliders, a new
Color object is created which corresponds to the color from the three
values. This color is used to fill a small rectangle.
|