Class 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$
    Author:
    Michael Shilman, John Reekie
    • Constructor Summary

      Constructors 
      Constructor Description
      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

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      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." This will be one of the positioning constants defined in javax.swing.SwingConstants.
      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." This must be one of the positioning constants defined in javax.swing.SwingConstants.
      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 java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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
        Returns:
        the bounding box of this figure.
      • 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.
      • 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
        Returns:
        the outline shape of this figure
      • 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
        Parameters:
        g - The 2D graphics object that this object it to be painted upon.
      • 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
        Parameters:
        at - The transform to be used.
      • 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.