1 7 8 package javax.swing.colorchooser; 9 10 import javax.swing.*; 11 import javax.swing.event.*; 12 import java.awt.*; 13 14 32 class DefaultRGBChooserPanel extends AbstractColorChooserPanel implements ChangeListener { 33 34 protected JSlider redSlider; 35 protected JSlider greenSlider; 36 protected JSlider blueSlider; 37 protected JSpinner redField; 38 protected JSpinner blueField; 39 protected JSpinner greenField; 40 41 private final int minValue = 0; 42 private final int maxValue = 255; 43 44 private boolean isAdjusting = false; 46 public DefaultRGBChooserPanel() { 47 super(); 48 } 49 50 53 private void setColor( Color newColor ) { 54 int red = newColor.getRed(); 55 int blue = newColor.getBlue(); 56 int green = newColor.getGreen(); 57 58 if (redSlider.getValue() != red) { 59 redSlider.setValue(red); 60 } 61 if (greenSlider.getValue() != green) { 62 greenSlider.setValue(green); 63 } 64 if (blueSlider.getValue() != blue) { 65 blueSlider.setValue(blue); 66 } 67 68 if (((Integer )redField.getValue()).intValue() != red) 69 redField.setValue(new Integer (red)); 70 if (((Integer )greenField.getValue()).intValue() != green) 71 greenField.setValue(new Integer (green)); 72 if (((Integer )blueField.getValue()).intValue() != blue ) 73 blueField.setValue(new Integer (blue)); 74 } 75 76 public String getDisplayName() { 77 return UIManager.getString("ColorChooser.rgbNameText"); 78 } 79 80 99 public int getMnemonic() { 100 return getInt("ColorChooser.rgbMnemonic", -1); 101 } 102 103 127 public int getDisplayedMnemonicIndex() { 128 return getInt("ColorChooser.rgbDisplayedMnemonicIndex", -1); 129 } 130 131 public Icon getSmallDisplayIcon() { 132 return null; 133 } 134 135 public Icon getLargeDisplayIcon() { 136 return null; 137 } 138 139 143 public void installChooserPanel(JColorChooser enclosingChooser) { 144 super.installChooserPanel(enclosingChooser); 145 } 146 147 protected void buildChooser() { 148 149 String redString = UIManager.getString("ColorChooser.rgbRedText"); 150 String greenString = UIManager.getString("ColorChooser.rgbGreenText"); 151 String blueString = UIManager.getString("ColorChooser.rgbBlueText"); 152 153 setLayout( new BorderLayout() ); 154 Color color = getColorFromModel(); 155 156 157 JPanel enclosure = new JPanel(); 158 enclosure.setLayout( new SmartGridLayout ( 3, 3 ) ); 159 160 162 add( enclosure, BorderLayout.CENTER ); 163 165 JLabel l = new JLabel(redString); 167 l.setDisplayedMnemonic(AbstractColorChooserPanel.getInt("ColorChooser.rgbRedMnemonic", -1)); 168 enclosure.add(l); 169 redSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, color.getRed()); 170 redSlider.setMajorTickSpacing( 85 ); 171 redSlider.setMinorTickSpacing( 17 ); 172 redSlider.setPaintTicks( true ); 173 redSlider.setPaintLabels( true ); 174 enclosure.add( redSlider ); 175 redField = new JSpinner( 176 new SpinnerNumberModel(color.getRed(), minValue, maxValue, 1)); 177 l.setLabelFor(redSlider); 178 JPanel redFieldHolder = new JPanel(new CenterLayout ()); 179 redField.addChangeListener(this); 180 redFieldHolder.add(redField); 181 enclosure.add(redFieldHolder); 182 183 184 l = new JLabel(greenString); 186 l.setDisplayedMnemonic(AbstractColorChooserPanel.getInt("ColorChooser.rgbGreenMnemonic", -1)); 187 enclosure.add(l); 188 greenSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, color.getGreen()); 189 greenSlider.setMajorTickSpacing( 85 ); 190 greenSlider.setMinorTickSpacing( 17 ); 191 greenSlider.setPaintTicks( true ); 192 greenSlider.setPaintLabels( true ); 193 enclosure.add(greenSlider); 194 greenField = new JSpinner( 195 new SpinnerNumberModel(color.getGreen(), minValue, maxValue, 1)); 196 l.setLabelFor(greenSlider); 197 JPanel greenFieldHolder = new JPanel(new CenterLayout ()); 198 greenFieldHolder.add(greenField); 199 greenField.addChangeListener(this); 200 enclosure.add(greenFieldHolder); 201 202 l = new JLabel(blueString); 204 l.setDisplayedMnemonic(AbstractColorChooserPanel.getInt("ColorChooser.rgbBlueMnemonic", -1)); 205 enclosure.add(l); 206 blueSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, color.getBlue()); 207 blueSlider.setMajorTickSpacing( 85 ); 208 blueSlider.setMinorTickSpacing( 17 ); 209 blueSlider.setPaintTicks( true ); 210 blueSlider.setPaintLabels( true ); 211 enclosure.add(blueSlider); 212 blueField = new JSpinner( 213 new SpinnerNumberModel(color.getBlue(), minValue, maxValue, 1)); 214 l.setLabelFor(blueSlider); 215 JPanel blueFieldHolder = new JPanel(new CenterLayout ()); 216 blueFieldHolder.add(blueField); 217 blueField.addChangeListener(this); 218 enclosure.add(blueFieldHolder); 219 220 redSlider.addChangeListener( this ); 221 greenSlider.addChangeListener( this ); 222 blueSlider.addChangeListener( this ); 223 224 redSlider.putClientProperty("JSlider.isFilled", Boolean.TRUE); 225 greenSlider.putClientProperty("JSlider.isFilled", Boolean.TRUE); 226 blueSlider.putClientProperty("JSlider.isFilled", Boolean.TRUE); 227 } 228 229 public void uninstallChooserPanel(JColorChooser enclosingChooser) { 230 super.uninstallChooserPanel(enclosingChooser); 231 removeAll(); 232 } 233 234 public void updateChooser() { 235 if (!isAdjusting) { 236 isAdjusting = true; 237 238 setColor(getColorFromModel()); 239 240 isAdjusting = false; 241 } 242 } 243 244 public void stateChanged( ChangeEvent e ) { 245 if ( e.getSource() instanceof JSlider && !isAdjusting) { 246 247 int red = redSlider.getValue(); 248 int green = greenSlider.getValue(); 249 int blue = blueSlider.getValue() ; 250 Color color = new Color (red, green, blue); 251 252 getColorSelectionModel().setSelectedColor(color); 253 } else if (e.getSource() instanceof JSpinner && !isAdjusting) { 254 255 int red = ((Integer )redField.getValue()).intValue(); 256 int green = ((Integer )greenField.getValue()).intValue(); 257 int blue = ((Integer )blueField.getValue()).intValue(); 258 Color color = new Color (red, green, blue); 259 260 getColorSelectionModel().setSelectedColor(color); 261 } 262 } 263 264 } 265 266 267 | Popular Tags |