|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.awt.Component java.awt.Container javax.swing.JComponent javax.swing.JPanel com.levigo.util.swing.colorpicker.ColorPicker
public class ColorPicker
This is a panel that offers a robust set of controls to pick a color.
This was originally intended to replace the JColorChooser
. To use this class to
create a color choosing dialog, simply call:
ColorPicker.showDialog(frame, originalColor);
However this panel is also resizable, and it can exist in other contexts. For example, you might
try the following panel:
ColorPicker picker = new ColorPicker(false, false);
picker.setPreferredSize(new Dimension(200,160));
picker.setMode(ColorPicker.HUE);
This will create a miniature color picker that still lets the user choose from every available
color, but it does not include all the buttons and numeric controls on the right side of the
panel. This might be ideal if you are working with limited space, or non-power-users who don't
need the RGB values of a color. The main()
method of this class demonstrates
possible ways you can customize a ColorPicker
component.
To listen to color changes to this panel, you can add a PropertyChangeListener
listening for changes to the SELECTED_COLOR_PROPERTY
. This will be triggered only
when the RGB value of the selected color changes.
To listen to opacity changes to this panel, use a PropertyChangeListener
listening
for changes to the OPACITY_PROPERTY
.
Nested Class Summary | |
---|---|
static class |
ColorPicker.Mode
|
Nested classes/interfaces inherited from class javax.swing.JPanel |
---|
JPanel.AccessibleJPanel |
Nested classes/interfaces inherited from class javax.swing.JComponent |
---|
JComponent.AccessibleJComponent |
Nested classes/interfaces inherited from class java.awt.Container |
---|
Container.AccessibleAWTContainer |
Nested classes/interfaces inherited from class java.awt.Component |
---|
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy |
Field Summary | |
---|---|
static String |
MODE_CONTROLS_VISIBLE_PROPERTY
PropertyChangeEvents will be triggered for this property when
setModeControlsVisible() is called. |
static String |
MODE_PROPERTY
PropertyChangeEvents will be triggered when the mode changes. |
static String |
OPACITY_PROPERTY
PropertyChangeEvents will be triggered when the opacity value is adjusted. |
static String |
SELECTED_COLOR_PROPERTY
PropertyChangeEvents will be triggered for this property when the selected color
changes. |
protected static ResourceBundle |
strings
The localized strings used in this (and related) panel(s). |
Fields inherited from class javax.swing.JComponent |
---|
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW |
Fields inherited from class java.awt.Component |
---|
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT |
Fields inherited from interface java.awt.image.ImageObserver |
---|
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH |
Constructor Summary | |
---|---|
ColorPicker()
Create a new ColorPicker with all controls visible except opacity. |
|
ColorPicker(boolean showExpertControls,
boolean includeOpacity)
Create a new ColorPicker . |
Method Summary | |
---|---|
void |
addLowerRightComponent(JComponent theComponent)
Add a component to the lower-right corner between the vertical and the alpha-slider. |
Color |
getColor()
|
ColorPalette |
getColorPanel()
|
JPanel |
getExpertControls()
This returns the panel with several rows of spinner controls. |
float[] |
getHSB()
|
ColorPicker.Mode |
getMode()
|
float |
getOpacity()
Returns the currently selected opacity (a float between 0 and 1). |
int[] |
getRGB()
|
void |
setColor(Color c)
Sets the current color of this ColorPicker . |
void |
setExpertControlsVisible(boolean b)
The labels/spinners/buttons on the right side of a ColorPicker are optional. |
void |
setHexControlsVisible(boolean b)
This controls whether the hex field (and label) are visible or not. |
void |
setHSB(float h,
float s,
float b)
Sets the current color of this ColorPicker |
void |
setHSBControlsVisible(boolean b)
This shows or hides the HSB spinner controls. |
void |
setMode(ColorPicker.Mode mode)
Sets the mode of this ColorPicker . |
void |
setModeControlsVisible(boolean b)
This controls whether the radio buttons that adjust the mode are visible. |
void |
setOpacity(int v)
Sets the currently selected opacity. |
void |
setOpacityVisible(boolean b)
This shows or hides the alpha controls. |
void |
setPreviewSwatchVisible(boolean b)
This controls whether the preview swatch visible or not. |
void |
setRGB(int r,
int g,
int b)
Sets the current color of this ColorPicker |
void |
setRGBControlsVisible(boolean b)
This shows or hides the RGB spinner controls. |
Methods inherited from class javax.swing.JPanel |
---|
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected static ResourceBundle strings
public static final String SELECTED_COLOR_PROPERTY
PropertyChangeEvents
will be triggered for this property when the selected color
changes.
(Events are only created when then RGB values of the color change. This means, for example, that the change from HSB(0,0,0) to HSB(.4,0,0) will not generate events, because when the brightness stays zero the RGB color remains (0,0,0). So although the hue moved around, the color is still black, so no events are created.)
public static final String MODE_CONTROLS_VISIBLE_PROPERTY
PropertyChangeEvents
will be triggered for this property when
setModeControlsVisible()
is called.
public static final String OPACITY_PROPERTY
PropertyChangeEvents
will be triggered when the opacity value is adjusted.
public static final String MODE_PROPERTY
PropertyChangeEvents
will be triggered when the mode changes. (That is, when the
wheel switches from HUE, SAT, BRI, RED, GREEN, or BLUE modes.)
Constructor Detail |
---|
public ColorPicker()
ColorPicker
with all controls visible except opacity.
public ColorPicker(boolean showExpertControls, boolean includeOpacity)
ColorPicker
.
showExpertControls
- the labels/spinners/buttons on the right side of a
ColorPicker
are optional. This boolean will control whether they are
shown or not.
It may be that your users will never need or want numeric control when they choose their colors, so hiding this may simplify your interface.
includeOpacity
- whether the opacity controls will be shownMethod Detail |
---|
public void setHexControlsVisible(boolean b)
Note this lives inside the "expert controls", so if
setExpertControlsVisible(false)
has been called, then calling this method makes no
difference: the hex controls will be hidden.
public void setPreviewSwatchVisible(boolean b)
Note this lives inside the "expert controls", so if
setExpertControlsVisible(false)
has been called, then calling this method makes no
difference: the swatch will be hidden.
public void setExpertControlsVisible(boolean b)
ColorPicker
are optional. This
method will control whether they are shown or not.
It may be that your users will never need or want numeric control when they choose their colors, so hiding this may simplify your interface.
b
- whether to show or hide the expert controls.public float[] getHSB()
ColorPicker
. Each value is between
[0,1].public int[] getRGB()
ColorPicker
. Each value is between
[0,255].public float getOpacity()
public void setOpacity(int v)
v
- an int between 0 and 255.public void setMode(ColorPicker.Mode mode)
ColorPicker
. This is especially useful if this picker is in
non-expert mode, so the radio buttons are not visible for the user to directly select.
mode
- must be HUE, SAT, BRI, RED, GREEN or BLUE.public void setModeControlsVisible(boolean b)
(These buttons appear next to the spinners in the expert controls.)
Note these live inside the "expert controls", so if
setExpertControlsVisible(false)
has been called, then these will never be visible.
b
- public ColorPicker.Mode getMode()
ColorPicker
. HUE
, SAT
, BRI
, RED
, GREEN
, or BLUE
.
The default mode is BRI
, because that provides the most
aesthetic/recognizable color wheel.
public void setColor(Color c)
ColorPicker
. This method simply calls
setRGB()
and setOpacity()
.
c
- the new color to use.public void setRGB(int r, int g, int b)
ColorPicker
r
- the red value. Must be between [0,255].g
- the green value. Must be between [0,255].b
- the blue value. Must be between [0,255].public Color getColor()
Color
this ColorPicker
has selected.
This is equivalent to:
int[] i = getRGB();
return new Color(i[0], i[1], i[2], opacitySlider.getValue());
public JPanel getExpertControls()
Note you can also call methods such as setRGBControlsVisible()
to adjust which
controls are showing.
(This returns the panel this ColorPicker
uses, so if you put it in another
container, it will be removed from this ColorPicker
.)
public void setRGBControlsVisible(boolean b)
Note these live inside the "expert controls", so if
setExpertControlsVisible(false)
has been called, then calling this method makes no
difference: the RGB controls will be hidden.
b
- whether the controls should be visible or not.public void setHSBControlsVisible(boolean b)
Note these live inside the "expert controls", so if
setExpertControlsVisible(false)
has been called, then calling this method makes no
difference: the HSB controls will be hidden.
b
- whether the controls should be visible or not.public void setOpacityVisible(boolean b)
Note the alpha spinner live inside the "expert controls", so if
setExpertControlsVisible(false)
has been called, then this method does not affect
that spinner. However, the opacity slider is not affected by the visibility of the
export controls.
b
- public ColorPalette getColorPanel()
ColorPickerPanel
this ColorPicker
displays.public void setHSB(float h, float s, float b)
ColorPicker
h
- the hue value.s
- the saturation value. Must be between [0,1].b
- the blue value. Must be between [0,1].public void addLowerRightComponent(JComponent theComponent)
theComponent
-
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |