edu.neu.ccs.gui
Class TextPaintable

java.lang.Object
  |
  +--edu.neu.ccs.gui.AbstractPaintable
        |
        +--edu.neu.ccs.gui.TextPaintable
All Implemented Interfaces:
Paintable, SupportsPropertyChange

public class TextPaintable
extends AbstractPaintable

A TextPaintable creates a Paintable using a string, a font, and other related data.

In 2.3.2, the method getBounds2D was modified to use the default Bounds2D rectangle if that rectangle is non-null before making any other tests or computations.

Since:
2.3

Field Summary
static String SET_ANCHOR_LOCATOR
          Bound property name for set anchor locator.
static String SET_ANCHOR_POSITION
          Bound property name for set anchor position.
static String SET_BOUNDS_STRATEGY
          Bound property name for set bounds strategy.
static String SET_FILL_PAINT
          Bound property name for set fill paint.
static String SET_FONT
          Bound property name for set font.
static String SET_STRING
          Bound property name for set string.
static FontRenderContext standardFRC
          The standard font render context defined using the identity transform, anti-aliasing on, and fractional-metrics off.
 
Fields inherited from class edu.neu.ccs.gui.AbstractPaintable
SET_DEFAULT_BOUNDS2D, SET_DEFAULT_CENTER, SET_OPACITY, SET_VISIBLE
 
Constructor Summary
TextPaintable()
          The constructor that creates a TextPaintable with all defaults.
TextPaintable(String string)
          The constructor that creates a TextPaintable with the given string.
TextPaintable(String string, Font font)
          The constructor that creates a TextPaintable with the given string and font.
TextPaintable(String string, Font font, Paint fillpaint)
          The constructor that creates a TextPaintable with the given string, font, and fill paint.
TextPaintable(String string, Font font, Paint fillpaint, TextBounds.Strategy strategy)
          The constructor that creates a TextPaintable with the given string, font, fill paint, and bounds strategy.
TextPaintable(String string, Font font, Paint fillpaint, TextBounds.Strategy strategy, TextAnchor.Locator locator)
          The constructor that creates a TextPaintable with the given string, font, fill paint, bounds strategy, and anchor locator.
TextPaintable(String string, Font font, Paint fillpaint, TextBounds.Strategy strategy, TextAnchor.Locator locator, float anchorX, float anchorY)
          The constructor that creates a TextPaintable with the given string, font, fill paint, bounds strategy, anchor locator, and anchor position.
 
Method Summary
 boolean contains(double x, double y)
          Tests if a point specified by coordinates is inside the paintable.
 TextAnchor.Locator getAnchorLocator()
          Returns the TextAnchor.Locator for this TextPaintable.
 float[] getAnchorPosition()
          Returns a copy of the anchor position for this TextPaintable as an array float[2].
 float getAscentLineY()
          Returns the ascent line y-coordinate of this TextPaintable.
 float getBaseLineY()
          Returns the base line y-coordinate of this TextPaintable.
 Rectangle2D getBounds2D()
          Returns the bounds of the paintable based on the default settings or on more detailed computations.
 TextBounds.Strategy getBoundsStrategy()
          Returns the bounds strategy for this TextPaintable.
 Point2D getCenter()
          Returns a copy of the center of the paint region affected by the paint method.
 float getCenterX()
          Returns the center x-coordinate of this TextPaintable.
