diva.canvas.toolbox
Class LabelFigure

java.lang.Object
  extended by diva.canvas.AbstractFigure
      extended by diva.canvas.toolbox.LabelFigure
All Implemented Interfaces:
CanvasComponent, Figure, VisibleComponent, UserObjectContainer

public class LabelFigure
extends AbstractFigure

A figure which draws a string. If the string contains newlines, then it will be broken up into multiple lines. Strings can be "anchored" in the center or on one of the edges or corners, so that when the font or text changes, the label appears to stay in the right location.

Version:
$Id: LabelFigure.java 54924 2009-07-10 23:05:00Z eal $
Author:
Michael Shilman, John Reekie

Field Summary
private  int _anchor
          The anchor on the label.
private static int[] _anchors
          The order of anchors used by the autoanchor method.
private  java.awt.geom.Rectangle2D _bounds
          The bounds in the internal coordinate system.
private  java.awt.geom.Rectangle2D _cachedBounds
          The cached bounds, in the external coordinate system
private static java.awt.Font _defaultFont
          The default font.
private  java.awt.Paint _fillPaint
          The fill paint of the string.
private  java.awt.Font _font
          The font.
private  double _padding
          The "padding" around the text
private  java.awt.Shape _shape
          The list of shapes used to draw the string, in the local coordinate system
private  java.lang.String _string
          The string that gets painted.
private  TransformContext _transformContext
          The transform context
 
Constructor Summary
LabelFigure()
          Construct an empty label figure.
LabelFigure(java.lang.String s)
          Construct a label figure displaying the given string, using the default font.
LabelFigure(java.lang.String s, java.awt.Font f)
          Construct a label figure displaying the given string in the given font.
LabelFigure(java.lang.String s, java.awt.Font font, double padding, int anchor)
          Construct a label figure displaying the given string in the given font, with the given padding and anchor.
LabelFigure(java.lang.String s, java.awt.Font font, double padding, int anchor, java.awt.Color color)
          Construct a label figure displaying the given string in the given font, with the given padding and anchor, and the given color.
LabelFigure(java.lang.String s, java.lang.String face, int style, int size)
          Construct a label figure displaying the given string in the given face, style, and size.
 
Method Summary
private  void _update()
          Update the shape used to draw the figure.
 void autoAnchor(java.awt.Shape s)
          Choose an anchor point so as not to intersect a given figure.
 int getAnchor()
          Get the point at which this figure is "anchored."
 java.awt.geom.Point2D getAnchorPoint()
          Get the location at which the anchor is currently located.
 java.awt.geom.Rectangle2D getBounds()
          Get the bounds of this string
 java.awt.Paint getFillPaint()
          Get the fill paint for this label.
 java.awt.Font getFont()
          Get the font that this label is drawn in.
 java.awt.geom.Point2D getOrigin()
          Return the origin, which is the anchor point.
 double getPadding()
          Get the padding around the text.
 java.awt.Shape getShape()
          Get the shape of this label figure.
 java.lang.String getString()
          Get the string.
 void paint(java.awt.Graphics2D g)
          Paint the figure.
 void setAnchor(int anchor)
          Set the point at which this figure is "anchored."
 void setFillPaint(java.awt.Paint p)
          Set the fill paint that this shape is drawn with.
 void setFont(java.awt.Font f)
          Set the font.
 void setPadding(double padding)
          Set the "padding" around the text.
 void setString(java.lang.String s)
          Set the string.
 void transform(java.awt.geom.AffineTransform at)
          Transform the label with the given transform.
 void translateTo(double x, double y)
          Translate the label so that the current anchor is located at the given point.
 void translateTo(java.awt.geom.Point2D pt)
          Translate the label so that the current anchor is located at the given point.
 
Methods inherited from class diva.canvas.AbstractFigure
contains, getInteractor, getLayer, getParent, getToolTipText, getTransformContext, getUserObject, hit, intersects, isVisible, paint, repaint, repaint, setInteractor, setParent, setToolTipText, setUserObject, setVisible, translate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_anchor

private int _anchor
The anchor on the label. This must be one of the constants defined in SwingConstants.


_bounds

private java.awt.geom.Rectangle2D _bounds
The bounds in the internal coordinate system. This is kept separately from the shape, in case the shape is empty (as it will be for a blank string). In that case, a small bounding box is set -- the default is a zero bounding box, which will produce the wrong coordinates.


_cachedBounds

private java.awt.geom.Rectangle2D _cachedBounds
The cached bounds, in the external coordinate system


_defaultFont

private static java.awt.Font _defaultFont
The default font.


_fillPaint

private java.awt.Paint _fillPaint
The fill paint of the string.


_font

private java.awt.Font _font
The font.


_padding

private double _padding
The "padding" around the text


_shape

private java.awt.Shape _shape
The list of shapes used to draw the string, in the local coordinate system


_string

private java.lang.String _string
The string that gets painted.


_transformContext

