@@ -80,15 +80,15 @@ public ColorChooser(Frame owner, boolean modal, Color initialColor,
8080 Box rangeBox = new Box (BoxLayout .Y_AXIS );
8181 rangeBox .setAlignmentY (0 );
8282 rangeBox .setBorder (BorderFactory .createBevelBorder (BevelBorder .LOWERED ));
83- rangeBox .add (range . getCanvas () );
83+ rangeBox .add (range );
8484 box .add (rangeBox );
8585 box .add (Box .createHorizontalStrut (10 ));
8686
8787 slider = new ColorSlider ();
8888 Box sliderBox = new Box (BoxLayout .Y_AXIS );
8989 sliderBox .setAlignmentY (0 );
9090 sliderBox .setBorder (BorderFactory .createBevelBorder (BevelBorder .LOWERED ));
91- sliderBox .add (slider . getCanvas () );
91+ sliderBox .add (slider );
9292 box .add (sliderBox );
9393 box .add (Box .createHorizontalStrut (10 ));
9494
@@ -106,10 +106,6 @@ public ColorChooser(Frame owner, boolean modal, Color initialColor,
106106
107107 window .pack ();
108108 window .setResizable (false );
109-
110- // initialize once the components exist
111- range .init ();
112- slider .init ();
113109
114110// Dimension size = getSize();
115111// Dimension screen = Toolkit.getScreenSize();
@@ -236,8 +232,8 @@ public void insertUpdate(DocumentEvent e) {
236232 updateRGB (Integer .parseInt (str , 16 ));
237233 updateHSB ();
238234 }
239- range .redraw ();
240- slider .redraw ();
235+ range .repaint ();
236+ slider .repaint ();
241237 //colorPanel.setBackground(new Color(red, green, blue));
242238 colorPanel .repaint ();
243239 updating = false ;
@@ -486,58 +482,46 @@ protected JLabel createFixedLabel(String title) {
486482 }
487483
488484
489- public class ColorRange extends PApplet {
490-
491- static final int WIDE = 256 ;
492- static final int HIGH = 256 ;
485+ public class ColorRange extends JComponent {
493486
494- int lastX , lastY ;
487+ static final int WIDTH = 256 ;
488+ static final int HEIGHT = 256 ;
495489
490+ private int lastX , lastY ;
496491
497- public int sketchWidth () {
498- return WIDE ;
499- }
500-
501- public int sketchHeight () {
502- return HIGH ;
503- }
504-
505- public void setup () {
506- noLoop ();
492+ public ColorRange () {
493+ addMouseListener (new MouseAdapter () {
494+ @ Override
495+ public void mousePressed (MouseEvent e ) {
496+ updateMouse (e );
497+ }
498+ });
507499
508- colorMode (HSB , 360 , 256 , 256 );
509- noFill ();
510- rectMode (CENTER );
500+ addMouseMotionListener (new MouseMotionAdapter () {
501+ @ Override
502+ public void mouseDragged (MouseEvent e ) {
503+ updateMouse (e );
504+ }
505+ });
511506
512- loadPixels ();
513- }
507+ addKeyListener (new KeyAdapter () {
508+ @ Override
509+ public void keyPressed (KeyEvent e ) {
510+ super .keyPressed (e );
514511
515- public void draw () {
516- if (width == WIDE && height == HIGH ) {
517- int index = 0 ;
518- for (int j = 0 ; j < 256 ; j ++) {
519- for (int i = 0 ; i < 256 ; i ++) {
520- pixels [index ++] = color (hue , i , 255 - j );
512+ if (e .getKeyCode () == KeyEvent .VK_ESCAPE ) {
513+ ColorChooser .this .hide ();
521514 }
522515 }
523-
524- updatePixels ();
525- stroke ((brightness > 50 ) ? 0 : 255 );
526- rect (lastX , lastY , 9 , 9 );
527- }
528- }
529-
530- public void mousePressed () {
531- updateMouse ();
516+ });
532517 }
533518
534- public void mouseDragged ( ) {
535- updateMouse ();
536- }
519+ private void updateMouse ( MouseEvent e ) {
520+ int mouseX = e . getX ();
521+ int mouseY = e . getY ();
537522
538- public void updateMouse () {
539- if ((mouseX >= 0 ) && (mouseX < 256 ) &&
540- (mouseY >= 0 ) && (mouseY < 256 )) {
523+ if ((mouseX >= 0 ) && (mouseX < WIDTH ) &&
524+ (mouseY >= 0 ) && (mouseY < HEIGHT )) {
541525 int nsaturation = (int ) (100 * (mouseX / 255.0f ));
542526 int nbrightness = 100 - ((int ) (100 * (mouseY / 255.0f )));
543527 saturationField .setText (String .valueOf (nsaturation ));
@@ -548,102 +532,105 @@ public void updateMouse() {
548532 }
549533 }
550534
535+ @ Override
536+ public void paintComponent (Graphics g ) {
537+ super .paintComponent (g );
538+
539+ for (int j = 0 ; j < WIDTH ; j ++) {
540+ for (int i = 0 ; i < HEIGHT ; i ++) {
541+ g .setColor (Color .getHSBColor (hue / 360f , i / 256f , (255 - j ) / 256f ));
542+ g .fillRect (i , j , 1 , 1 );
543+ }
544+ }
545+
546+ g .setColor ((brightness > 50 ) ? Color .BLACK : Color .WHITE );
547+ g .drawRect (lastX - 5 , lastY - 5 , 10 , 10 );
548+ }
549+
550+ @ Override
551551 public Dimension getPreferredSize () {
552- return new Dimension (WIDE , HIGH );
552+ return new Dimension (WIDTH , HEIGHT );
553553 }
554554
555+ @ Override
555556 public Dimension getMinimumSize () {
556- return new Dimension ( WIDE , HIGH );
557+ return getPreferredSize ( );
557558 }
558559
560+ @ Override
559561 public Dimension getMaximumSize () {
560- return new Dimension (WIDE , HIGH );
561- }
562-
563- public void keyPressed () {
564- if (key == ESC ) {
565- ColorChooser .this .hide ();
566- // don't quit out of processing
567- // http://dev.processing.org/bugs/show_bug.cgi?id=1006
568- key = 0 ;
569- }
562+ return getPreferredSize ();
570563 }
571564 }
572565
573566
574- public class ColorSlider extends PApplet {
567+ public class ColorSlider extends JComponent {
575568
576- static final int WIDE = 20 ;
577- static final int HIGH = 256 ;
569+ static final int WIDTH = 20 ;
570+ static final int HEIGHT = 256 ;
578571
579- public int sketchWidth () {
580- return WIDE ;
581- }
582-
583- public int sketchHeight () {
584- return HIGH ;
585- }
586-
587- public void setup () {
588- colorMode (HSB , 255 , 100 , 100 );
589- noLoop ();
590- loadPixels ();
591- }
592-
593- public void draw () {
594- // if ((g == null) || (g.pixels == null)) return;
595- if ((width != WIDE ) || (height < HIGH )) {
596- //System.out.println("bad size " + width + " " + height);
597- return ;
598- }
572+ public ColorSlider () {
573+ addMouseListener (new MouseAdapter () {
574+ @ Override
575+ public void mousePressed (MouseEvent e ) {
576+ updateMouse (e );
577+ }
578+ });
599579
600- int index = 0 ;
601- int sel = 255 - (int ) (255 * (hue / 359f ));
602- for (int j = 0 ; j < 256 ; j ++) {
603- int c = color (255 - j , 100 , 100 );
604- if (j == sel ) c = 0xFF000000 ;
605- for (int i = 0 ; i < WIDE ; i ++) {
606- g .pixels [index ++] = c ;
580+ addMouseMotionListener (new MouseMotionAdapter () {
581+ @ Override
582+ public void mouseDragged (MouseEvent e ) {
583+ updateMouse (e );
607584 }
608- }
609- updatePixels ();
610- }
585+ });
611586
612- public void mousePressed () {
613- updateMouse ();
614- }
587+ addKeyListener (new KeyAdapter () {
588+ @ Override
589+ public void keyPressed (KeyEvent e ) {
590+ super .keyPressed (e );
615591
616- public void mouseDragged () {
617- updateMouse ();
592+ if (e .getKeyCode () == KeyEvent .VK_ESCAPE ) {
593+ ColorChooser .this .hide ();
594+ }
595+ }
596+ });
618597 }
619598
620- public void updateMouse () {
621- if ((mouseX >= 0 ) && (mouseX < 256 ) &&
622- (mouseY >= 0 ) && (mouseY < 256 )) {
599+ private void updateMouse (MouseEvent e ) {
600+ int mouseX = e .getX ();
601+ int mouseY = e .getY ();
602+
603+ if ((mouseX >= 0 ) && (mouseX < WIDTH ) &&
604+ (mouseY >= 0 ) && (mouseY < HEIGHT )) {
623605 int nhue = 359 - (int ) (359 * (mouseY / 255.0f ));
624606 hueField .setText (String .valueOf (nhue ));
625607 }
626608 }
627609
610+ public void paintComponent (Graphics g ) {
611+ super .paintComponent (g );
612+
613+ int sel = 255 - (int ) (255 * (hue / 359.0 ));
614+ for (int j = 0 ; j < HEIGHT ; j ++) {
615+ Color color = Color .getHSBColor ((255 - j ) / 256f , 1 , 1 );
616+ if (j == sel ) {
617+ color = Color .BLACK ;
618+ }
619+ g .setColor (color );
620+ g .drawRect (0 , j , WIDTH , 1 );
621+ }
622+ }
623+
628624 public Dimension getPreferredSize () {
629- return new Dimension (WIDE , HIGH );
625+ return new Dimension (WIDTH , HEIGHT );
630626 }
631627
632628 public Dimension getMinimumSize () {
633- return new Dimension ( WIDE , HIGH );
629+ return getPreferredSize ( );
634630 }
635631
636632 public Dimension getMaximumSize () {
637- return new Dimension (WIDE , HIGH );
638- }
639-
640- public void keyPressed () {
641- if (key == ESC ) {
642- ColorChooser .this .hide ();
643- // don't quit out of processing
644- // http://dev.processing.org/bugs/show_bug.cgi?id=1006
645- key = 0 ;
646- }
633+ return getPreferredSize ();
647634 }
648635 }
649636
0 commit comments