static Font getDefaultFont()
          Returns the default font for a JLabel in the current UI settings.
 float getDescentLineY()
          Returns the descent line y-coordinate of this TextPaintable.
 Paint getFillPaint()
          Returns the fill paint for this TextPaintable.
 Font getFont()
          Returns the font for this TextPaintable.
 float getHeight()
          Returns the height of the area that will be occupied by this TextPaintable.
 float getLeadingLineY()
          Returns the leading line y-coordinate of this TextPaintable.
 float getLeftX()
          Returns the left x-coordinate of this TextPaintable.
 Rectangle2D getLooseBounds()
          Returns the smallest rectangle that contains the pair of rectangles getStringBounds() and getTightBounds().
 Shape getOutline()
          Returns the Shape that represents the outline of the rendered text string in its font at its location.
 float getRightX()
          Returns the right x-coordinate of this TextPaintable.
 String getString()
          Returns the string for this TextPaintable.
 Rectangle2D getStringBounds()
          Returns a copy of the 2-dimensional bounds of the paint region as computed using the getStringBounds method of the Font class together with the anchor locator.
 TextLayout getTextLayout()
          Returns the internal TextLayout that encapsulates the text string and the font.
 Rectangle2D getTightBounds()
          Returns a copy of the 2-dimensional bounds of the paint region as computed using the getBounds method of the TextLayout class together with the anchor locator.
 float getWidth()
          Returns the width of the area that will be occupied by this TextPaintable.
 void paint(Graphics g)
          Paints onto a Graphics context g using information from this object.
 void setAnchorLocator(TextAnchor.Locator locator)
          Sets the TextAnchor.Locator for this TextPaintable.
 void setAnchorPosition(float[] position)
          Sets the anchor position for this TextPaintable using an array float[2].
 void setAnchorPosition(float anchorX, float anchorY)
          Sets the anchor position for this TextPaintable.
 void setBoundsStrategy(TextBounds.Strategy strategy)
          Sets the bounds strategy for this TextPaintable.
 void setFillPaint(Paint fillpaint)
          Sets the fill paint for this TextPaintable.
 void setFont(Font font)
          Sets the font for this TextPaintable.
 void setString(String string)
          Sets the string for this TextPaintable.
 
Methods inherited from class edu.neu.ccs.gui.AbstractPaintable
addForwardingListener, addPropertyChangeListener, addPropertyChangeListener, addPropertyChangeListeners, addPropertyChangeListeners, applyOpacity, contains, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getDefaultBounds2D, getDefaultCenter, getForwardingListener, getOpacity, getPreparedGraphics2D, getPropertyChangeListeners, getPropertyChangeListeners, hasListeners, isVisible, possiblyContains, removeAndAddForwardingListener, removeForwardingListener, removePropertyChangeListener, removePropertyChangeListener, setDefaultBounds2D, setDefaultCenter, setOpacity, setVisible
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SET_STRING

public static final String SET_STRING
Bound property name for set string.

See Also:
Constant Field Values

SET_FONT

public static final String SET_FONT
Bound property name for set font.

See Also:
Constant Field Values

SET_FILL_PAINT

public static final String SET_FILL_PAINT
Bound property name for set fill paint.

See Also:
Constant Field Values

SET_ANCHOR_LOCATOR

public static final String SET_ANCHOR_LOCATOR
Bound property name for set anchor locator.

See Also:
Constant Field Values

SET_ANCHOR_POSITION

public static final String SET_ANCHOR_POSITION
Bound property name for set anchor position.

See Also:
Constant Field Values

SET_BOUNDS_STRATEGY

public static final String SET_BOUNDS_STRATEGY
Bound property name for set bounds strategy.

See Also:
Constant Field Values

standardFRC

public static final FontRenderContext standardFRC
The standard font render context defined using the identity transform, anti-aliasing on, and fractional-metrics off.

Constructor Detail

TextPaintable

public TextPaintable()

The constructor that creates a TextPaintable with all defaults.

The default anchor position is (0, 0) which must be changed to a more useful value by the caller.

See Also:
setString(String), setFont(Font), setFillPaint(Paint), setBoundsStrategy(TextBounds.Strategy), setAnchorLocator(TextAnchor.Locator), setAnchorPosition(float, float), setAnchorPosition(float[]), TextPaintable(String), TextPaintable(String, Font), TextPaintable(String, Font, Paint), TextPaintable(String, Font, Paint, TextBounds.Strategy), TextPaintable(String, Font, Paint, TextBounds.Strategy, TextAnchor.Locator), TextPaintable(String, Font, Paint, TextBounds.Strategy, TextAnchor.Locator, float, float)