private TransformContext _transformContext
The transform context


_anchors

private static int[] _anchors
The order of anchors used by the autoanchor method.

Constructor Detail

LabelFigure

public LabelFigure()
Construct an empty label figure.


LabelFigure

public LabelFigure(java.lang.String s)
Construct a label figure displaying the given string, using the default font.


LabelFigure

public LabelFigure(java.lang.String s,
                   java.lang.String face,
                   int style,
                   int size)
Construct a label figure displaying the given string in the given face, style, and size. A new Font object representing the face, style, and size is created for this label.


LabelFigure

public LabelFigure(java.lang.String s,
                   java.awt.Font f)
Construct a label figure displaying the given string in the given font. This is the best constructor to use if you are creating a lot of labels in a font other than the default, as a single instance of Font can then be shared by many labels.


LabelFigure

public LabelFigure(java.lang.String s,
                   java.awt.Font font,
                   double padding,
                   int anchor)
Construct a label figure displaying the given string in the given font, with the given padding and anchor.


LabelFigure

public LabelFigure(java.lang.String s,
                   java.awt.Font font,
                   double padding,
                   int anchor,
                   java.awt.Color color)
Construct a label figure displaying the given string in the given font, with the given padding and anchor, and the given color.

Method Detail

autoAnchor

public void autoAnchor(java.awt.Shape s)
Choose an anchor point so as not to intersect a given figure. The anchor point is cycled through until one is reached such that the bounding box of the label does not intersect the given shape. If there is none, the anchor is not changed. The order of preference is the current anchor, the four edges, and the four corners.


getAnchor

public int getAnchor()
Get the point at which this figure is "anchored." This will be one of the positioning constants defined in javax.swing.SwingConstants.


getAnchorPoint

public java.awt.geom.Point2D getAnchorPoint()
Get the location at which the anchor is currently located. This method looks at the anchor and padding attributes to figure out the point.


getBounds

public java.awt.geom.Rectangle2D getBounds()
Get the bounds of this string

Specified by:
getBounds in interface Figure
Overrides:
getBounds in class AbstractFigure

getFont

public java.awt.Font getFont()
Get the font that this label is drawn in. To get the font name, style, and size, call this method and then call the appropriate methods on the Font object.


getFillPaint

public java.awt.Paint getFillPaint()
Get the fill paint for this label.


getOrigin

public java.awt.geom.Point2D getOrigin()
Return the origin, which is the anchor point.

Specified by:
getOrigin in interface Figure
Overrides:
getOrigin in class AbstractFigure
Returns:
The anchor point.
See Also:
AbstractFigure.getBounds()

getPadding

public double getPadding()
Get the padding around the text.


getShape

public java.awt.Shape getShape()
Get the shape of this label figure. This just returns the bounds, since hit-testing on the actual filled latter shapes is way slow (and not that useful, since usually you want to treat the whole label as a single object anyway, and not have to click on an actual filled pixel).

Specified by:
getShape in interface Figure
Specified by:
getShape in class AbstractFigure

getString

public java.lang.String getString()
Get the string.


paint

public void paint(java.awt.Graphics2D g)
Paint the figure.

Specified by:
paint in interface VisibleComponent
Specified by:
paint in class AbstractFigure

setAnchor

public void setAnchor(int anchor)
Set the point at which this figure is "anchored." This must be one of the positioning constants defined in javax.swing.SwingConstants. The default is SwingConstants.CENTER. Whenever the font or string is changed, the label will be moved so that the anchor remains at the same position on the screen. When this method is called, the figure is adjusted so that the new anchor is at the same position as the old anchor was. The actual position of the text relative to the anchor point is shifted by the padding attribute.


setFillPaint

public void setFillPaint(java.awt.Paint p)
Set the fill paint that this shape is drawn with.


setFont

public void setFont(java.awt.Font f)
Set the font.


setPadding

public void setPadding(double padding)
Set the "padding" around the text. This is used only if anchors are used -- when the label is positioned relative to an anchor, it is also shifted by the padding distance so that there is some space between the anchor point and the text. The default padding is two, and the padding must not be set to zero if automatic anchoring is used.


setString

public void setString(java.lang.String s)
Set the string.


transform

public void transform(java.awt.geom.AffineTransform at)
Transform the label with the given transform. Note that the anchor of the figure will appear to move -- use translateTo() to move it back again if this method being called to (for example) rotate the label.

Specified by:
transform in interface Figure
Specified by:
transform in class AbstractFigure

translateTo

public void translateTo(double x,
                        double y)
Translate the label so that the current anchor is located at the given point. Use this if you apply a transform to a label in order to rotate or scale it, but don't want the label to actually go anywhere.


translateTo

public void translateTo(java.awt.geom.Point2D pt)
Translate the label so that the current anchor is located at the given point. Use this if you apply a transform to a label in order to rotate or scale it, but don't want the label to actually go anywhere.


_update

private void _update()
Update the shape used to draw the figure.