FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
processing/app/src/processing/app/ui/ColorChooser.java at master · unexCoder/processing · GitHub
unexCoder
processing
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
processing
/
app
/
src
/
processing
/
app
/
ui
/
ColorChooser.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
723 lines (574 loc) · 19.5 KB
Breadcrumbs
processing
/
app
/
src
/
processing
/
app
/
ui
/
ColorChooser.java
Copy path
File metadata and controls
723 lines (574 loc) · 19.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012-19 The Processing Foundation
Copyright (c) 2006-12 Ben Fry and Casey Reas
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package
processing
.
app
.
ui
;
import
processing
.
app
.
Language
;
import
processing
.
app
.
Platform
;
import
processing
.
core
.*;
import
java
.
awt
.
BorderLayout
;
import
java
.
awt
.
Color
;
import
java
.
awt
.
Container
;
import
java
.
awt
.
Cursor
;
import
java
.
awt
.
Dimension
;
import
java
.
awt
.
Frame
;
import
java
.
awt
.
Graphics
;
import
java
.
awt
.
event
.*;
import
javax
.
swing
.*;
import
javax
.
swing
.
border
.*;
import
javax
.
swing
.
event
.*;
import
javax
.
swing
.
text
.*;
/**
* Generic color selector frame, pulled from the Tool object. API not really
* worked out here (what should the constructor be? how flexible?) So use with
* caution and be ready for it to break in future releases.
*/
public
class
ColorChooser
{
//extends JFrame implements DocumentListener {
int
hue
,
saturation
,
brightness
;
// range 360, 100, 100
int
red
,
green
,
blue
;
// range 256, 256, 256
ColorRange
range
;
ColorSlider
slider
;
JTextField
hueField
,
saturationField
,
brightnessField
;
JTextField
redField
,
greenField
,
blueField
;
JTextField
hexField
;
JPanel
colorPanel
;
DocumentListener
colorListener
;
JDialog
window
;
// public String getMenuTitle() {
// return "Color Selector";
// }
public
ColorChooser
(
Frame
owner
,
boolean
modal
,
Color
initialColor
,
String
buttonName
,
ActionListener
buttonListener
) {
//super("Color Selector");
window
=
new
JDialog
(
owner
,
Language
.
text
(
"color_chooser"
),
modal
);
window
.
getContentPane
().
setLayout
(
new
BorderLayout
());
Box
box
=
Box
.
createHorizontalBox
();
box
.
setBorder
(
new
EmptyBorder
(
12
,
12
,
12
,
12
));
range
=
new
ColorRange
();
Box
rangeBox
=
new
Box
(
BoxLayout
.
Y_AXIS
);
rangeBox
.
setAlignmentY
(
0
);
rangeBox
.
setBorder
(
BorderFactory
.
createBevelBorder
(
BevelBorder
.
LOWERED
));
rangeBox
.
add
(
range
);
box
.
add
(
rangeBox
);
box
.
add
(
Box
.
createHorizontalStrut
(
10
));
slider
=
new
ColorSlider
();
Box
sliderBox
=
new
Box
(
BoxLayout
.
Y_AXIS
);
sliderBox
.
setAlignmentY
(
0
);
sliderBox
.
setBorder
(
BorderFactory
.
createBevelBorder
(
BevelBorder
.
LOWERED
));
sliderBox
.
add
(
slider
);
box
.
add
(
sliderBox
);
box
.
add
(
Box
.
createHorizontalStrut
(
10
));
box
.
add
(
createColorFields
(
buttonName
,
buttonListener
));
// System.out.println("1: " + hexField.getInsets());
box
.
add
(
Box
.
createHorizontalStrut
(
10
));
// System.out.println("2: " + hexField.getInsets());
window
.
getContentPane
().
add
(
box
,
BorderLayout
.
CENTER
);
// System.out.println(hexField);
// System.out.println("3: " + hexField.getInsets());
// colorPanel.setInsets(hexField.getInsets());
window
.
pack
();
window
.
setResizable
(
false
);
// Dimension size = getSize();
// Dimension screen = Toolkit.getScreenSize();
// setLocation((screen.width - size.width) / 2,
// (screen.height - size.height) / 2);
window
.
setLocationRelativeTo
(
null
);
window
.
setDefaultCloseOperation
(
WindowConstants
.
DO_NOTHING_ON_CLOSE
);
window
.
addWindowListener
(
new
WindowAdapter
() {
public
void
windowClosing
(
WindowEvent
e
) {
hide
();
}
});
Toolkit
.
registerWindowCloseKeys
(
window
.
getRootPane
(),
new
ActionListener
() {
public
void
actionPerformed
(
ActionEvent
actionEvent
) {
hide
();
}
});
Toolkit
.
setIcon
(
window
);
colorListener
=
new
ColorListener
();
hueField
.
getDocument
().
addDocumentListener
(
colorListener
);
saturationField
.
getDocument
().
addDocumentListener
(
colorListener
);
brightnessField
.
getDocument
().
addDocumentListener
(
colorListener
);
redField
.
getDocument
().
addDocumentListener
(
colorListener
);
greenField
.
getDocument
().
addDocumentListener
(
colorListener
);
blueField
.
getDocument
().
addDocumentListener
(
colorListener
);
hexField
.
getDocument
().
addDocumentListener
(
colorListener
);
setColor
(
initialColor
);
// System.out.println("4: " + hexField.getInsets());
}
//hexField.setText("#FFFFFF");
public
void
show
() {
window
.
setVisible
(
true
);
}
public
void
hide
() {
window
.
setVisible
(
false
);
}
public
Color
getColor
() {
return
new
Color
(
red
,
green
,
blue
);
}
public
void
setColor
(
Color
color
) {
updateRGB
(
color
.
getRGB
());
}
public
String
getHexColor
() {
return
"#"
+
PApplet
.
hex
(
red
,
2
) +
PApplet
.
hex
(
green
,
2
) +
PApplet
.
hex
(
blue
,
2
);
}
public
class
ColorListener
implements
DocumentListener
{
public
void
changedUpdate
(
DocumentEvent
e
) {
//System.out.println("changed");
}
public
void
removeUpdate
(
DocumentEvent
e
) {
//System.out.println("remove");
}
boolean
updating
;
public
void
insertUpdate
(
DocumentEvent
e
) {
if
(
updating
)
return
;
// don't update forever recursively
updating
=
true
;
Document
doc
=
e
.
getDocument
();
if
(
doc
==
hueField
.
getDocument
()) {
hue
=
bounded
(
hue
,
hueField
,
359
);
updateRGB
();
updateHex
();
}
else
if
(
doc
==
saturationField
.
getDocument
()) {
saturation
=
bounded
(
saturation
,
saturationField
,
99
);
updateRGB
();
updateHex
();
}
else
if
(
doc
==
brightnessField
.
getDocument
()) {
brightness
=
bounded
(
brightness
,
brightnessField
,
99
);
updateRGB
();
updateHex
();
}
else
if
(
doc
==
redField
.
getDocument
()) {
red
=
bounded
(
red
,
redField
,
255
);
updateHSB
();
updateHex
();
}
else
if
(
doc
==
greenField
.
getDocument
()) {
green
=
bounded
(
green
,
greenField
,
255
);
updateHSB
();
updateHex
();
}
else
if
(
doc
==
blueField
.
getDocument
()) {
blue
=
bounded
(
blue
,
blueField
,
255
);
updateHSB
();
updateHex
();
}
else
if
(
doc
==
hexField
.
getDocument
()) {
String
str
=
hexField
.
getText
();
if
(
str
.
startsWith
(
"#"
)) {
str
=
str
.
substring
(
1
);
}
while
(
str
.
length
() <
6
) {
str
+=
"0"
;
}
if
(
str
.
length
() >
6
) {
str
=
str
.
substring
(
0
,
6
);
}
updateRGB
(
Integer
.
parseInt
(
str
,
16
));
updateHSB
();
}
range
.
repaint
();
slider
.
repaint
();
//colorPanel.setBackground(new Color(red, green, blue));
colorPanel
.
repaint
();
updating
=
false
;
}
}
/**
* Set the RGB values based on the current HSB values.
*/
protected
void
updateRGB
() {
updateRGB
(
Color
.
HSBtoRGB
(
hue
/
359f
,
saturation
/
99f
,
brightness
/
99f
));
}
/**
* Set the RGB values based on a calculated ARGB int.
* Used by both updateRGB() to set the color from the HSB values,
* and by updateHex(), to unpack the hex colors and assign them.
*/
protected
void
updateRGB
(
int
rgb
) {
red
= (
rgb
>>
16
) &
0xff
;
green
= (
rgb
>>
8
) &
0xff
;
blue
=
rgb
&
0xff
;
redField
.
setText
(
String
.
valueOf
(
red
));
greenField
.
setText
(
String
.
valueOf
(
green
));
blueField
.
setText
(
String
.
valueOf
(
blue
));
}
/**
* Set the HSB values based on the current RGB values.
*/
protected
void
updateHSB
() {
float
hsb
[] =
new
float
[
3
];
Color
.
RGBtoHSB
(
red
,
green
,
blue
,
hsb
);
hue
= (
int
) (
hsb
[
0
] *
359.0f
);
saturation
= (
int
) (
hsb
[
1
] *
99.0f
);
brightness
= (
int
) (
hsb
[
2
] *
99.0f
);
hueField
.
setText
(
String
.
valueOf
(
hue
));
saturationField
.
setText
(
String
.
valueOf
(
saturation
));
brightnessField
.
setText
(
String
.
valueOf
(
brightness
));
}
protected
void
updateHex
() {
hexField
.
setText
(
getHexColor
());
}
/**
* Get the bounded value for a specific range. If the value is outside
* the max, you can't edit right away, so just act as if it's already
* been bounded and return the bounded value, then fire an event to set
* it to the value that was just returned.
*/
protected
int
bounded
(
int
current
,
final
JTextField
field
,
final
int
max
) {
String
text
=
field
.
getText
();
if
(
text
.
length
() ==
0
) {
return
0
;
}
try
{
int
value
=
Integer
.
parseInt
(
text
);
if
(
value
>
max
) {
SwingUtilities
.
invokeLater
(
new
Runnable
() {
public
void
run
() {
field
.
setText
(
String
.
valueOf
(
max
));
}
});
return
max
;
}
return
value
;
}
catch
(
NumberFormatException
e
) {
return
current
;
// should not be reachable
}
}
protected
Container
createColorFields
(
String
buttonName
,
ActionListener
buttonListener
) {
Box
box
=
Box
.
createVerticalBox
();
box
.
setAlignmentY
(
0
);
final
int
GAP
=
Platform
.
isWindows
() ?
5
:
0
;
final
int
BETWEEN
=
Platform
.
isWindows
() ?
8
:
6
;
//10;
Box
row
;
row
=
Box
.
createHorizontalBox
();
if
(
Platform
.
isMacOS
()) {
row
.
add
(
Box
.
createHorizontalStrut
(
17
));
}
else
{
row
.
add
(
createFixedLabel
(
""
));
}
// Can't just set the bg color of the panel because it also tints the bevel
// (on OS X), which looks odd. So instead we override paintComponent().
colorPanel
=
new
JPanel
() {
public
void
paintComponent
(
Graphics
g
) {
g
.
setColor
(
new
Color
(
red
,
green
,
blue
));
Dimension
size
=
getSize
();
g
.
fillRect
(
0
,
0
,
size
.
width
,
size
.
height
);
}
};
colorPanel
.
setBorder
(
BorderFactory
.
createBevelBorder
(
BevelBorder
.
LOWERED
));
Dimension
dim
=
new
Dimension
(
70
,
25
);
colorPanel
.
setMinimumSize
(
dim
);
colorPanel
.
setMaximumSize
(
dim
);
colorPanel
.
setPreferredSize
(
dim
);
row
.
add
(
colorPanel
);
row
.
add
(
Box
.
createHorizontalGlue
());
box
.
add
(
row
);
box
.
add
(
Box
.
createVerticalStrut
(
BETWEEN
));
// if (Base.isMacOS()) { // need a little extra
// box.add(Box.createVerticalStrut(BETWEEN));
// }
row
=
Box
.
createHorizontalBox
();
row
.
add
(
createFixedLabel
(
"H"
));
row
.
add
(
hueField
=
new
NumberField
(
4
,
false
));
row
.
add
(
new
JLabel
(
"
\u00B0
"
));
// degree symbol
row
.
add
(
Box
.
createHorizontalGlue
());
box
.
add
(
row
);
box
.
add
(
Box
.
createVerticalStrut
(
GAP
));
row
=
Box
.
createHorizontalBox
();
row
.
add
(
createFixedLabel
(
"S"
));
row
.
add
(
saturationField
=
new
NumberField
(
4
,
false
));
row
.
add
(
new
JLabel
(
" %"
));
row
.
add
(
Box
.
createHorizontalGlue
());
box
.
add
(
row
);
box
.
add
(
Box
.
createVerticalStrut
(
GAP
));
row
=
Box
.
createHorizontalBox
();
row
.
add
(
createFixedLabel
(
"B"
));
row
.
add
(
brightnessField
=
new
NumberField
(
4
,
false
));
row
.
add
(
new
JLabel
(
" %"
));
row
.
add
(
Box
.
createHorizontalGlue
());
box
.
add
(
row
);
box
.
add
(
Box
.
createVerticalStrut
(
BETWEEN
));
//
row
=
Box
.
createHorizontalBox
();
row
.
add
(
createFixedLabel
(
"R"
));
row
.
add
(
redField
=
new
NumberField
(
4
,
false
));
row
.
add
(
Box
.
createHorizontalGlue
());
box
.
add
(
row
);
box
.
add
(
Box
.
createVerticalStrut
(
GAP
));
row
=
Box
.
createHorizontalBox
();
row
.
add
(
createFixedLabel
(
"G"
));
row
.
add
(
greenField
=
new
NumberField
(
4
,
false
));
row
.
add
(
Box
.
createHorizontalGlue
());
box
.
add
(
row
);
box
.
add
(
Box
.
createVerticalStrut
(
GAP
));
row
=
Box
.
createHorizontalBox
();
row
.
add
(
createFixedLabel
(
"B"
));
row
.
add
(
blueField
=
new
NumberField
(
4
,
false
));
row
.
add
(
Box
.
createHorizontalGlue
());
box
.
add
(
row
);
box
.
add
(
Box
.
createVerticalStrut
(
BETWEEN
));
//
row
=
Box
.
createHorizontalBox
();
row
.
add
(
createFixedLabel
(
""
));
// Windows needs extra space, OS X and Linux do not
// Mac OS X needs 6 because #CCCCCC is quite wide
final
int
hexCount
=
Platform
.
isWindows
() ?
7
:
6
;
row
.
add
(
hexField
=
new
NumberField
(
hexCount
,
true
));
row
.
add
(
Box
.
createHorizontalGlue
());
box
.
add
(
row
);
box
.
add
(
Box
.
createVerticalStrut
(
GAP
));
//
// // Not great, because the insets make things weird anyway
// //Dimension dim = new Dimension(hexField.getPreferredSize());
// Dimension dim = new Dimension(70, 20);
// colorPanel.setMinimumSize(dim);
// colorPanel.setMaximumSize(dim);
// colorPanel.setPreferredSize(dim);
//// colorPanel.setBorder(new EmptyBorder(hexField.getInsets()));
//
row
=
Box
.
createHorizontalBox
();
if
(
Platform
.
isMacOS
()) {
row
.
add
(
Box
.
createHorizontalStrut
(
11
));
}
else
{
row
.
add
(
createFixedLabel
(
""
));
}
JButton
button
=
new
JButton
(
buttonName
);
button
.
addActionListener
(
buttonListener
);
//System.out.println("button: " + button.getInsets());
row
.
add
(
button
);
row
.
add
(
Box
.
createHorizontalGlue
());
box
.
add
(
row
);
row
=
Box
.
createHorizontalBox
();
if
(
Platform
.
isMacOS
()) {
row
.
add
(
Box
.
createHorizontalStrut
(
11
));
}
else
{
row
.
add
(
createFixedLabel
(
""
));
}
button
=
new
JButton
(
Language
.
text
(
"prompt.cancel"
));
button
.
addActionListener
(
new
ActionListener
() {
@
Override
public
void
actionPerformed
(
ActionEvent
e
) {
ColorChooser
.
this
.
hide
();
}
});
row
.
add
(
button
);
row
.
add
(
Box
.
createHorizontalGlue
());
box
.
add
(
row
);
//
box
.
add
(
Box
.
createVerticalGlue
());
return
box
;
}
int
labelH
;
/**
* return a label of a fixed width
*/
protected
JLabel
createFixedLabel
(
String
title
) {
JLabel
label
=
new
JLabel
(
title
);
if
(
labelH
==
0
) {
labelH
=
label
.
getPreferredSize
().
height
;
}
Dimension
dim
=
new
Dimension
(
15
,
labelH
);
label
.
setPreferredSize
(
dim
);
label
.
setMinimumSize
(
dim
);
label
.
setMaximumSize
(
dim
);
return
label
;
}
public
class
ColorRange
extends
JComponent
{
static
final
int
WIDE
=
256
;
static
final
int
HIGH
=
256
;
private
int
lastX
,
lastY
;
public
ColorRange
() {
setCursor
(
Cursor
.
getPredefinedCursor
(
Cursor
.
CROSSHAIR_CURSOR
));
addMouseListener
(
new
MouseAdapter
() {
@
Override
public
void
mousePressed
(
MouseEvent
e
) {
updateMouse
(
e
);
}
});
addMouseMotionListener
(
new
MouseMotionAdapter
() {
@
Override
public
void
mouseDragged
(
MouseEvent
e
) {
updateMouse
(
e
);
}
});
addKeyListener
(
new
KeyAdapter
() {
@
Override
public
void
keyPressed
(
KeyEvent
e
) {
super
.
keyPressed
(
e
);
if
(
e
.
getKeyCode
() ==
KeyEvent
.
VK_ESCAPE
) {
ColorChooser
.
this
.
hide
();
}
}
});
}
private
void
updateMouse
(
MouseEvent
e
) {
int
mouseX
=
e
.
getX
();
int
mouseY
=
e
.
getY
();
if
((
mouseX
>=
0
) && (
mouseX
<
WIDE
) &&
(
mouseY
>=
0
) && (
mouseY
<
HIGH
)) {
int
nsaturation
= (
int
) (
100
* (
mouseX
/
255.0f
));
int
nbrightness
=
100
- ((
int
) (
100
* (
mouseY
/
255.0f
)));
saturationField
.
setText
(
String
.
valueOf
(
nsaturation
));
brightnessField
.
setText
(
String
.
valueOf
(
nbrightness
));
lastX
=
mouseX
;
lastY
=
mouseY
;
}
}
@
Override
public
void
paintComponent
(
Graphics
g
) {
super
.
paintComponent
(
g
);
for
(
int
j
=
0
;
j
<
WIDE
;
j
++) {
for
(
int
i
=
0
;
i
<
HIGH
;
i
++) {
g
.
setColor
(
Color
.
getHSBColor
(
hue
/
360f
,
i
/
256f
, (
255
-
j
) /
256f
));
g
.
fillRect
(
i
,
j
,
1
,
1
);
}
}
g
.
setColor
((
brightness
>
50
) ?
Color
.
BLACK
:
Color
.
WHITE
);
g
.
drawRect
(
lastX
-
5
,
lastY
-
5
,
10
,
10
);
}
@
Override
public
Dimension
getPreferredSize
() {
return
new
Dimension
(
WIDE
,
HIGH
);
}
@
Override
public
Dimension
getMinimumSize
() {
return
getPreferredSize
();
}
@
Override
public
Dimension
getMaximumSize
() {
return
getPreferredSize
();
}
}
public
class
ColorSlider
extends
JComponent
{
static
final
int
WIDE
=
20
;
static
final
int
HIGH
=
256
;
public
ColorSlider
() {
setCursor
(
Cursor
.
getPredefinedCursor
(
Cursor
.
CROSSHAIR_CURSOR
));
addMouseListener
(
new
MouseAdapter
() {
@
Override
public
void
mousePressed
(
MouseEvent
e
) {
updateMouse
(
e
);
}
});
addMouseMotionListener
(
new
MouseMotionAdapter
() {
@
Override
public
void
mouseDragged
(
MouseEvent
e
) {
updateMouse
(
e
);
}
});
addKeyListener
(
new
KeyAdapter
() {
@
Override
public
void
keyPressed
(
KeyEvent
e
) {
super
.
keyPressed
(
e
);
if
(
e
.
getKeyCode
() ==
KeyEvent
.
VK_ESCAPE
) {
ColorChooser
.
this
.
hide
();
}
}
});
}
private
void
updateMouse
(
MouseEvent
e
) {
int
mouseX
=
e
.
getX
();
int
mouseY
=
e
.
getY
();
if
((
mouseX
>=
0
) && (
mouseX
<
WIDE
) &&
(
mouseY
>=
0
) && (
mouseY
<
HIGH
)) {
int
nhue
=
359
- (
int
) (
359
* (
mouseY
/
255.0f
));
hueField
.
setText
(
String
.
valueOf
(
nhue
));
}
}
public
void
paintComponent
(
Graphics
g
) {
super
.
paintComponent
(
g
);
int
sel
=
255
- (
int
) (
255
* (
hue
/
359.0
));
for
(
int
j
=
0
;
j
<
HIGH
;
j
++) {
Color
color
=
Color
.
getHSBColor
((
255
-
j
) /
256f
,
1
,
1
);
if
(
j
==
sel
) {
color
=
Color
.
BLACK
;
}
g
.
setColor
(
color
);
g
.
drawRect
(
0
,
j
,
WIDE
,
1
);
}
}
public
Dimension
getPreferredSize
() {
return
new
Dimension
(
WIDE
,
HIGH
);
}
public
Dimension
getMinimumSize
() {
return
getPreferredSize
();
}
public
Dimension
getMaximumSize
() {
return
getPreferredSize
();
}
}
/**
* Extension of JTextField that only allows numbers
*/
static
class
NumberField
extends
JTextField
{
public
boolean
allowHex
;
public
NumberField
(
int
cols
,
boolean
allowHex
) {
super
(
cols
);
this
.
allowHex
=
allowHex
;
}
protected
Document
createDefaultModel
() {
return
new
NumberDocument
(
this
);
}
public
Dimension
getPreferredSize
() {
if
(!
allowHex
) {
return
new
Dimension
(
45
,
super
.
getPreferredSize
().
height
);
}
return
super
.
getPreferredSize
();
}
public
Dimension
getMinimumSize
() {
return
getPreferredSize
();
}
public
Dimension
getMaximumSize
() {
return
getPreferredSize
();
}
}
/**
* Document model to go with JTextField that only allows numbers.
*/
static
class
NumberDocument
extends
PlainDocument
{
NumberField
parentField
;
public
NumberDocument
(
NumberField
parentField
) {
this
.
parentField
=
parentField
;
//System.out.println("setting parent to " + parentSelector);
}
public
void
insertString
(
int
offs
,
String
str
,
AttributeSet
a
)
throws
BadLocationException
{
if
(
str
==
null
)
return
;
char
chars
[] =
str
.
toCharArray
();
int
charCount
=
0
;
// remove any non-digit chars
for
(
int
i
=
0
;
i
<
chars
.
length
;
i
++) {
boolean
ok
=
Character
.
isDigit
(
chars
[
i
]);
if
(
parentField
.
allowHex
) {
if
((
chars
[
i
] >=
'A'
) && (
chars
[
i
] <=
'F'
))
ok
=
true
;
if
((
chars
[
i
] >=
'a'
) && (
chars
[
i
] <=
'f'
))
ok
=
true
;
if
((
offs
==
0
) && (
i
==
0
) && (
chars
[
i
] ==
'#'
))
ok
=
true
;
}
if
(
ok
) {
if
(
charCount
!=
i
) {
// shift if necessary
chars
[
charCount
] =
chars
[
i
];
}
charCount
++;
}
}
super
.
insertString
(
offs
,
new
String
(
chars
,
0
,
charCount
),
a
);
// can't call any sort of methods on the enclosing class here
// seems to have something to do with how Document objects are set up
}
}
// static public void main(String[] args) {
// ColorSelector cs = new ColorSelector();
// cs.init(null);
// EventQueue.invokeLater(cs);
// }
}
Back
|
FazBrowse Home
|
New Git URL