TextPaintable

public TextPaintable(String string)

The constructor that creates a TextPaintable with the given string.

The default anchor position is (0, 0) which must be changed to a more useful value by the caller.

Parameters:
string - the string to display
See Also:
setString(String), setFont(Font), setFillPaint(Paint), setBoundsStrategy(TextBounds.Strategy), setAnchorLocator(TextAnchor.Locator), setAnchorPosition(float, float), setAnchorPosition(float[]), TextPaintable(), TextPaintable(String, Font), TextPaintable(String, Font, Paint), TextPaintable(String, Font, Paint, TextBounds.Strategy), TextPaintable(String, Font, Paint, TextBounds.Strategy, TextAnchor.Locator), TextPaintable(String, Font, Paint, TextBounds.Strategy, TextAnchor.Locator, float, float)

TextPaintable

public TextPaintable(String string,
                     Font font)

The constructor that creates a TextPaintable with the given string and font.

The default anchor position is (0, 0) which must be changed to a more useful value by the caller.

Parameters:
string - the string to display
font - the font to use to display the string
See Also:
setString(String), setFont(Font), setFillPaint(Paint), setBoundsStrategy(TextBounds.Strategy), setAnchorLocator(TextAnchor.Locator), setAnchorPosition(float, float), setAnchorPosition(float[]), TextPaintable(), TextPaintable(String), TextPaintable(String, Font, Paint), TextPaintable(String, Font, Paint, TextBounds.Strategy), TextPaintable(String, Font, Paint, TextBounds.Strategy, TextAnchor.Locator), TextPaintable(String, Font, Paint, TextBounds.Strategy, TextAnchor.Locator, float, float)

TextPaintable

public TextPaintable(String string,
                     Font font,
                     Paint fillpaint)

The constructor that creates a TextPaintable with the given string, font, and fill paint.

The default anchor position is (0, 0) which must be changed to a more useful value by the caller.

Parameters:
string - the string to display
font - the font to use to display the string
fillpaint - the paint to use to fill the string
See Also:
setString(String), setFont(Font), setFillPaint(Paint), setBoundsStrategy(TextBounds.Strategy), setAnchorLocator(TextAnchor.Locator), setAnchorPosition(float, float), setAnchorPosition(float[]), TextPaintable(), TextPaintable(String), TextPaintable(String, Font), TextPaintable(String, Font, Paint, TextBounds.Strategy), TextPaintable(String, Font, Paint, TextBounds.Strategy, TextAnchor.Locator), TextPaintable(String, Font, Paint, TextBounds.Strategy, TextAnchor.Locator, float, float)

TextPaintable

public TextPaintable(String string,
                     Font font,
                     Paint fillpaint,
                     TextBounds.Strategy strategy)

The constructor that creates a TextPaintable with the given string, font, fill paint, and bounds strategy.

The default anchor position is (0, 0) which must be changed to a more useful value by the caller.

Parameters:
string - the string to display
font - the font to use to display the string
fillpaint - the paint to use to fill the string
strategy - the strategy to use to compute the paintable bounds
See Also:
setString(String), setFont(Font), setFillPaint(Paint), setBoundsStrategy(TextBounds.Strategy), setAnchorLocator(TextAnchor.Locator), setAnchorPosition(float, float), setAnchorPosition(float[]), TextPaintable(), TextPaintable(String), TextPaintable(String, Font), TextPaintable(String, Font, Paint), TextPaintable(String, Font, Paint, TextBounds.Strategy, TextAnchor.Locator), TextPaintable(String, Font, Paint, TextBounds.Strategy, TextAnchor.Locator, float, float)

TextPaintable

public TextPaintable(String string,
                     Font font,
                     Paint fillpaint,
                     TextBounds.Strategy strategy,
                     TextAnchor.Locator locator)

The constructor that creates a TextPaintable with the given string, font, fill paint, bounds strategy, and anchor locator.

