Beispiel #1
0
  /**
   * Sets the selected color of this panel.
   *
   * <p>If this panel is in RED, GREEN, or BLUE mode, then this method converts these values to RGB
   * coordinates and calls <code>setRGB</code>.
   *
   * <p>This method may regenerate the graphic if necessary.
   *
   * @param h the hue value of the selected color.
   * @param s the saturation value of the selected color.
   * @param b the brightness value of the selected color.
   */
  public void setHSB(float h, float s, float b) {
    if (Float.isInfinite(h) || Float.isNaN(h))
      throw new IllegalArgumentException("The hue value (" + h + ") is not a valid number.");
    // hue is cyclic, so it can be any value:
    while (h < 0) h++;
    while (h > 1) h--;

    if (s < 0 || s > 1)
      throw new IllegalArgumentException("The saturation value (" + s + ") must be between [0,1]");
    if (b < 0 || b > 1)
      throw new IllegalArgumentException("The brightness value (" + b + ") must be between [0,1]");

    if (hue != h || sat != s || bri != b) {
      if (mode == ColorPicker.HUE || mode == ColorPicker.BRI || mode == ColorPicker.SAT) {
        float lastHue = hue;
        float lastBri = bri;
        float lastSat = sat;
        hue = h;
        sat = s;
        bri = b;
        if (mode == ColorPicker.HUE) {
          if (lastHue != hue) {
            regenerateImage();
          }
        } else if (mode == ColorPicker.SAT) {
          if (lastSat != sat) {
            regenerateImage();
          }
        } else if (mode == ColorPicker.BRI) {
          if (lastBri != bri) {
            regenerateImage();
          }
        }
      } else {

        Color c = new Color(Color.HSBtoRGB(h, s, b));
        setRGB(c.getRed(), c.getGreen(), c.getBlue());
        return;
      }

      Color c = new Color(Color.HSBtoRGB(hue, sat, bri));
      red = c.getRed();
      green = c.getGreen();
      blue = c.getBlue();

      regeneratePoint();
      repaint();
      fireChangeListeners();
    }
  }
  /** {@collect.stats} Sets the values of the controls to reflect the color */
  private void setColor(Color newColor) {
    int red = newColor.getRed();
    int blue = newColor.getBlue();
    int green = newColor.getGreen();

    if (redSlider.getValue() != red) {
      redSlider.setValue(red);
    }
    if (greenSlider.getValue() != green) {
      greenSlider.setValue(green);
    }
    if (blueSlider.getValue() != blue) {
      blueSlider.setValue(blue);
    }

    if (((Integer) redField.getValue()).intValue() != red) redField.setValue(new Integer(red));
    if (((Integer) greenField.getValue()).intValue() != green)
      greenField.setValue(new Integer(green));
    if (((Integer) blueField.getValue()).intValue() != blue) blueField.setValue(new Integer(blue));
  }
  protected void buildChooser() {

    String redString = UIManager.getString("ColorChooser.rgbRedText");
    String greenString = UIManager.getString("ColorChooser.rgbGreenText");
    String blueString = UIManager.getString("ColorChooser.rgbBlueText");

    setLayout(new BorderLayout());
    Color color = getColorFromModel();

    JPanel enclosure = new JPanel();
    enclosure.setLayout(new SmartGridLayout(3, 3));
    enclosure.setInheritsPopupMenu(true);

    // The panel that holds the sliders

    add(enclosure, BorderLayout.CENTER);
    //        sliderPanel.setBorder(new LineBorder(Color.black));

    // The row for the red value
    JLabel l = new JLabel(redString);
    l.setDisplayedMnemonic(AbstractColorChooserPanel.getInt("ColorChooser.rgbRedMnemonic", -1));
    enclosure.add(l);
    redSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, color.getRed());
    redSlider.setMajorTickSpacing(85);
    redSlider.setMinorTickSpacing(17);
    redSlider.setPaintTicks(true);
    redSlider.setPaintLabels(true);
    redSlider.setInheritsPopupMenu(true);
    enclosure.add(redSlider);
    redField = new JSpinner(new SpinnerNumberModel(color.getRed(), minValue, maxValue, 1));
    l.setLabelFor(redSlider);
    redField.setInheritsPopupMenu(true);
    JPanel redFieldHolder = new JPanel(new CenterLayout());
    redFieldHolder.setInheritsPopupMenu(true);
    redField.addChangeListener(this);
    redFieldHolder.add(redField);
    enclosure.add(redFieldHolder);

    // The row for the green value
    l = new JLabel(greenString);
    l.setDisplayedMnemonic(AbstractColorChooserPanel.getInt("ColorChooser.rgbGreenMnemonic", -1));
    enclosure.add(l);
    greenSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, color.getGreen());
    greenSlider.setMajorTickSpacing(85);
    greenSlider.setMinorTickSpacing(17);
    greenSlider.setPaintTicks(true);
    greenSlider.setPaintLabels(true);
    greenSlider.setInheritsPopupMenu(true);
    enclosure.add(greenSlider);
    greenField = new JSpinner(new SpinnerNumberModel(color.getGreen(), minValue, maxValue, 1));
    l.setLabelFor(greenSlider);
    greenField.setInheritsPopupMenu(true);
    JPanel greenFieldHolder = new JPanel(new CenterLayout());
    greenFieldHolder.add(greenField);
    greenFieldHolder.setInheritsPopupMenu(true);
    greenField.addChangeListener(this);
    enclosure.add(greenFieldHolder);

    // The slider for the blue value
    l = new JLabel(blueString);
    l.setDisplayedMnemonic(AbstractColorChooserPanel.getInt("ColorChooser.rgbBlueMnemonic", -1));
    enclosure.add(l);
    blueSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, color.getBlue());
    blueSlider.setMajorTickSpacing(85);
    blueSlider.setMinorTickSpacing(17);
    blueSlider.setPaintTicks(true);
    blueSlider.setPaintLabels(true);
    blueSlider.setInheritsPopupMenu(true);
    enclosure.add(blueSlider);
    blueField = new JSpinner(new SpinnerNumberModel(color.getBlue(), minValue, maxValue, 1));
    l.setLabelFor(blueSlider);
    blueField.setInheritsPopupMenu(true);
    JPanel blueFieldHolder = new JPanel(new CenterLayout());
    blueFieldHolder.add(blueField);
    blueField.addChangeListener(this);
    blueFieldHolder.setInheritsPopupMenu(true);
    enclosure.add(blueFieldHolder);

    redSlider.addChangeListener(this);
    greenSlider.addChangeListener(this);
    blueSlider.addChangeListener(this);

    redSlider.putClientProperty("JSlider.isFilled", Boolean.TRUE);
    greenSlider.putClientProperty("JSlider.isFilled", Boolean.TRUE);
    blueSlider.putClientProperty("JSlider.isFilled", Boolean.TRUE);
  }