3939
4040
4141/**
42- * Generic color selector frame, pulled from the Tool object.
42+ * Generic color selector frame, pulled from the Tool object. API not really
43+ * worked out here (what should the constructor be? how flexible?) So use with
44+ * caution and be ready for it to break in future releases.
4345 */
4446public class ColorChooser { //extends JFrame implements DocumentListener {
4547
@@ -60,14 +62,13 @@ public class ColorChooser { //extends JFrame implements DocumentListener {
6062 JDialog window ;
6163
6264
63- public String getMenuTitle () {
64- return "Color Selector" ;
65- }
65+ // public String getMenuTitle() {
66+ // return "Color Selector";
67+ // }
6668
6769
68- public ColorChooser (Frame owner , boolean modal ,
69- String buttonName , ActionListener buttonListener ) {
70- //window = new jdi
70+ public ColorChooser (Frame owner , boolean modal , Color initialColor ,
71+ String buttonName , ActionListener buttonListener ) {
7172 //super("Color Selector");
7273 window = new JDialog (owner , "Color Selector" , modal );
7374 window .getContentPane ().setLayout (new BorderLayout ());
@@ -93,7 +94,7 @@ public ColorChooser(Frame owner, boolean modal,
9394 box .add (sliderBox );
9495 box .add (Box .createHorizontalStrut (10 ));
9596
96- box .add (createColorFields ());
97+ box .add (createColorFields (buttonName , buttonListener ));
9798// System.out.println("1: " + hexField.getInsets());
9899
99100 box .add (Box .createHorizontalStrut (10 ));
@@ -117,12 +118,12 @@ public ColorChooser(Frame owner, boolean modal,
117118 window .setDefaultCloseOperation (WindowConstants .DO_NOTHING_ON_CLOSE );
118119 window .addWindowListener (new WindowAdapter () {
119120 public void windowClosing (WindowEvent e ) {
120- hideSelector ();
121+ hide ();
121122 }
122123 });
123124 Toolkit .registerWindowCloseKeys (window .getRootPane (), new ActionListener () {
124125 public void actionPerformed (ActionEvent actionEvent ) {
125- hideSelector ();
126+ hide ();
126127 }
127128 });
128129
@@ -137,20 +138,21 @@ public void actionPerformed(ActionEvent actionEvent) {
137138 blueField .getDocument ().addDocumentListener (colorListener );
138139 hexField .getDocument ().addDocumentListener (colorListener );
139140
141+ setColor (initialColor );
140142// System.out.println("4: " + hexField.getInsets());
141143 }
142144
143145
144146 //hexField.setText("#FFFFFF");
145147
146148
147- public void showSelector () {
149+ public void show () {
148150 window .setVisible (true );
149151 window .setCursor (Cursor .getPredefinedCursor (Cursor .CROSSHAIR_CURSOR ));
150152 }
151153
152154
153- public void hideSelector () {
155+ public void hide () {
154156 window .setVisible (false );
155157 }
156158
@@ -160,6 +162,11 @@ public Color getColor() {
160162 }
161163
162164
165+ public void setColor (Color color ) {
166+ updateRGB (color .getRGB ());
167+ }
168+
169+
163170 public String getHexColor () {
164171 return "#" + PApplet .hex (red , 2 ) + PApplet .hex (green , 2 ) + PApplet .hex (blue , 2 );
165172 }
@@ -313,7 +320,7 @@ public void run() {
313320 }
314321
315322
316- protected Container createColorFields () {
323+ protected Container createColorFields (String buttonName , ActionListener buttonListener ) {
317324 Box box = Box .createVerticalBox ();
318325 box .setAlignmentY (0 );
319326
@@ -403,7 +410,8 @@ public void paintComponent(Graphics g) {
403410 row = Box .createHorizontalBox ();
404411 row .add (createFixedLabel ("" ));
405412 // Windows needs extra space, OS X and Linux do not
406- final int hexCount = Base .isWindows () ? 7 : 5 ;
413+ // Mac OS X needs 6 because #CCCCCC is quite wide
414+ final int hexCount = Base .isWindows () ? 7 : 6 ;
407415 row .add (hexField = new NumberField (hexCount , true ));
408416 row .add (Box .createHorizontalGlue ());
409417 box .add (row );
@@ -421,19 +429,18 @@ public void paintComponent(Graphics g) {
421429
422430 //
423431
424- /*
425432 row = Box .createHorizontalBox ();
426433 if (Base .isMacOS ()) {
427434 row .add (Box .createHorizontalStrut (11 ));
428435 } else {
429436 row .add (createFixedLabel ("" ));
430437 }
431- JButton button = new JButton("Select");
438+ JButton button = new JButton (buttonName );
439+ button .addActionListener (buttonListener );
432440 //System.out.println("button: " + button.getInsets());
433441 row .add (button );
434442 row .add (Box .createHorizontalGlue ());
435443 box .add (row );
436- */
437444
438445 //
439446
@@ -533,8 +540,7 @@ public Dimension getMaximumSize() {
533540
534541 public void keyPressed () {
535542 if (key == ESC ) {
536- //ColorSelector.this.setVisible(false);
537- hideSelector ();
543+ ColorChooser .this .hide ();
538544 // don't quit out of processing
539545 // http://dev.processing.org/bugs/show_bug.cgi?id=1006
540546 key = 0 ;
@@ -604,8 +610,7 @@ public Dimension getMaximumSize() {
604610
605611 public void keyPressed () {
606612 if (key == ESC ) {
607- //ColorSelector.this.setVisible(false);
608- hideSelector ();
613+ ColorChooser .this .hide ();
609614 // don't quit out of processing
610615 // http://dev.processing.org/bugs/show_bug.cgi?id=1006
611616 key = 0 ;
0 commit comments