The default anchor position is (0, 0) which must be changed to a more useful value by the caller.

Parameters:
string - the string to display
font - the font to use to display the string
fillpaint - the paint to use to fill the string
strategy - the strategy to use to compute the paintable bounds
locator - the locator for the anchor position
See Also:
setString(String), setFont(Font), setFillPaint(Paint), setBoundsStrategy(TextBounds.Strategy), setAnchorLocator(TextAnchor.Locator), setAnchorPosition(float, float), setAnchorPosition(float[]), TextPaintable(), TextPaintable(String), TextPaintable(String, Font), TextPaintable(String, Font, Paint), TextPaintable(String, Font, Paint, TextBounds.Strategy), TextPaintable(String, Font, Paint, TextBounds.Strategy, TextAnchor.Locator, float, float)

TextPaintable

public TextPaintable(String string,
                     Font font,
                     Paint fillpaint,
                     TextBounds.Strategy strategy,
                     TextAnchor.Locator locator,
                     float anchorX,
                     float anchorY)

The constructor that creates a TextPaintable with the given string, font, fill paint, bounds strategy, anchor locator, and anchor position.

Parameters that are null are set to the following defaults:

Parameters:
string - the string to display
font - the font to use to display the string
fillpaint - the paint to use to fill the string
strategy - the strategy to use to compute the paintable bounds
locator - the locator for the anchor position
anchorX - the anchor x-coordinate
anchorY - the anchor y-coordinate
See Also:
setString(String), setFont(Font), setFillPaint(Paint), setBoundsStrategy(TextBounds.Strategy), setAnchorLocator(TextAnchor.Locator), setAnchorPosition(float, float), setAnchorPosition(float[]), TextPaintable(), TextPaintable(String), TextPaintable(String, Font), TextPaintable(String, Font, Paint), TextPaintable(String, Font, Paint, TextBounds.Strategy), TextPaintable(String, Font, Paint, TextBounds.Strategy, TextAnchor.Locator)
Method Detail

paint

public final void paint(Graphics g)

Paints onto a Graphics context g using information from this object.

Does nothing if the encapsulated text string has zero length.

Specified by:
paint in interface Paintable
Specified by:
paint in class AbstractPaintable
Parameters:
g - the graphics context on which to paint
See Also:
AbstractPaintable.getPreparedGraphics2D(Graphics)

getTextLayout

public TextLayout getTextLayout()

Returns the internal TextLayout that encapsulates the text string and the font.

If the encapsulated text string has zero length, then returns null.

Returns:
the internal TextLayout object

getStringBounds

public final Rectangle2D getStringBounds()

Returns a copy of the 2-dimensional bounds of the paint region as computed using the getStringBounds method of the Font class together with the anchor locator.

In the vertical direction, these bounds extend from the ascent line to the leading line. Thus, the height of these bounds do not depend on the particular string being rendered.

In the horizontal direction, these bounds extend from the left edge of string being painted and will include allowances for both leading and trailing white space. Unfortunately, however, if the string has no leading and trailing whitespace, it is possible for pixels to be painted outside of these bounds due to serifs or to the slant of an italic font.

If the encapsulated text string has zero length, then this method returns a rectangle at the anchor position of zero width and height.

Returns:
a copy of the 2-dimensional bounds of the paint region
See Also:
getTightBounds(), getLooseBounds(), getBounds2D()

getTightBounds

public final Rectangle2D getTightBounds()

Returns a copy of the 2-dimensional bounds of the paint region as computed using the getBounds method of the TextLayout class together with the anchor locator.

These bounds return a tight rectangle that includes all pixel positions that may be modified when the string is painted. The only exception to this rule is that anti-aliasing may cause some pixels adjacent to this rectangle to be rendered also. There is no Java code that is capable of testing for this effect.

The height of these bounds will vary from string to string depending on the height of the ascenders and descenders of the particular characters in the particular string.

These bounds do not include leading and trailing whitespace.

Be aware that if you have set the logical left edge of the paint area for a string to some value x then it is still possible for pixels to the left of x to be modified during rendering of the string due to the presence of serifs. The bounds returned by this method will include adjustments for such effects and will be as accurate as possible.

If the encapsulated text string has zero length, then this method returns a rectangle at the anchor position of zero width and height.

Returns:
a copy of the tight bounds of the paint region
See Also:
getStringBounds(), getLooseBounds(), getBounds2D()

getLooseBounds

public final Rectangle2D getLooseBounds()

Returns the smallest rectangle that contains the pair of rectangles getStringBounds() and getTightBounds().

These bounds return a loose rectangle that includes all pixel positions that may be modified when the string is painted plus allowances for additional horizontal and vertical white space.

If the encapsulated text string has zero length, then this method returns a rectangle at the anchor position of zero width and height.

Returns:
a copy of the tight bounds of the paint region
See Also:
getStringBounds(), getTightBounds(), getBounds2D()

getBounds2D

public final Rectangle2D getBounds2D()

Returns the bounds of the paintable based on the default settings or on more detailed computations.

If the value of getDefaultBounds2D is non-null, then this value is returned.

Otherwise, if the encapsulated text string has zero length, then returns a rectangle of zero width and height located at the anchor location.

Otherwise, returns a copy of the 2-dimensional bounds of the paint region affected by the paint method according to the current bounds strategy.

Specified by:
getBounds2D in interface Paintable
Specified by:
getBounds2D in class AbstractPaintable
Returns:
a copy of the 2-dimensional bounds of the paint region
See Also:
getStringBounds(), getTightBounds(), getLooseBounds()

getCenter

public final Point2D getCenter()

Returns a copy of the center of the paint region affected by the paint method.

The center is computed as the center of the base line.

This method uses the anchor locator.

Specified by:
getCenter in interface Paintable
Overrides:
getCenter in class AbstractPaintable
Returns:
a copy of the center of the paint region
See Also:
AbstractPaintable.setDefaultCenter(Point2D), AbstractPaintable.getDefaultCenter()

getOutline

public Shape getOutline()

Returns the Shape that represents the outline of the rendered text string in its font at its location.

Experimental tests show that the Shape returned by this method is not 100% accurate. This is due to problems in the getOutline method of the Java class TextLayout.

If the encapsulated text string has zero length, then returns an empty Shape.

Returns:
the Shape object

contains

public boolean contains(double x,
                        double y)

Tests if a point specified by coordinates is inside the paintable.

This method returns false if one or more of the following conditions occurs:

Parameters:
x - the x-coordinate of the point
y - the y-coordinate of the point
Returns:
whether or not a specified point is inside the paintable

setString

public final void setString(String string)

Sets the string for this TextPaintable.

If the given string is null, it is set to the zero length string.

Does nothing if the string is equal to the current string.

Fires property change: SET_STRING.

Parameters:
string - the string to display
See Also:
getString()

getString

public final String getString()
Returns the string for this TextPaintable.

Returns:
the string to display
See Also:
setString(String)

setFont

public final void setFont(Font font)

Sets the font for this TextPaintable.

If the given font is null then it is set to getDefaultFont().

Does nothing if the font is equal to the current font.

Fires property change: SET_FONT.

Parameters:
font - the font to use to display the string
See Also:
getFont()

getFont

public final Font getFont()
Returns the font for this TextPaintable.

Returns:
the font to use to display the string
See Also:
setFont(Font)

setFillPaint

public final void setFillPaint(Paint fillpaint)

Sets the fill paint for this TextPaintable.

If the given paint is null, it is set to Color.black.

Fires property change: SET_FILL_PAINT.

Parameters:
fillpaint - the paint to use to fill the string
See Also:
getFillPaint()

getFillPaint

public final Paint getFillPaint()
Returns the fill paint for this TextPaintable.

Returns:
the paint to use to fill the string
See Also:
setFillPaint(Paint)

setBoundsStrategy

public final void setBoundsStrategy(TextBounds.Strategy strategy)

