-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorApp.java
More file actions
executable file
·352 lines (317 loc) · 9.73 KB
/
Copy pathColorApp.java
File metadata and controls
executable file
·352 lines (317 loc) · 9.73 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// CS 326 HW8 Nick Alvarez Spring 2020
// File path needs to be modified to your operating environment and directories to run properly on computer
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;
public class ColorApp extends JFrame
{
private static final long serialVersionUID = 1;
protected JList listColors;
public String windowSaved = "Color Sampler", windowUnsaved = "Color Sampler*";
protected ColorBox cBox;
protected JLabel redLabel, greenLabel, blueLabel;
protected JTextField redText, greenText, blueText;
protected JButton rp, rm, gp, gm, bp, bm, save, reset;
protected int red = 255, green = 255, blue = 255;
protected File colorFile = new File("colors.txt");
public static void main(String argv[])
{
/*JFrame frame = new JFrame("Color Sampler");
frame.setSize(350,150);
frame.addWindowListener(new WindowDestroyer());
frame.setVisible(true);*/
new ColorApp("Color Sampler");
}
public class ColorInput //Must be placed above ColorApp
{
public String c;
public int r,g,b;
}
protected ColorInput[] colorsTXT = new ColorInput[11];
public void FileInput(ColorInput[] myColors) throws IOException
{
//System.out.println("Attempted to run FileInput()\n");
//System.out.println(new File(".").getAbsolutePath());
FileInputStream stream = new FileInputStream(colorFile);
System.out.println("colors.txt has been accessed.\n");
InputStreamReader reader = new InputStreamReader(stream);
StreamTokenizer tokens = new StreamTokenizer(reader);
int totalColors = 0;
while(tokens.nextToken() != StreamTokenizer.TT_EOF) // Did not like tokens.TT_EOF
{
myColors[totalColors] = new ColorInput();
myColors[totalColors].c = (String) tokens.sval;
tokens.nextToken();
myColors[totalColors].r = (int) tokens.nval;
tokens.nextToken();
myColors[totalColors].g = (int) tokens.nval;
tokens.nextToken();
myColors[totalColors].b = (int) tokens.nval;
totalColors++;
}
stream.close();
}
public ColorApp(String title)
{
super(title);
setSize(500,500);
addWindowListener(new WindowDestroyer());
WindowSetup();
try
{
FileInput(colorsTXT);
}
catch (IOException e)
{
System.out.println("Oops! You did not define your file directory. Please place 'colors.txt' in this path: " + new File(".").getAbsolutePath());
System.exit(0);
}
String names[] = new String[11];
for (int i=0; i<11; i++)
{
names[i] = colorsTXT[i].c;
}
listColors.setListData(names);
cBox = new ColorBox();
getContentPane().add(cBox);
cBox.setLocation(10,10);
cBox.setSize(380, 230);
//getContentPane().setLayout(null);
//getContentPane().add(cBox);
//getContentPane().add(new JScrollPane(listColors));
//listColors.setBounds(320, 10, 490, 490);
//cBox.setBounds(10, 10, 300, 200);
setVisible(true);
//String colors[] = {"Red", "Yellow", "Blue"};
}
private class ColorBox extends JComponent // The graphical part that displays the colors
{
private static final long serialVersionUID = 1;
public void paint(Graphics g)
{
Dimension d = getSize();
g.setColor(new Color(red, green, blue, 255)); //Sets to white
g.fillRect(0,0,d.width-2, d.height-2);
}
}
public void WindowSetup() // Setting up all items, sizes, locations in content pane
{
//Item instances
//R
rp = new JButton("+");
rp.addActionListener(new ActionHandler());
rm = new JButton("-");
rm.addActionListener(new ActionHandler());
redText = new JTextField();
redLabel = new JLabel("Red:");
//G
gp = new JButton("+");
gp.addActionListener(new ActionHandler());
gm = new JButton("-");
gm.addActionListener(new ActionHandler());
greenText = new JTextField();
greenLabel = new JLabel("Green:");
//B
bp = new JButton("+");
bp.addActionListener(new ActionHandler());
bm = new JButton("-");
bm.addActionListener(new ActionHandler());
blueText = new JTextField();
blueLabel = new JLabel("Blue:");
//List (from ColorApp)
listColors = new JList();
listColors.addListSelectionListener(new ListHandler());
//Function buttons
save = new JButton("SAVE");
save.addActionListener(new ActionHandler());
reset = new JButton("RESET");
reset.addActionListener(new ActionHandler());
//Content setup (from ColorApp)
getContentPane().setLayout(null);
getContentPane().add(redLabel);
getContentPane().add(redText);
getContentPane().add(rp);
getContentPane().add(rm);
getContentPane().add(greenLabel);
getContentPane().add(greenText);
getContentPane().add(gp);
getContentPane().add(gm);
getContentPane().add(blueLabel);
getContentPane().add(blueText);
getContentPane().add(bp);
getContentPane().add(bm);
getContentPane().add(save);
getContentPane().add(reset);
getContentPane().add(listColors);
//Item locations
redLabel.setLocation(10, 250);
redLabel.setSize(40,40);
redText.setLocation(60, 250);
redText.setSize(90,40);
rp.setLocation(160, 250);
rp.setSize(90,40);
rm.setLocation(260, 250);
rm.setSize(90,40);
greenLabel.setLocation(10, 300);
greenLabel.setSize(40,40);
greenText.setLocation(60, 300);
greenText.setSize(90,40);
gp.setLocation(160, 300);
gp.setSize(90,40);
gm.setLocation(260, 300);
gm.setSize(90,40);
blueLabel.setLocation(10, 350);
blueLabel.setSize(40,40);
blueText.setLocation(60, 350);
blueText.setSize(90,40);
bp.setLocation(160, 350);
bp.setSize(90,40);
bm.setLocation(260, 350);
bm.setSize(90,40);
save.setLocation(10, 420);
save.setSize(90,20);
reset.setLocation(110, 420);
reset.setSize(90,20);
listColors.setLocation(400, 10);
listColors.setSize(90, 480);
}
private class WindowDestroyer extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
try
{
FileOutputStream ostream = new FileOutputStream(colorFile); // Save any changes on close
PrintWriter writer = new PrintWriter(ostream);
for (int i=0; i<11; i++)
{
writer.println(colorsTXT[i].c + " " + colorsTXT[i].r + " " + colorsTXT[i].g + " " + colorsTXT[i].b);
}
writer.flush();
ostream.close();
}
catch (IOException f)
{
System.out.println("File write error");
}
System.exit(0);
}
}
private class ActionHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//Save
if (e.getSource() == save)
{
ColorApp.this.setTitle(windowSaved); // No unsaved changes
int i = listColors.getSelectedIndex();
colorsTXT[i].r = red; // Update colors, to be written later
colorsTXT[i].g = green;
colorsTXT[i].b = blue;
}
//Reset
else if (e.getSource() == reset)
{
ColorApp.this.setTitle(windowSaved);
int i = listColors.getSelectedIndex();
red = colorsTXT[i].r;
redText.setText(red + "");
green = colorsTXT[i].g;
greenText.setText(green + "");
blue = colorsTXT[i].b;
blueText.setText(blue + "");
repaint();
}
//Red +
else if (e.getSource() == rp)
{
if (red<255)
{
red += 5;
redText.setText(red + ""); //Found out the hard way that adding "" ensures it is a String
repaint();
ColorApp.this.setTitle(windowUnsaved);
}
}
//Red -
else if (e.getSource() == rm)
{
if (red>0)
{
ColorApp.this.setTitle(windowUnsaved);
red -= 5;
redText.setText(red + "");
repaint();
}
}
//Green +
else if (e.getSource() == gp)
{
if (green<255)
{
ColorApp.this.setTitle(windowUnsaved);
green += 5;
greenText.setText(green + "");
repaint();
}
}
else if (e.getSource() == gm)
{
if (green>0)
{
ColorApp.this.setTitle(windowUnsaved);
green -= 5;
greenText.setText(green +"");
repaint();
}
}
//Blue +
else if (e.getSource() == bp)
{
if (blue<255)
{
ColorApp.this.setTitle(windowUnsaved);
blue += 5;
blueText.setText(blue + "");
repaint();
}
}
//Blue -
else if (e.getSource() == bm)
{
if (blue>0)
{
ColorApp.this.setTitle(windowUnsaved);
blue -= 5;
blueText.setText(blue + "");
repaint();
}
}
}
}
private class ListHandler implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent e)
{
if (e.getSource() == listColors)
{
if (!e.getValueIsAdjusting()) //We change our selection so we must change the displayed color
{
int i = listColors.getSelectedIndex();
ColorApp.this.setTitle(windowSaved); // Unsaved data lost
red = colorsTXT[i].r;
redText.setText(red + "");
green = colorsTXT[i].g;
greenText.setText(green + "");
blue = colorsTXT[i].b;
blueText.setText(blue + "");
repaint();
}
}
}
}
}