A widget to manipulate an RGBA colour.
//package com.ryanm.util.swing;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.LinkedList;
import java.util.List;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
* A widget to manipulate an RGBA colour.
*
* @author ryanm
*/
public class RGBAColourChooser extends Box implements ChangeListener
{
private JLabel redLabel = new JLabel( "R" );
private JSlider redSlider = new JSlider( 0, 255 );
private JLabel greenLabel = new JLabel( "G" );
private JSlider greenSlider = new JSlider( 0, 255 );
private JLabel blueLabel = new JLabel( "B" );
private JSlider blueSlider = new JSlider( 0, 255 );
private JLabel alphaLabel = new JLabel( "A" );
private JSlider alphaSlider = new JSlider( 0, 255 );
private Box sliderBox;
private Color oldValue = Color.WHITE;
private List<ChangeListener> listeners = new LinkedList<ChangeListener>();
private JPanel previewer = new JPanel() {
@Override
public void paint( Graphics g )
{
int width = getWidth();
int height = getHeight();
g.setColor( Color.white );
g.fillRect( 0, 0, width, height );
g.setColor( Color.black );
g.fillRect( 0, height / 2, width / 2, height / 2 );
g.fillRect( width / 2, 0, width / 2, height / 2 );
g.setColor( oldValue );
g.fillRect( width / 4, height / 4, width / 2, height / 2 );
}
@Override
public Dimension getPreferredSize()
{
int size = sliderBox.getHeight();
size = size == 0 ? 64 : size;
return new Dimension( size, size );
}
@Override
public Dimension getMaximumSize()
{
return getPreferredSize();
}
};
/**
* Constructs a new RGBAColourChooser
*/
public RGBAColourChooser()
{
super( BoxLayout.X_AXIS );
Box rb = new Box( BoxLayout.X_AXIS );
rb.add( redLabel );
rb.add( redSlider );
Box gb = new Box( BoxLayout.X_AXIS );
gb.add( greenLabel );
gb.add( greenSlider );
Box bb = new Box( BoxLayout.X_AXIS );
bb.add( blueLabel );
bb.add( blueSlider );
Box ab = new Box( BoxLayout.X_AXIS );
ab.add( alphaLabel );
ab.add( alphaSlider );
redSlider.setValue( oldValue.getRed() );
greenSlider.setValue( oldValue.getGreen() );
blueSlider.setValue( oldValue.getBlue() );
alphaSlider.setValue( oldValue.getAlpha() );
redSlider.addChangeListener( this );
greenSlider.addChangeListener( this );
blueSlider.addChangeListener( this );
alphaSlider.addChangeListener( this );
redSlider.setToolTipText( String.valueOf( redSlider.getValue() ) );
greenSlider.setToolTipText( String.valueOf( greenSlider.getValue() ) );
blueSlider.setToolTipText( String.valueOf( blueSlider.getValue() ) );
alphaSlider.setToolTipText( String.valueOf( alphaSlider.getValue() ) );
sliderBox = new Box( BoxLayout.Y_AXIS );
sliderBox.add( rb );
sliderBox.add( gb );
sliderBox.add( bb );
sliderBox.add( ab );
add( previewer );
add( Box.createHorizontalStrut( 5 ) );
add( sliderBox );
}
/**
* Sets the widget's selected colour
*
* @param color
* The new colour
*/
public void setColour( Color color )
{
if( !oldValue.equals( color ) )
{
// copy the values
oldValue = color;
redSlider.removeChangeListener( this );
greenSlider.removeChangeListener( this );
blueSlider.removeChangeListener( this );
alphaSlider.removeChangeListener( this );
redSlider.setValue( oldValue.getRed() );
greenSlider.setValue( oldValue.getGreen() );
blueSlider.setValue( oldValue.getBlue() );
alphaSlider.setValue( oldValue.getAlpha() );
redSlider.setToolTipText( String.valueOf( redSlider.getValue() ) );
greenSlider.setToolTipText( String.valueOf( greenSlider.getValue() ) );
blueSlider.setToolTipText( String.valueOf( blueSlider.getValue() ) );
alphaSlider.setToolTipText( String.valueOf( alphaSlider.getValue() ) );
redSlider.addChangeListener( this );
greenSlider.addChangeListener( this );
blueSlider.addChangeListener( this );
alphaSlider.addChangeListener( this );
previewer.repaint();
ChangeEvent ce = new ChangeEvent( this );
for( ChangeListener listener : listeners )
{
listener.stateChanged( ce );
}
}
}
/**
* Gets the widget's currently selected colour
*
* @return The current colour
*/
public Color getColour()
{
return oldValue;
}
@Override
public void stateChanged( ChangeEvent e )
{
int red = redSlider.getValue();
int green = greenSlider.getValue();
int blue = blueSlider.getValue();
int alpha = alphaSlider.getValue();
setColour( new Color( red, green, blue, alpha ) );
}
@Override
public void setEnabled( boolean b )
{
alphaLabel.setEnabled( b );
alphaSlider.setEnabled( b );
blueLabel.setEnabled( b );
blueSlider.setEnabled( b );
greenLabel.setEnabled( b );
greenSlider.setEnabled( b );
redLabel.setEnabled( b );
redSlider.setEnabled( b );
}
@Override
public boolean isEnabled()
{
return alphaSlider.isEnabled();
}
/**
* Adds a change listener to this widget. The ChangeListener will
* be appraised of any changes to the selected colour
*
* @param listener
* The listener to add
*/
public void addChangeListener( ChangeListener listener )
{
listeners.add( listener );
}
/**
* Removes a ChangeListener from this widget. The ChangeListener
* will no longer be appraised of changes to the selected colour
*
* @param listener
* The listener to remove
*/
public void removeChangeListener( ChangeListener listener )
{
listeners.remove( listener );
}
}
Related examples in the same category
1. | Color class is used to work with colors in Java 2D | | |
2. | Color Utilities: common color operations | | |
3. | Color Difference | | |
4. | Rainbow Color | | |
5. | XOR color | | |
6. | Color Gradient | | |
7. | Common color utilities | | |
8. | Drawing with Color | | |
9. | Color fading animation | | |
10. | 140 colors - defined for X Window System listed in O'Reilly html pocket reference 87pp | | |
11. | Color Util | | |
12. | Color Factory | | |
13. | An efficient color quantization algorithm | | |
14. | Utility for checking colors given either hexa or natural language string descriptions. | | |
15. | Derives a color by adding the specified offsets to the base color's hue, saturation, and brightness values | | |
16. | Map colors into names and vice versa. | | |
17. | Converts a given string into a color. | | |
18. | If the color is equal to one of the defined constant colors, that name is returned instead. | | |
19. | Converts the String representation of a color to an actual Color object. | | |
20. | Returns blue-yellow-red color scale | | |
21. | Returns green-yellow-red-black color scale | | |
22. | Returns black-red-yellow-green color scale | | |
23. | Returns color based on 0-9 scale ranging from green to yellow | | |
24. | Returns color based on 0-9 scale ranging from yellow to red | | |
25. | Returns color based on 0-9 scale ranging from black to green | | |
26. | Returns n-dimensional array of colors for given nx3 integer array of RGB values | | |
27. | Web color enum | | |
28. | Utility class for managing resources such as colors, fonts, images, etc. | | |
29. | Make a color transparent | | |
30. | Return a Color object given a string representation of it | | |
31. | Return a string representation of a color | | |
32. | Serializes a color to its HTML markup (e.g. "#ff0000" for red) | | |
33. | Parses a java.awt.Color from an HTML color string in the form '#RRGGBB' where RR, GG, and BB are the red, green, and blue bytes in hexadecimal form | | |
34. | Performs a somewhat subjective analysis of a color to determine how dark it looks to a user | | |
35. | Lightens a color by a given amount | | |
36. | Darkens a color by a given amount | | |
37. | Blend two colors | | |
38. | Utility for working with natively-ordered integer-packed RGBA-format colours. | | |
39. | HSV to RGB | | |