Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
Change Listeners
To monitor the use of a slider in a Java program, you must have a class that implements the ChangeListener interface, which is part of the javax.swing.event package of classes. This interface includes only one method, which takes the following form:
public void stateChanged(ChangeEvent evt) {
// statements to handle the event
}
To register an object as a change listener, call the addChangeListener( Object ) method of the container that holds the slider. When the slider is moved, the listening object's stateChanged() method will be called.
This method is called with a ChangeEvent object that can be used to identify the slider component that was changed in value. Call the object's getSource() method and cast the object to a JSlider, as in the following statement:
JSlider changedSlider = (JSlider)evt.getSource();
In this example, evt is the ChangeEvent object that was an argument to the stateChanged() method.
When you are using a change listener, it's important to note that change events occur throughout the movement of a slider. They begin when the slider is first moved, and 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 true while movement is taking place, and false otherwise.
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 Color class, which is part of the java.awt package.
One way to create a Color object 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.
For example, the following statement creates a Color object that represents the color butterscotch:
Color butterscotch = new Color(255, 204, 128);
The red value used to create this Color object is 255, so it contains the maximum amount of red. It also contains a large amount of green and some blue.
Colors can be represented by a range of values, so they are an ideal use for slider components.
Listing 16.2 contains the ColorSlide application, which has three sliders, three labels for the sliders, and a panel where the color is displayed. Enter this source code into your word processor and save the file as ColorSlide.java.
Example 16.2. The Full Text of ColorSlide.java
1: import javax.swing.*;
2: import javax.swing.event.*;
3: import java.awt.*;
4:
5: public class ColorSlide extends JFrame implements ChangeListener {
6: ColorPanel canvas = new ColorPanel();
7: JSlider red = new JSlider(0, 255, 255);
8: JSlider green = new JSlider(0, 255, 0);
9: JSlider blue = new JSlider(0, 255, 0);
10:
11: public ColorSlide() {
12: super("Color Slide");
13: setSize(270, 300);
14: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
15: setVisible(true);
16: red.setMajorTickSpacing(50);
17: red.setMinorTickSpacing(10);
18: red.setPaintTicks(true);
19: red.setPaintLabels(true);
20: red.addChangeListener(this);
21: green.setMajorTickSpacing(50);
22: green.setMinorTickSpacing(10);
23: green.setPaintTicks(true);
24: green.setPaintLabels(true);
25: green.addChangeListener(this);
26: blue.setMajorTickSpacing(50);
27: blue.setMinorTickSpacing(10);
28: blue.setPaintTicks(true);
29: blue.setPaintLabels(true);
30: blue.addChangeListener(this);
31: JLabel redLabel = new JLabel("Red: ");
32: JLabel greenLabel = new JLabel("Green: ");
33: JLabel blueLabel = new JLabel("Blue: ");
34: GridLayout grid = new GridLayout(4, 1);
35: FlowLayout right = new FlowLayout(FlowLayout.RIGHT);
36: Container pane = getContentPane();
37: pane.setLayout(grid);
38: JPanel redPanel = new JPanel();
39: redPanel.setLayout(right);
40: redPanel.add(redLabel);
41: redPanel.add(red);
42: pane.add(redPanel);
43: JPanel greenPanel = new JPanel();
44: greenPanel.setLayout(right);
45: greenPanel.add(greenLabel);
46: greenPanel.add(green);
47: pane.add(greenPanel);
48: JPanel bluePanel = new JPanel();
49: bluePanel.setLayout(right);
50: bluePanel.add(blueLabel);
51: bluePanel.add(blue);
52: pane.add(bluePanel);
53: pane.add(canvas);
54: setContentPane(pane);
55: }
56:
57: public void stateChanged(ChangeEvent evt) {
58: JSlider source = (JSlider)evt.getSource();
59: if (source.getValueIsAdjusting() != true) {
60: Color current = new Color(red.getValue(), green.getValue(),
61: blue.getValue());
62: canvas.changeColor(current);
63: canvas.repaint();
64: }
65: }
66:
67: public Insets getInsets() {
68: Insets border = new Insets(45, 10, 10, 10);
69: return border;
70: }
71:
72: public static void main(String[] arguments) {
73: ColorSlide cs = new ColorSlide();
74: }
75: }
76:
77: class ColorPanel extends JPanel {
78: Color background;
79:
80: ColorPanel() {
81: background = Color.red;
82: }
83:
84: public void paintComponent(Graphics comp) {
85: Graphics2D comp2D = (Graphics2D)comp;
86: comp2D.setColor(background);
87: comp2D.fillRect(0, 0, getSize().width, getSize().height);
88: }
89:
90: void changeColor(Color newBackground) {
91: background = newBackground;
92: }
93: }
Compile and run the application. A frame will open that contains three sliders that represent the amount of red, green, and blue in a panel along the bottom edge of the frame.
Adjust the values of each slider to change the color that is displayed. Figure 16.3 shows the application being used to create North Texas Mean Green, a color that has a red value of 50, a green value of 150, and a blue value of 50, a shade that inspires students and alumni of the University of North Texas to leap to our feet and make ferocious eagle-claw hand gestures that put fear in our rivals.
Menus | Next Section

Account Sign In
View your cart