Class BasicFigure

  • All Implemented Interfaces:
    CanvasComponent, Figure, ShapedFigure, VisibleComponent, UserObjectContainer
    Direct Known Subclasses:
    BasicEllipse, BasicRectangle, PathFigure, RoundedRectangle

    public class BasicFigure
    extends AbstractFigure
    implements ShapedFigure
    A BasicFigure is one that contains a single instance of Shape. The figure can have a fill with optional compositing (for translucency), and a stroke with a different fill. With this class, simple objects can be created on-the-fly simply by passing an instance of java.awt.Shape to the constructor. This class is mainly intended for use for simple open and closed shapes. For more complex Figures, use the VectorFigure class.
    Version:
    $Id$
    Author:
    John Reekie, Nick Zamora
    • Constructor Detail

      • BasicFigure

        public BasicFigure​(java.awt.Shape shape)
        Create a new figure with the given shape. The figure, by default, has a unit-width continuous black outline and no fill. The given shape will be cloned to prevent the original from being modified.
      • BasicFigure

        @Deprecated
        public BasicFigure​(java.awt.Shape shape,
                           int lineWidth)
        Deprecated.
        Use the float constructor instead.
        Create a new figure with the given shape and outline width. It has no fill. The default outline paint is black. The given shape will be cloned to prevent the original from being modified.
      • BasicFigure

        public BasicFigure​(java.awt.Shape shape,
                           float lineWidth)
        Create a new figure with the given shape and outline width. It has no fill. The default outline paint is black. The given shape will be cloned to prevent the original from being modified.
      • BasicFigure

        public BasicFigure​(java.awt.Shape shape,
                           java.awt.Paint fill)
        Create a new figure with the given paint pattern. The figure, by default, has no stroke. The given shape will be cloned to prevent the original from being modified.
      • BasicFigure

        public BasicFigure​(java.awt.Shape shape,
                           java.awt.Paint fill,
                           float lineWidth)
        Create a new figure with the given paint pattern and line width. The given shape will be cloned to prevent the original from being modified.
    • Method Detail

      • getBounds

        public java.awt.geom.Rectangle2D getBounds()
        Get the bounding box of this figure. This method overrides the inherited method to take account of the thickness of the stroke, if there is one.
        Specified by:
        getBounds in interface Figure
        Overrides:
        getBounds in class AbstractFigure
        Returns:
        the bounding box of this figure.
      • getComposite

        public java.awt.Composite getComposite()
        Get the compositing operator
      • getDashArray

        public float[] getDashArray()
        Get the dash array. If the stroke is not a BasicStroke then null will always be returned.
      • getFillPaint

        public java.awt.Paint getFillPaint()
        Get the fill paint
      • getLineWidth

        public float getLineWidth()
        Get the line width. If the stroke is not a BasicStroke then 1.0 will always be returned.
      • getOrigin

        public java.awt.geom.Point2D getOrigin()
        Return the origin of the figure in the enclosing transform context. This overrides the base class to return the center of the shape, if the figure is centered, or the origin of the shape if the figure is not centered.
        Specified by:
        getOrigin in interface Figure
        Overrides:
        getOrigin in class AbstractFigure
        Returns:
        The origin of the figure.
        See Also:
        AbstractFigure.getBounds()
      • getRotation

        public double getRotation()
        Get the angle of rotation of the figure.
        Returns:
        The cumulative rotation angle.
        See Also:
        setRotation(double)
      • getShape

        public java.awt.Shape getShape()
        Get the shape of this figure.
        Specified by:
        getShape in interface Figure
        Specified by:
        getShape in class AbstractFigure
        Returns:
        the outline shape of this figure
      • getStrokePaint

        public java.awt.Paint getStrokePaint()
        Get the paint used to stroke this figure
      • hit

        public boolean hit​(java.awt.geom.Rectangle2D r)
        Test if this figure intersects the given rectangle. If there is a fill but no outline, then there is a hit if the shape is intersected. If there is an outline but no fill, then the area covered by the outline stroke is tested. If there is both a fill and a stroke, the region bounded by the outside edge of the stroke is tested. If there is neither a fill nor a stroke, then return false. If the figure is not visible, always return false.
        Specified by:
        hit in interface Figure
        Overrides:
        hit in class AbstractFigure
        Parameters:
        r - The rectangle to be checked.
        Returns:
        true if the figure is hit by the given rectangle.
      • isCentered

        public boolean isCentered()
        Return whether the figure should be centered on its origin.
        Returns:
        False If the origin of the figure, as returned by getOrigin(), is the upper left corner.
        See Also:
        getOrigin(), setCentered(boolean)
      • paint

        public void paint​(java.awt.Graphics2D g)
        Paint the figure. The figure is redrawn with the current shape, fill, and outline.
        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.
      • setCentered

        public void setCentered​(boolean centered)
        Specify whether the figure should be centered on its origin. By default, it is.
        Parameters:
        centered - False to make the origin of the figure, as returned by getOrigin(), be the upper left corner.
        See Also:
        getOrigin()
      • setComposite

        public void setComposite​(java.awt.AlphaComposite c)
        Set the compositing operation for this figure.
      • setDashArray

        public void setDashArray​(float[] dashArray)
        Set the dash array of the stroke. The existing stroke will be removed, but the line width will be preserved if possible.
      • setFillPaint

        public void setFillPaint​(java.awt.Paint p)
        Set the fill paint. If p is null then the figure will not be filled.
      • setLineWidth

        public void setLineWidth​(float lineWidth)
        Set the line width. The existing stroke will be removed, but the dash array will be preserved if possible.
      • setPrototypeShape

        public void setPrototypeShape​(java.awt.Shape s)
        Change the shape of the figure without modifying its other properties, such as its position. If you are writing client code that is modifying the appearance of the Figure after it is created, this is most likely the method you want to call, as opposed to setShape().
      • setRotation

        public void setRotation​(double angle)
        Set the rotation angle of the figure to the specified angle in radians.
        Parameters:
        angle - The angle of rotation.
        See Also:
        getRotation()
      • setShape

        public void setShape​(java.awt.Shape s)
        Set the shape of this figure. Note that this method is primarily intended to be used by interactors that modify the figures shape (such as PathManipulator). In particular, the position of the figure is likely to move. In order to change the shape of the figure without moving the figure, use the setPrototypeShape() method instead.
        Specified by:
        setShape in interface ShapedFigure
      • setStrokePaint

        public void setStrokePaint​(java.awt.Paint p)
        Set the stroke paint
      • setStroke

        public void setStroke​(java.awt.Stroke s)
        Set the stroke
      • transform

        public void transform​(java.awt.geom.AffineTransform at)
        Transform the figure with the supplied transform. This can be used to perform arbitrary translation, scaling, shearing, and rotation operations. As much as possible, this method attempts to preserve the type of the shape: if the shape of this figure is an of RectangularShape or Polyline, then the shape may be modified directly. Otherwise, a general transformation is used that loses the type of the shape, converting it into a GeneralPath.
        Specified by:
        transform in interface Figure
        Specified by:
        transform in class AbstractFigure
        Parameters:
        at - The transform to be used.