📄 defaulthsbchooserpanel.java
字号:
slider.setValue(((Number) hSpinner.getValue()).intValue()); if (! handlingMouse) { gradientPoint.x = (int) ((1 - ((Number) sSpinner.getValue()).intValue() / 100f) * imgWidth); gradientPoint.y = (int) ((1 - ((Number) bSpinner.getValue()).intValue() / 100f) * imgHeight); } break; case SLOCKED: if (slider != null) slider.setValue(((Number) sSpinner.getValue()).intValue()); if (! handlingMouse) { gradientPoint.x = (int) (((Number) hSpinner.getValue()).intValue() / 360f * imgWidth); gradientPoint.y = (int) ((1 - ((Number) bSpinner.getValue()).intValue() / 100f) * imgHeight); } break; case BLOCKED: if (slider != null) slider.setValue(((Number) bSpinner.getValue()).intValue()); if (! handlingMouse) { gradientPoint.x = (int) (((Number) hSpinner.getValue()).intValue() / 360f * imgWidth); gradientPoint.y = (int) ((1 - ((Number) sSpinner.getValue()).intValue() / 100f) * imgHeight); } break; } internalChange = false; if (! handlingMouse && slider != null && ! slider.getValueIsAdjusting()) updateImage(); if (! handlingMouse || locked != HLOCKED) updateTrack(); updateTextFields(); } /** * This method builds the DefaultHSBChooserPanel. */ protected void buildChooser() { setLayout(new BorderLayout()); add(buildRightPanel(), BorderLayout.EAST); JPanel container = new JPanel(); container.setLayout(new BorderLayout()); gradientPanel = new JPanel() { public Dimension getPreferredSize() { return new Dimension(imgWidth, imgHeight); } public void paint(Graphics g) { if (gradientImage != null) g.drawImage(gradientImage, 0, 0, this); Color saved = g.getColor(); g.setColor(Color.WHITE); g.drawOval(gradientPoint.x - 3, gradientPoint.y - 3, 6, 6); g.setColor(saved); } }; MouseAdapter ml = new MainGradientMouseListener(); gradientPanel.addMouseListener(ml); gradientPanel.addMouseMotionListener((MouseMotionListener) ml); trackPanel = new JPanel() { public Dimension getPreferredSize() { return new Dimension(trackWidth, imgHeight); } public void paint(Graphics g) { if (trackImage != null) g.drawImage(trackImage, 0, 0, this); } }; slider = new JSlider(); slider.setPaintTrack(false); slider.setPaintTicks(false); slider.setOrientation(SwingConstants.VERTICAL); updateSlider(); container.add(gradientPanel, BorderLayout.WEST); container.add(slider, BorderLayout.CENTER); container.add(trackPanel, BorderLayout.EAST); add(container, BorderLayout.WEST); slider.addChangeListener(new SliderChangeListener()); repaint(); } /** * This method uninstalls the DefaultHSBPanel. * * @param chooser The JColorChooser to remove this panel from. */ public void uninstallChooserPanel(JColorChooser chooser) { trackImage = null; gradientImage = null; gradientPanel = null; slider = null; hSpinner = null; sSpinner = null; bSpinner = null; hRadio = null; sRadio = null; bRadio = null; removeAll(); super.uninstallChooserPanel(chooser); } /** * This helper method creates the right side panel (the panel with the * Spinners and TextFields). * * @return The right side panel. */ private Container buildRightPanel() { JPanel container = new JPanel(); container.setLayout(new GridLayout(6, 2)); hRadio = new JRadioButton("H"); sRadio = new JRadioButton("S"); bRadio = new JRadioButton("B"); ButtonGroup group = new ButtonGroup(); group.add(hRadio); group.add(sRadio); group.add(bRadio); hSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 359, 1)); sSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1)); bSpinner = new JSpinner(new SpinnerNumberModel(100, 0, 100, 1)); hSpinner.setEnabled(false); sSpinner.setEnabled(false); bSpinner.setEnabled(false); ChangeListener cl = new RadioStateListener(); ChangeListener scroll = new ImageScrollListener(); hRadio.addChangeListener(cl); sRadio.addChangeListener(cl); bRadio.addChangeListener(cl); hSpinner.addChangeListener(scroll); sSpinner.addChangeListener(scroll); bSpinner.addChangeListener(scroll); hRadio.setSelected(true); container.add(hRadio); container.add(hSpinner); container.add(sRadio); container.add(sSpinner); container.add(bRadio); container.add(bSpinner); rFull = new JLabel("red full"); gFull = new JLabel("green full"); bFull = new JLabel("blue full"); container.add(R); container.add(rFull); container.add(G); container.add(gFull); container.add(B); container.add(bFull); return container; } /** * This method returns the small display icon. * * @return The small display icon. */ public Icon getSmallDisplayIcon() { return null; } /** * This method returns the large display icon. * * @return The large display icon. */ public Icon getLargeDisplayIcon() { return null; } /** * This method paints the chooser panel. * * @param g The graphics object to paint with. */ public void paint(Graphics g) { super.paint(g); } /** * This method updates the gradient image with a new one taking the Hue * value as the constant. */ private void updateHLockImage() { int index = 0; int[] pix = new int[imgWidth * imgHeight]; float hValue = ((Number) hSpinner.getValue()).intValue() / 360f; for (int j = 0; j < imgHeight; j++) for (int i = 0; i < imgWidth; i++) pix[index++] = Color.HSBtoRGB(hValue, (imgWidth - i * 1f) / imgWidth, (imgHeight - j * 1f) / imgHeight) | (255 << 24); gradientImage = createImage(new MemoryImageSource(imgWidth, imgHeight, pix, 0, imgWidth)); } /** * This method updates the gradient image with a new one taking the * Brightness value as the constant. */ private void updateBLockImage() { int[] pix = new int[imgWidth * imgHeight]; float bValue = ((Number) bSpinner.getValue()).intValue() / 100f; int index = 0; for (int j = 0; j < imgHeight; j++) for (int i = 0; i < imgWidth; i++) pix[index++] = Color.HSBtoRGB(i * 1f / imgWidth, (imgHeight - j * 1f) / imgHeight, bValue) | (255 << 24); gradientImage = createImage(new MemoryImageSource(imgWidth, imgHeight, pix, 0, imgWidth)); } /** * This method updates the gradient image with a new one taking the * Saturation value as the constant. */ private void updateSLockImage() { int[] pix = new int[imgWidth * imgHeight]; float sValue = ((Number) sSpinner.getValue()).intValue() / 100f; int index = 0; for (int j = 0; j < imgHeight; j++) for (int i = 0; i < imgWidth; i++) pix[index++] = Color.HSBtoRGB(i * 1f / imgWidth, sValue, (imgHeight - j * 1f) / imgHeight) | (255 << 24); gradientImage = createImage(new MemoryImageSource(imgWidth, imgHeight, pix, 0, imgWidth)); } /** * This method calls the appropriate method to update the gradient image * depending on which HSB value is constant. * This is package-private to avoid an accessor method. */ void updateImage() { switch (locked) { case HLOCKED: updateHLockImage(); break; case SLOCKED: updateSLockImage(); break; case BLOCKED: updateBLockImage(); break; } } /** * This method updates the TextFields with the correct RGB values. */ private void updateTextFields() { int c = getColorSelectionModel().getSelectedColor().getRGB(); rFull.setText("" + (c >> 16 & 0xff)); gFull.setText("" + (c >> 8 & 0xff)); bFull.setText("" + (c & 0xff)); repaint(); } /** * This method updates the slider in response to making a different HSB * property the constant. * This is package-private to avoid an accessor method. */ void updateSlider() { if (slider == null) return; slider.setMinimum(0); if (locked == HLOCKED) { slider.setMaximum(359); ; slider.setValue(((Number) hSpinner.getValue()).intValue()); slider.setInverted(true); } else { slider.setMaximum(100); slider.setInverted(false); if (sRadio.isSelected()) slider.setValue(((Number) sSpinner.getValue()).intValue()); else slider.setValue(((Number) bSpinner.getValue()).intValue()); } repaint(); } /** * This method updates the track gradient image depending on which HSB * property is constant. * This is package-private to avoid an accessor method. */ void updateTrack() { switch (locked) { case HLOCKED: updateHTrack(); break; case SLOCKED: updateSTrack(); break; case BLOCKED: updateBTrack(); break; } } /** * This method updates the track gradient image if the Hue value is allowed * to change (according to the JRadioButtons). */ private void updateHTrack() { int trackIndex = 0; int[] trackPix = new int[trackWidth * imgHeight]; for (int j = 0; j < imgHeight; j++) for (int i = 0; i < trackWidth; i++) trackPix[trackIndex++] = Color.HSBtoRGB(j * 1f / imgHeight, 1f, 1f) | (255 << 24); trackImage = createImage(new MemoryImageSource(trackWidth, imgHeight, trackPix, 0, trackWidth)); } /** * This method updates the track gradient image if the Saturation value is * allowed to change (according to the JRadioButtons). */ private void updateSTrack() { int[] trackPix = new int[trackWidth * imgHeight]; float hValue = ((Number) hSpinner.getValue()).intValue() / 360f; float bValue = ((Number) bSpinner.getValue()).intValue() / 100f; int trackIndex = 0; for (int j = 0; j < imgHeight; j++) for (int i = 0; i < trackWidth; i++) trackPix[trackIndex++] = Color.HSBtoRGB(hValue, (imgHeight - j * 1f) / imgHeight, bValue) | (255 << 24); trackImage = createImage(new MemoryImageSource(trackWidth, imgHeight, trackPix, 0, trackWidth)); } /** * This method updates the track gradient image if the Brightness value is * allowed to change (according to the JRadioButtons). */ private void updateBTrack() { int[] trackPix = new int[trackWidth * imgHeight]; float hValue = ((Number) hSpinner.getValue()).intValue() / 360f; float sValue = ((Number) sSpinner.getValue()).intValue() / 100f; int trackIndex = 0; for (int j = 0; j < imgHeight; j++) for (int i = 0; i < trackWidth; i++) trackPix[trackIndex++] = Color.HSBtoRGB(hValue, sValue, (imgHeight - j * 1f) / imgHeight) | (255 << 24); trackImage = createImage(new MemoryImageSource(trackWidth, imgHeight, trackPix, 0, trackWidth)); } /** * This method returns the HSB values for the currently selected color. * * @return The HSB values for the currently selected color. */ private float[] getHSBValues() { Color c = getColorFromModel(); float[] f = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null); return f; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -