-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreferences.java
224 lines (183 loc) · 6.15 KB
/
Preferences.java
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.*;
public class Preferences extends JFrame {
private JMenuBar menuBar;
private JLabel Lbl1;
private JLabel Lbl2;
private JLabel RedLbl;
private JLabel GreenLbl;
private JLabel BlueLbl;
private JTextField RGBTxtField;
private JPanel Cpanel;
public JSlider RSlider;
public JSlider GSlider;
public JSlider BSlider;
static int R = 214;
static int G = 217;
static int B = 223;
// Constructor
public Preferences() {
this.setTitle("Preferences");
this.setSize(400, 200);
// menu generate method
generateMenu();
this.setJMenuBar(menuBar);
// pane with null layout
JPanel contentPane = new JPanel(null);
contentPane.setPreferredSize(new Dimension(400, 200));
contentPane.setBackground(new Color(192, 192, 192));
// adding components to contentPane panel
// adding panel to JFrame and setting of window position and close
// operation
getContentPane().add(contentPane);
contentPane.setLayout(null);
Cpanel = new JPanel();
Cpanel.setBounds(287, 51, 103, 87);
Cpanel.setBackground(new Color(R, G, B));
contentPane.add(Cpanel);
Cpanel.setVisible(false);
RGBTxtField = new JTextField();
RGBTxtField.setBounds(287, 11, 103, 40);
RGBTxtField.setFont(new Font("Consolas", Font.PLAIN, 12));
RGBTxtField.setToolTipText("RGB");
RGBTxtField.setEditable(false);
RGBTxtField.setText("(" + R + "," + G + "," + B + ")");
contentPane.add(RGBTxtField);
RGBTxtField.setColumns(10);
RGBTxtField.setVisible(false);
Lbl1 = new JLabel("This is the Preferences Frame");
Lbl1.setBounds(29, 76, 360, 40);
Lbl1.setFont(new Font("Consolas", Font.PLAIN, 20));
// Lbl1.
contentPane.add(Lbl1);
Lbl1.setVisible(true);
Lbl2 = new JLabel("This changes the color of the background");
Lbl2.setBounds(0, 0, 319, 40);
Lbl2.setFont(new Font("Consolas", Font.PLAIN, 12));
contentPane.add(Lbl2);
Lbl2.setVisible(false);
RedLbl = new JLabel("Red");
RedLbl.setBounds(19, 60, 32, 14);
contentPane.add(RedLbl);
RedLbl.setVisible(false);
GreenLbl = new JLabel("Green");
GreenLbl.setBounds(10, 90, 41, 14);
contentPane.add(GreenLbl);
GreenLbl.setVisible(false);
BlueLbl = new JLabel("Blue");
BlueLbl.setBounds(19, 124, 32, 14);
contentPane.add(BlueLbl);
BlueLbl.setVisible(false);
RSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
RSlider.setBounds(61, 51, 200, 23);
contentPane.add(RSlider);
RSlider.setVisible(false);
RSlider.setValue(R); // set the current R value of the slider to the R
// value of the background
RSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent event) {
// SliderAction();
JSlider source = (JSlider) event.getSource();
R = (int) source.getValue();
// System.out.println("Red = " + R);// for debugging
Cpanel.setBackground(new Color(R, G, B)); // preview pane
MyProject2.contentPane.setBackground(new Color(R, G, B)); // change
// the
// background
// color
// of
// the
// contentPane
// of
// the
// Main
// class
RGBTxtField.setText("(" + R + "," + G + "," + B + ")");
}
});
GSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
GSlider.setBounds(61, 81, 200, 23);
contentPane.add(GSlider);
GSlider.setVisible(false);
GSlider.setValue(G); // set the current G value of the slider to the G
// value of the background
GSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent event) {
JSlider source = (JSlider) event.getSource();
G = (int) source.getValue();
// System.out.println("Green = " + G); // for debugging
Cpanel.setBackground(new Color(R, G, B)); // preview pane
MyProject2.contentPane.setBackground(new Color(R, G, B)); // change
// the
// background
// color
// of
// the
// contentPane
// of
// the
// Main
// class
RGBTxtField.setText("(" + R + "," + G + "," + B + ")");
}
});
BSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
BSlider.setBounds(61, 115, 200, 23);
contentPane.add(BSlider);
BSlider.setVisible(false);
BSlider.setValue(B); // set the current B value of the slider to the B
// value of the background
BSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent event) {
JSlider source = (JSlider) event.getSource();
B = (int) source.getValue();
// System.out.println("Blue = " + B);// for debugging
Cpanel.setBackground(new Color(R, G, B)); // preview pane
MyProject2.contentPane.setBackground(new Color(R, G, B)); // change
// the
// background
// color
// of
// the
// contentPane
// of
// the
// Main
// class
RGBTxtField.setText("(" + R + "," + G + "," + B + ")");
}
});
this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.pack();
this.setVisible(true);
}
// method for generate menu
public void generateMenu() {
menuBar = new JMenuBar();
JMenu Background = new JMenu("Background");
JMenuItem Color = new JMenuItem("Color");
Color.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent bc) {
Lbl1.setVisible(false); // make this 'disapear' when the
// menuItem is clicked
Lbl2.setVisible(true);
RedLbl.setVisible(true);
GreenLbl.setVisible(true);
BlueLbl.setVisible(true);
Cpanel.setVisible(true);
RGBTxtField.setVisible(true);
RSlider.setVisible(true);
GSlider.setVisible(true);
BSlider.setVisible(true);
}
});
// add menuItems to menus
Background.add(Color);
menuBar.add(Background);
}
}