@@ -80,15 +80,15 @@ public ColorChooser(Frame owner, boolean modal, Color initialColor, |
80 | 80 | Box rangeBox = new Box(BoxLayout.Y_AXIS); |
81 | 81 | rangeBox.setAlignmentY(0); |
82 | 82 | rangeBox.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); |
83 | | - rangeBox.add(range.getCanvas()); |
| 83 | + rangeBox.add(range); |
84 | 84 | box.add(rangeBox); |
85 | 85 | box.add(Box.createHorizontalStrut(10)); |
86 | 86 |
|
87 | 87 | slider = new ColorSlider(); |
88 | 88 | Box sliderBox = new Box(BoxLayout.Y_AXIS); |
89 | 89 | sliderBox.setAlignmentY(0); |
90 | 90 | sliderBox.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); |
91 | | - sliderBox.add(slider.getCanvas()); |
| 91 | + sliderBox.add(slider); |
92 | 92 | box.add(sliderBox); |
93 | 93 | box.add(Box.createHorizontalStrut(10)); |
94 | 94 |
|
@@ -106,10 +106,6 @@ public ColorChooser(Frame owner, boolean modal, Color initialColor, |
106 | 106 |
|
107 | 107 | window.pack(); |
108 | 108 | window.setResizable(false); |
109 | | - |
110 | | - // initialize once the components exist |
111 | | - range.init(); |
112 | | - slider.init(); |
113 | 109 |
|
114 | 110 | // Dimension size = getSize(); |
115 | 111 | // Dimension screen = Toolkit.getScreenSize(); |
@@ -236,8 +232,8 @@ public void insertUpdate(DocumentEvent e) { |
236 | 232 | updateRGB(Integer.parseInt(str, 16)); |
237 | 233 | updateHSB(); |
238 | 234 | } |
239 | | - range.redraw(); |
240 | | - slider.redraw(); |
| 235 | + range.repaint(); |
| 236 | + slider.repaint(); |
241 | 237 | //colorPanel.setBackground(new Color(red, green, blue)); |
242 | 238 | colorPanel.repaint(); |
243 | 239 | updating = false; |
@@ -486,58 +482,46 @@ protected JLabel createFixedLabel(String title) { |
486 | 482 | } |
487 | 483 |
|
488 | 484 |
|
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 { |
493 | 486 |
|
494 | | - int lastX, lastY; |
| 487 | + static final int WIDTH = 256; |
| 488 | + static final int HEIGHT = 256; |
495 | 489 |
|
| 490 | + private int lastX, lastY; |
496 | 491 |
|
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 | + }); |
507 | 499 |
|
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 | + }); |
511 | 506 |
|
512 | | - loadPixels(); |
513 | | - } |
| 507 | + addKeyListener(new KeyAdapter() { |
| 508 | + @Override |
| 509 | + public void keyPressed(KeyEvent e) { |
| 510 | + super.keyPressed(e); |
514 | 511 |
|
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(); |
521 | 514 | } |
522 | 515 | } |
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 | + }); |
532 | 517 | } |
533 | 518 |
|
534 | | - public void mouseDragged() { |
535 | | - updateMouse(); |
536 | | - } |
| 519 | + private void updateMouse(MouseEvent e) { |
| 520 | + int mouseX = e.getX(); |
| 521 | + int mouseY = e.getY(); |
537 | 522 |
|
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)) { |
541 | 525 | int nsaturation = (int) (100 * (mouseX / 255.0f)); |
542 | 526 | int nbrightness = 100 - ((int) (100 * (mouseY / 255.0f))); |
543 | 527 | saturationField.setText(String.valueOf(nsaturation)); |
@@ -548,102 +532,105 @@ public void updateMouse() { |
548 | 532 | } |
549 | 533 | } |
550 | 534 |
|
| 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 |
551 | 551 | public Dimension getPreferredSize() { |
552 | | - return new Dimension(WIDE, HIGH); |
| 552 | + return new Dimension(WIDTH, HEIGHT); |
553 | 553 | } |
554 | 554 |
|
| 555 | + @Override |
555 | 556 | public Dimension getMinimumSize() { |
556 | | - return new Dimension(WIDE, HIGH); |
| 557 | + return getPreferredSize(); |
557 | 558 | } |
558 | 559 |
|
| 560 | + @Override |
559 | 561 | 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(); |
570 | 563 | } |
571 | 564 | } |
572 | 565 |
|
573 | 566 |
|
574 | | - public class ColorSlider extends PApplet { |
| 567 | + public class ColorSlider extends JComponent { |
575 | 568 |
|
576 | | - static final int WIDE = 20; |
577 | | - static final int HIGH = 256; |
| 569 | + static final int WIDTH = 20; |
| 570 | + static final int HEIGHT = 256; |
578 | 571 |
|
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 | + }); |
599 | 579 |
|
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); |
607 | 584 | } |
608 | | - } |
609 | | - updatePixels(); |
610 | | - } |
| 585 | + }); |
611 | 586 |
|
612 | | - public void mousePressed() { |
613 | | - updateMouse(); |
614 | | - } |
| 587 | + addKeyListener(new KeyAdapter() { |
| 588 | + @Override |
| 589 | + public void keyPressed(KeyEvent e) { |
| 590 | + super.keyPressed(e); |
615 | 591 |
|
616 | | - public void mouseDragged() { |
617 | | - updateMouse(); |
| 592 | + if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { |
| 593 | + ColorChooser.this.hide(); |
| 594 | + } |
| 595 | + } |
| 596 | + }); |
618 | 597 | } |
619 | 598 |
|
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)) { |
623 | 605 | int nhue = 359 - (int) (359 * (mouseY / 255.0f)); |
624 | 606 | hueField.setText(String.valueOf(nhue)); |
625 | 607 | } |
626 | 608 | } |
627 | 609 |
|
| 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 | + |
628 | 624 | public Dimension getPreferredSize() { |
629 | | - return new Dimension(WIDE, HIGH); |
| 625 | + return new Dimension(WIDTH, HEIGHT); |
630 | 626 | } |
631 | 627 |
|
632 | 628 | public Dimension getMinimumSize() { |
633 | | - return new Dimension(WIDE, HIGH); |
| 629 | + return getPreferredSize(); |
634 | 630 | } |
635 | 631 |
|
636 | 632 | 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(); |
647 | 634 | } |
648 | 635 | } |
649 | 636 |
|
|