Sets the bounds strategy for this TextPaintable.

If the given strategy is null, it is set to TextBounds.LOOSE.

Fires property change: SET_BOUNDS_STRATEGY.

Parameters:
strategy - the strategy to use to compute the paintable bounds
See Also:
getBoundsStrategy()

getBoundsStrategy

public final TextBounds.Strategy getBoundsStrategy()
Returns the bounds strategy for this TextPaintable.

Returns:
the strategy to use to compute the paintable bounds
See Also:
setFillPaint(Paint)

setAnchorLocator

public final void setAnchorLocator(TextAnchor.Locator locator)

Sets the TextAnchor.Locator for this TextPaintable.

The following constants are provided in the class TextAnchor as possible options for the TextAnchor.Locator parameter:

If the given locator is null, it is set to TextAnchor.LEFT_BASELINE.

Fires property change: SET_ANCHOR_LOCATOR.

Parameters:
locator - the locator for the anchor position
See Also:
getAnchorLocator()

getAnchorLocator

public final TextAnchor.Locator getAnchorLocator()

Returns the TextAnchor.Locator for this TextPaintable.

Returns:
the locator for the anchor position
See Also:
setAnchorLocator(TextAnchor.Locator)

setAnchorPosition

public final void setAnchorPosition(float anchorX,
                                    float anchorY)

Sets the anchor position for this TextPaintable.

Fires property change: SET_ANCHOR_POSITION.

Parameters:
anchorX - the anchor x-coordinate
anchorY - the anchor y-coordinate
See Also:
setAnchorPosition(float[]), getAnchorPosition()

setAnchorPosition

public final void setAnchorPosition(float[] position)

Sets the anchor position for this TextPaintable using an array float[2].

A null parameter is ignored.

Fires property change: SET_ANCHOR_POSITION.

Parameters:
position - the anchor position
See Also:
setAnchorPosition(float, float), getAnchorPosition()

getAnchorPosition

public final float[] getAnchorPosition()
Returns a copy of the anchor position for this TextPaintable as an array float[2].

Returns:
the anchor position
See Also:
setAnchorPosition(float, float), setAnchorPosition(float[])

getLeftX

public final float getLeftX()

Returns the left x-coordinate of this TextPaintable.

This method uses the anchor locator.

Returns:
the left x-coordinate of this text paintable

getRightX

public final float getRightX()

Returns the right x-coordinate of this TextPaintable.

This method uses the anchor locator.

Returns:
the right x-coordinate of this text paintable

getCenterX

public final float getCenterX()

Returns the center x-coordinate of this TextPaintable.

This method uses the anchor locator.

Returns:
the center x-coordinate of this text paintable

getBaseLineY

public final float getBaseLineY()

Returns the base line y-coordinate of this TextPaintable.

This method uses the anchor locator.

Returns:
the base line y-coordinate of this text paintable

getAscentLineY

public final float getAscentLineY()

Returns the ascent line y-coordinate of this TextPaintable.

This method uses the anchor locator.

Returns:
the ascent line y-coordinate of this text paintable

getDescentLineY

public final float getDescentLineY()

Returns the descent line y-coordinate of this TextPaintable.

This method uses the anchor locator.

Returns:
the descent line y-coordinate of this text paintable

getLeadingLineY

public final float getLeadingLineY()

Returns the leading line y-coordinate of this TextPaintable.

This methods uses the anchor locator.

Returns:
the leading line y-coordinate of this text paintable

getWidth

public final float getWidth()

Returns the width of the area that will be occupied by this TextPaintable.

This method uses the method getBounds2D.

Returns:
the width of this text paintable

getHeight

public final float getHeight()

Returns the height of the area that will be occupied by this TextPaintable.

This method uses the method getBounds2D.

Returns:
the height of this text paintable

getDefaultFont

public static Font getDefaultFont()

Returns the default font for a JLabel in the current UI settings. This font is used in constructing a TextPaintable if the user does not specify a font.

Returns:
the current JLabel default font