ptg7068951
224 HOUR 16:Building a Complex User Interface
Change events occur throughout a slider’s movement. They begin when
the slider is first moved, and they don’t stop occurring until the slider has
been released. For this reason, you might not want to do anything in the
stateChanged()method until the slider has stopped moving.
To see if a slider is currently being moved around, call its
getValueIsAdjusting()method. This method returns truewhile move-
ment is taking place and falseotherwise.
This technique is demonstrated in your next project, a Java application that
uses three sliders to choose a color. Colors are created in Java by using the
Colorclass in the java.awtpackage.
One way to create a Colorobject is to specify the amount of red, green,
and blue in the color. Each of these can be an integer from 0 to 255 with
255 representing the maximum amount of that color.
The following statement creates a Colorobject that represents the color
butterscotch:
Color butterscotch = new Color(255, 204, 128);
The red value used to create this Colorobject is 255, so it contains the max-
imum amount of red. It also contains a large amount of green and some
blue.
Listing 16.2 contains the ColorSlidersapplication, which has three sliders,
three labels for the sliders, and a panel where the color is displayed. Create
a new empty Java file called ColorSliders, enter the text of the listing in
the source editor, and save the file.
LISTING 16.2 The Full Text of ColorSliders.java
1: importjavax.swing.*;
2: importjavax.swing.event.*;
3: importjava.awt.*;
4:
5: public classColorSliders extendsJFrame implementsChangeListener {
6: ColorPanel canvas;
7: JSlider red;
8: JSlider green;
9: JSlider blue;
10:
11: publicColorSliders() {
12: super(“Color Slide”);
13: setLookAndFeel();
14: setSize(270, 300);
15: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);