-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathColorBuilder.java
More file actions
133 lines (111 loc) · 4.75 KB
/
ColorBuilder.java
File metadata and controls
133 lines (111 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package gui;
/**
* RUN:
* javac -cp .; gui/ColorBuilder.java && java -cp .; gui.ColorBuilder
* OUTPUT:
*
*/
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import net.mindview.util.*;
public class ColorBuilder extends JFrame {
private static final int WIDTH = 300;
private static final int HEIHGT = 180;
private JSlider aRedSlider = new JSlider(0, 255, 50);
private JSlider aGreenSlider = new JSlider(0, 255, 50);
private JSlider aBlueSlider = new JSlider(0, 255, 50);
private JTextField aRedValue = new JTextField("50", 5);
private JTextField aGreenValue = new JTextField("50", 5);
private JTextField aBlueValue = new JTextField("50", 5);
private JLabel aRedLabel = new JLabel("Red");
private JLabel aGreenLabel = new JLabel("Green");
private JLabel aBlueLabel = new JLabel("Blue");
private JPanel colorPanel = new JPanel();
public ColorBuilder()
{
add(colorPanel);
colorPanel.setOpaque(true);
colorPanel.setBackground(buildColor());
aRedValue.setEditable(false);
aGreenValue.setEditable(false);
aBlueValue.setEditable(false);
GroupLayout layout = new GroupLayout(colorPanel);
colorPanel.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(aRedLabel)
.addComponent(aGreenLabel)
.addComponent(aBlueLabel)
)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(aRedSlider)
.addComponent(aGreenSlider)
.addComponent(aBlueSlider)
)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(aRedValue)
.addComponent(aGreenValue)
.addComponent(aBlueValue)
)
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(aRedLabel)
.addComponent(aRedSlider)
.addComponent(aRedValue, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(aGreenLabel)
.addComponent(aGreenSlider)
.addComponent(aGreenValue, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(aBlueLabel)
.addComponent(aBlueSlider)
.addComponent(aBlueValue, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
)
);
aRedSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
aRedValue.setText(String.valueOf(((JSlider) e.getSource()).getValue()));
colorPanel.setBackground(buildColor());
}
});
aGreenSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
aGreenValue.setText(String.valueOf(((JSlider) e.getSource()).getValue()));
colorPanel.setBackground(buildColor());
}
});
aBlueSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
aBlueValue.setText(String.valueOf(((JSlider) e.getSource()).getValue()));
colorPanel.setBackground(buildColor());
}
});
}
private Color buildColor() {
return new Color(
Integer.parseInt(aRedValue.getText()),
Integer.parseInt(aGreenValue.getText()),
Integer.parseInt(aBlueValue.getText())
);
}
public static void main(String[] args) {
SwingConsole.run(new ColorBuilder(), WIDTH, HEIHGT);
}
}