📄 defaulthsbchooserpanel.java
字号:
/* DefaultHSBChooserPanel.java -- Copyright (C) 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package javax.swing.colorchooser;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Container;import java.awt.Dimension;import java.awt.Graphics;import java.awt.GridLayout;import java.awt.Image;import java.awt.Point;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseMotionListener;import java.awt.image.MemoryImageSource;import javax.swing.AbstractButton;import javax.swing.ButtonGroup;import javax.swing.Icon;import javax.swing.JColorChooser;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JRadioButton;import javax.swing.JSlider;import javax.swing.JSpinner;import javax.swing.SpinnerNumberModel;import javax.swing.SwingConstants;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;/** * This is the Default HSB Panel displayed in the JColorChooser. */class DefaultHSBChooserPanel extends AbstractColorChooserPanel{ /** The gradient image displayed. * This is package-private to avoid an accessor method. */ transient Image gradientImage; /** The Panel that holds the gradient image. */ private transient JPanel gradientPanel; /** The track gradient image. * This is package-private to avoid an accessor method. */ transient Image trackImage; /** The panel that holds the track. */ private transient JPanel trackPanel; /** The slider for the locked HSB value. * This is package-private to avoid an accessor method. */ transient JSlider slider; /** The RadioButton that controls the Hue. * This is package-private to avoid an accessor method. */ transient JRadioButton hRadio; /** The RadioButton that controls the Saturation. * This is package-private to avoid an accessor method. */ transient JRadioButton sRadio; /** The RadioButton that controls the Brightness. * This is package-private to avoid an accessor method. */ transient JRadioButton bRadio; /** The JSpinner that controls the Hue. * This is package-private to avoid an accessor method. */ transient JSpinner hSpinner; /** The JSpinner that controls the Saturation. * This is package-private to avoid an accessor method. */ transient JSpinner sSpinner; /** The JSpinner that controls the Brightness. * This is package-private to avoid an accessor method. */ transient JSpinner bSpinner; /** The default width of the gradient image. */ private static final int imgWidth = 200; /** The default height of the gradient image. */ private static final int imgHeight = 200; /** The default width of the track gradient. */ private static final int trackWidth = 30; /** The JLabel for Red. */ private static final JLabel R = new JLabel("R"); /** The JLabel for Green. */ private static final JLabel G = new JLabel("G"); /** The JLabel for Blue. */ private static final JLabel B = new JLabel("B"); // FIXME: Should be textfields. /** The JLabel that displays the value of Red. */ private transient JLabel rFull; /** The JLabel that displays the value of Green. */ private transient JLabel gFull; /** The JLabel that displays the value of Blue. */ private transient JLabel bFull; /** The point that is displayed in the gradient image. * Package-private to avoid an accessor method. */ transient Point gradientPoint = new Point(); /** * This indicates that the change to the slider or point is triggered * internally. * This is package-private to avoid an accessor method. */ transient boolean internalChange = false; /** This indicates that the change to the spinner is triggered * internally. * This is package-private to avoid an accessor method. */ transient boolean spinnerTrigger = false; /** This int identifies which spinner is currently locked. * This is package-private to avoid an accessor method. */ transient int locked = -1; /** This value indicates that the Hue spinner is locked. */ static final int HLOCKED = 0; /** This value indicates that the Saturation spinner is locked. */ static final int SLOCKED = 1; /** This value indicates that the Brightness spinner is locked. */ static final int BLOCKED = 2; /** * This method indicates that the mouse event is in the process of being * handled. * This is package-private to avoid an accessor method. */ transient boolean handlingMouse; /** * This helper class handles mouse events on the gradient image. */ class MainGradientMouseListener extends MouseAdapter implements MouseMotionListener { /** * This method is called when the mouse is pressed over the gradient * image. The JColorChooser is then updated with new HSB values. * * @param e The MouseEvent. */ public void mousePressed(MouseEvent e) { gradientPoint = e.getPoint(); update(e.getPoint()); } /** * This method is called when the mouse is dragged over the gradient * image. The JColorChooser is then updated with the new HSB values. * * @param e The MouseEvent. */ public void mouseDragged(MouseEvent e) { Point p = e.getPoint(); if (p.x < 0 || p.y < 0 || p.y > imgHeight || p.x > imgWidth) return; gradientPoint = p; update(p); } /** * This method is called when the mouse is moved over the gradient image. * * @param e The MouseEvent. */ public void mouseMoved(MouseEvent e) { // Do nothing. } /** * This method updates the JColorChooser with the new values. * * @param p The Point where the MouseEvent occurred. */ private void update(Point p) { handlingMouse = true; if (hSpinner.isEnabled()) updateH(p); else if (sSpinner.isEnabled()) updateS(p); else updateB(p); handlingMouse = false; } /** * This method updates the SB values if Hue is locked. * * @param p The point where the MouseEvent occurred. */ private void updateH(Point p) { float s = (imgWidth - p.x * 1f) / imgWidth; float b = (imgHeight - p.y * 1f) / imgHeight; // Avoid two changes to the model by changing internalChange to true. internalChange = true; sSpinner.setValue(new Integer((int) (s * 100))); internalChange = false; bSpinner.setValue(new Integer((int) (b * 100))); revalidate(); } /** * This method updates the HB values if Saturation is locked. * * @param p The point where the MouseEvent occurred. */ private void updateS(Point p) { float h = p.x * 1f / imgWidth; float b = (imgHeight - p.y * 1f) / imgHeight; internalChange = true; hSpinner.setValue(new Integer((int) (h * 365))); internalChange = false; bSpinner.setValue(new Integer((int) (b * 100))); revalidate(); } /** * This method updates the HS values if Brightness is locked. * * @param p The point where the MouseEvent occurred. */ private void updateB(Point p) { float h = p.x * 1f / imgWidth; float s = (imgHeight - p.y * 1f) / imgHeight; internalChange = true; hSpinner.setValue(new Integer((int) (h * 365))); internalChange = false; sSpinner.setValue(new Integer((int) (s * 100))); revalidate(); } } /** * This method listens for slider value changes. */ class SliderChangeListener implements ChangeListener { /** * This method is called when the slider value changes. It should change * the color of the JColorChooser. * * @param e The ChangeEvent. */ public void stateChanged(ChangeEvent e) { if (internalChange) return; Integer value = new Integer(slider.getValue()); switch (locked) { case HLOCKED: hSpinner.setValue(value); break; case SLOCKED: sSpinner.setValue(value); break; case BLOCKED: bSpinner.setValue(value); break; } } } /** * This helper class determines the active JSpinner. */ class RadioStateListener implements ChangeListener { /** * This method is called when there is a new JRadioButton that was * selected. As a result, it should activate the associated JSpinner. * * @param e The ChangeEvent. */ public void stateChanged(ChangeEvent e) { JSpinner change; if (e.getSource() == hRadio) { locked = HLOCKED; change = hSpinner; } else if (e.getSource() == sRadio) { locked = SLOCKED; change = sSpinner; } else { locked = BLOCKED; change = bSpinner; } change.setEnabled(((AbstractButton) e.getSource()).isSelected()); updateSlider(); updateTrack(); updateImage(); repaint(); } } /** * This class listens to the JSpinners for changes. */ class ImageScrollListener implements ChangeListener { /** * This method is called whenever one of the JSpinner values change. The * JColorChooser should be updated with the new HSB values. * * @param e The ChangeEvent. */ public void stateChanged(ChangeEvent e) { if (internalChange) return; float h = ((Number) hSpinner.getValue()).intValue() / 360f; float s = ((Number) sSpinner.getValue()).intValue() / 100f; float b = ((Number) bSpinner.getValue()).intValue() / 100f; spinnerTrigger = true; getColorSelectionModel().setSelectedColor(new Color(Color.HSBtoRGB(h, s, b))); spinnerTrigger = false; if (! handlingMouse && slider != null && ! slider.getValueIsAdjusting()) { updateImage(); updateTrack(); } repaint(); } } /** * Creates a new DefaultHSBChooserPanel object. */ DefaultHSBChooserPanel() { super(); } /** * This method returns the name displayed by the JColorChooser tab that * holds this panel. * * @return The name displayed in the JColorChooser tab. */ public String getDisplayName() { return "HSB"; } /** * This method updates the various components inside the HSBPanel (the * JSpinners, the JSlider, and the gradient image point) with updated * values when the JColorChooser color value changes. */ public void updateChooser() { Color c = getColorSelectionModel().getSelectedColor(); float[] hsbVals = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null); internalChange = true; if (! spinnerTrigger) { hSpinner.setValue(new Integer((int) (hsbVals[0] * 360))); sSpinner.setValue(new Integer((int) (hsbVals[1] * 100))); bSpinner.setValue(new Integer((int) (hsbVals[2] * 100))); } switch (locked) { case HLOCKED: if (slider != null)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -