edu.neu.ccs.gui
Class PaintSwatch

java.lang.Object
  extended byjava.awt.Component
      extended byjava.awt.Container
          extended byjavax.swing.JComponent
              extended byedu.neu.ccs.gui.JPTComponent
                  extended byedu.neu.ccs.gui.PaintSwatch
All Implemented Interfaces:
Icon, ImageObserver, JPTConstants, MenuContainer, Serializable, SupportsPropertyChange, SwingConstants

public class PaintSwatch
extends JPTComponent
implements Icon

Class PaintSwatch is a light weight component to draw a rectangular paint swatch with a given paint, size, and optional border.

This class also implements the Icon interface.

For convenience, the class provides three static border objects so that a swatch border may be changed without changing the space needed in the GUI.

Since:
2.3.2
Version:
2.3.3
See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
private  Rectangle2D area
          The area to paint on within the border.
static Border BLACK_BORDER
          A standard black swatch border of standard thickness.
static int BORDER_SIZE
          The thickness of a standard swatch border.
static int DEFAULT_SWATCH_SIZE
          Default x and y swatch size.
private  Paint paint
          The encapsulated paint.
static String SET_BORDER
          Bound property name for set border.
static String SET_PAINT
          Bound property name for set paint.
static String SET_SIZE
          Bound property name for set size.
static Border TRANSPARENT_BORDER
          A standard transparent swatch border of standard thickness.
static Border WHITE_BORDER
          A standard white swatch border of standard thickness.
private  int xSize
          The x size.
private  int ySize
          The y size.
 
Fields inherited from class edu.neu.ccs.gui.JPTComponent
 
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.Container
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface edu.neu.ccs.util.JPTConstants
ABOVE, ALIGNMENT, BELOW, BOTTOM_LEFT, BOTTOM_RIGHT, DEFAULT, FONT, INPUT_PROPERTIES, MANDATORY, OPTIONAL, ORIENTATION, TOP_LEFT, TOP_RIGHT, VALUE
 
Fields inherited from interface javax.swing.SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
PaintSwatch()
          This constructor sets the swatch paint to Colors.transparent; sets the dimensions of the swatch to (20, 20); sets the border to BLACK_BORDER which has thickness 2.
PaintSwatch(Paint paint)
          This constructor provides the paint to paint in the swatch; sets the dimensions of the swatch to (20, 20); sets the border to BLACK_BORDER which has thickness 2.
PaintSwatch(Paint paint, Border border)
          This constructor provides the paint to paint in the swatch and the border; sets the dimensions of the swatch to (20, 20).
PaintSwatch(Paint paint, int xSize, int ySize)
          This constructor provides the paint to paint in the swatch and the x and y dimensions of the swatch; sets the border to BLACK_BORDER which has thickness 2.
PaintSwatch(Paint paint, int xSize, int ySize, Border border)
          This constructor provides the paint to paint in the swatch, the x and y dimensions of the swatch, and the border.
 
Method Summary
 int getIconHeight()
          Returns the icon height as getYSize().
 int getIconWidth()
          Returns the icon width as getXSize().
 Dimension getMaximumSize()
          Returns the same dimension as getPreferredSize.
 Dimension getMinimumSize()
          Returns the same dimension as getPreferredSize.
 Paint getPaint()
          Returns the paint of the swatch.
 Dimension getPreferredSize()
          Returns the preferred size of the swatch based on the size of the area to be painted and the swatch insets if any.
 Dimension getSize()
          Returns the same dimension as getPreferredSize.
 int getXSize()
          Returns the current x-dimension of the swatch excluding the border insets.
 int getYSize()
          Returns the current y-dimension of the swatch excluding the border insets.
protected  void paintComponent(Graphics g)
          Overrides the inherited paintComponent method to paint the swatch area with the encapsulated paint.
 void paintIcon(Component c, Graphics g, int x, int y)
          Draw the swatch as an icon at the specified location.
 void setBorder(Border border)
          Calls the inherited setBorder method and then fires the property change SET_BORDER.
 void setPaint(Paint paint)
          Sets the paint of the swatch.
 void setSize(int xSize, int ySize)
          Sets the size of the swatch by giving its x-size and y-size.
 
Methods inherited from class edu.neu.ccs.gui.JPTComponent
addBorder, addForwardingListener, addPropertyChangeListeners, addPropertyChangeListeners, bevelBorderLowered, bevelBorderLowered, bevelBorderLowered, bevelBorderLowered, bevelBorderLowered, bevelBorderRaised, bevelBorderRaised, bevelBorderRaised, bevelBorderRaised, bevelBorderRaised, emptyBorder, emptyBorder, emptyBorder, etchBorderLowered, etchBorderLowered, etchBorderLowered, etchBorderRaised, etchBorderRaised, etchBorderRaised, firePropertyChange, firePropertyChange, frame, frame, frame, frame, frame, frame, generalDialog, generalDialog, generalDialog, generalDialog, getForwardingListener, getKeyActionAdapter, getMouseActionAdapter, hasListeners, lineBorder, lineBorder, lineBorder, matteBorder, matteBorder, matteBorder, matteBorder, matteBorder, matteBorder, OKCancelDialog, OKCancelDialog, OKDialog, OKDialog, refreshComponent, removeAndAddForwardingListener, removeBorder, removeForwardingListener, setDeepBackground, setDeepBackground, titleBorder, YesNoCancelDialog, YesNoCancelDialog
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getNextFocusableComponent, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface edu.neu.ccs.gui.SupportsPropertyChange
addPropertyChangeListener, addPropertyChangeListener, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getPropertyChangeListeners, getPropertyChangeListeners, removePropertyChangeListener, removePropertyChangeListener
 

Field Detail

SET_PAINT

public static final String SET_PAINT
Bound property name for set paint.

See Also:
Constant Field Values

SET_SIZE

public static final String SET_SIZE
Bound property name for set size.

See Also:
Constant Field Values

SET_BORDER

public static final String SET_BORDER
Bound property name for set border.

See Also:
Constant Field Values

DEFAULT_SWATCH_SIZE

public static final int DEFAULT_SWATCH_SIZE
Default x and y swatch size.

See Also:
Constant Field Values

BORDER_SIZE

public static final int BORDER_SIZE
The thickness of a standard swatch border.

See Also:
Constant Field Values

BLACK_BORDER

public static final Border BLACK_BORDER
A standard black swatch border of standard thickness.


WHITE_BORDER

public static final Border WHITE_BORDER
A standard white swatch border of standard thickness.


TRANSPARENT_BORDER

public static final Border TRANSPARENT_BORDER
A standard transparent swatch border of standard thickness.


paint

private Paint paint
The encapsulated paint.


xSize

private int xSize
The x size. Must be at least 1. Initialized to 0 to guarantee that area will be set.


ySize

private int ySize
The y size. Must be at least 1. Initialized to 0 to guarantee that area will be set.


area

private Rectangle2D area
The area to paint on within the border. Will be set in setSize.

Constructor Detail

PaintSwatch

public PaintSwatch()

This constructor sets the swatch paint to Colors.transparent; sets the dimensions of the swatch to (20, 20); sets the border to BLACK_BORDER which has thickness 2.

The caller should set the swatch paint to a more useful paint after construction.

The preferred size of this component will be (24, 24) to take into account both the swatch size and the border.

Other constructors:


PaintSwatch

public PaintSwatch(Paint paint)

This constructor provides the paint to paint in the swatch; sets the dimensions of the swatch to (20, 20); sets the border to BLACK_BORDER which has thickness 2.

If the given paint is null, sets the paint to Colors.transparent.

The preferred size of this component will be (24, 24) to take into account both the swatch size and the border.

Parameters:
paint - the paint to paint in the swatch
See Also:
PaintSwatch()

PaintSwatch

public PaintSwatch(Paint paint,
                   int xSize,
                   int ySize)

This constructor provides the paint to paint in the swatch and the x and y dimensions of the swatch; sets the border to BLACK_BORDER which has thickness 2.

If the given paint is null, sets the paint to Colors.transparent.

The preferred size of this component will be (xSize+4, ySize+4) to take into account both the swatch size and the border.

Parameters:
paint - the paint to paint in the swatch
xSize - the x-dimension of the swatch (must be at least 1)
ySize - the y-dimension of the swatch (must be at least 1)
See Also:
PaintSwatch(Paint)

PaintSwatch

public PaintSwatch(Paint paint,
                   Border border)

This constructor provides the paint to paint in the swatch and the border; sets the dimensions of the swatch to (20, 20).

If the given paint is null, sets the paint to Colors.transparent.

Sets no border if the given border is null.

Parameters:
paint - the paint to paint in the swatch
border - the border for the swatch
See Also:
PaintSwatch(Paint)

PaintSwatch

public PaintSwatch(Paint paint,
                   int xSize,
                   int ySize,
                   Border border)

This constructor provides the paint to paint in the swatch, the x and y dimensions of the swatch, and the border.

If the given paint is null, sets the paint to Colors.transparent.

Sets no border if the given border is null.

Parameters:
paint - the paint to paint in the swatch
xSize - the x-dimension of the swatch (must be at least 1)
ySize - the y-dimension of the swatch (must be at least 1)
border - the border for the swatch
See Also:
PaintSwatch(Paint)
Method Detail

getPreferredSize

public Dimension getPreferredSize()

Returns the preferred size of the swatch based on the size of the area to be painted and the swatch insets if any.

Returns:
the preferred size

getMinimumSize

public Dimension getMinimumSize()

Returns the same dimension as getPreferredSize.

Returns:
the minimum size

getMaximumSize

public Dimension getMaximumSize()

Returns the same dimension as getPreferredSize.

Returns:
the maximum size

getSize

public Dimension getSize()

Returns the same dimension as getPreferredSize.

Returns:
the size

setPaint

public void setPaint(Paint paint)

Sets the paint of the swatch.

If the given paint is null, sets the paint to Colors.transparent.

Does nothing if the given paint is identical to the current paint.

Fires property change: SET_PAINT.

Parameters:
paint - the paint to set

getPaint

public Paint getPaint()

Returns the paint of the swatch.

Returns:
the paint of the swatch

setSize

public void setSize(int xSize,
                    int ySize)

Sets the size of the swatch by giving its x-size and y-size. If either number is less than 1, that number is set to 1. The size set by this method does not include the border insets.

Does nothing if both parameters equal the corresponding current settings.

Note that the method getPreferredSize will return dimension data that includes the border insets.

Fires property change: SET_SIZE.

Parameters:
xSize - the x-dimension of the swatch (must be at least 1)
ySize - the y-dimension of the swatch (must be at least 1)

getXSize

public int getXSize()

Returns the current x-dimension of the swatch excluding the border insets.

Returns:
the current x-dimension

getYSize

public int getYSize()

Returns the current y-dimension of the swatch excluding the border insets.

Returns:
the current y-dimension

setBorder

public void setBorder(Border border)

Calls the inherited setBorder method and then fires the property change SET_BORDER.

Does nothing if the given border is null or if the existing border is the same as the given border.

Parameters:
border - the border for the swatch

getIconWidth

public int getIconWidth()

Returns the icon width as getXSize().

Specified by:
getIconWidth in interface Icon
Returns:
the icon width

getIconHeight

public int getIconHeight()

Returns the icon height as getYSize().

Specified by:
getIconHeight in interface Icon
Returns:
the icon height

paintIcon

public void paintIcon(Component c,
                      Graphics g,
                      int x,
                      int y)

Draw the swatch as an icon at the specified location.

If the component parameter c is non-null, if it is set to opaque, and if its background color is non-null, then the background color will be painted before the swatch is painted. This will be useful only if the swatch paint is partially transparent. Otherwise, the background color will be painted over.

Does nothing if the graphics context parameter g is null.

Specified by:
paintIcon in interface Icon
Parameters:
c - the component on which the icon will be painted
g - the graphics context on which to paint
x - the x-location
y - the y-location

paintComponent

protected void paintComponent(Graphics g)

Overrides the inherited paintComponent method to paint the swatch area with the encapsulated paint.

If this component is set to opaque and if its background color is non-null, then the background color will be painted before the swatch is painted. This will be useful only if the swatch paint is partially transparent. Otherwise, the background will be painted over.

Does nothing if the graphics context parameter g is null.

Parameters:
g - the graphics context of the swatch