Class GraphicsObject

java.lang.Object
edu.macalester.graphics.GraphicsObject
Direct Known Subclasses:
Arc, Button, Ellipse, GraphicsGroup, GraphicsText, Image, Line, Path, Rectangle, TextField

public abstract class GraphicsObject extends Object
Abstract class representing some sort of graphical object that can be drawn and positioned on a canvas.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    final void
    Adds an observer to be notified of visual changes to this graphics object (typically for the purpose of knowing when to draw it).
    protected void
    Triggers a notifications to observers that this object's appearance has changed.
    protected abstract void
    For internal use.
    final boolean
    equals(Object other)
    Two GraphicsObjects are equal if are of the same class and have the same shape (position, rotation, scale, vertices), regardless of appearance (color and stroke width).
    The point relative to which scaling and rotation occur.
    abstract Rectangle2D
    Returns the bounding box of this graphics object in its local coordinates.
    Returns an axis-aligned bounding rectangle for this graphics object in its parent’s coordinate space, accounting for this object’s scaling and rotation.
    Returns the window that this Object is inside, or null if it does not belong to a window.
    final Point
    Returns the center of this shape's bounding box.
    getElementAt(double x, double y)
    Returns the topmost graphical object that touches the given position in the parent’s coordinate space.
    getElementAtLocalCoordinates(double x, double y)
    Returns the topmost graphical object that touches the given position in this object’s coordinate space.
    For internal use only.
    protected abstract Object
    For internal use only.
    final double
    Returns the height of this graphics object, in its local coordinates.
    final Point
    Gets the position of the object on the canvas.
    final double
    This graphic object’s visual rotation in degrees from its neutral position.
    final Point
    Returns the scaling factor for this graphics object.
    final double
    Returns the horizontal scaling factor for this graphics object.
    final double
    Returns the vertical scaling factor for this graphics object.
    final Point
    Returns the width and height of this graphics object, in its local coordinates.
    final Point
    Returns an upper limit on the width and height of this graphics object as it appears in its parent’s coordinate space, after applying this object’s scaling and rotation.
    final double
    Returns the width of this graphics object, in its local coordinates.
    final double
    Returns the object's current horizontal position.
    final double
    Returns the object's current vertical position.
    final int
     
    final boolean
    isInBounds(Point position)
    Returns true if the given point in the parent's coordinate space is within the bounds of this object.
    final void
    moveBy(double dx, double dy)
    Move the shape from its current (x, y) position to (x + dx, y + dy).
    final void
    moveBy(Point delta)
    Offsets the shape from its current position within its parent by the given delta.
    final void
    Removes the given observer.
    void
    Renders this graphics object to an offscreen pixel buffer.
    final void
    rotateBy(double angle)
    Rotates the object around its anchor point by the given number of degrees.
    void
    setAnchor(double x, double y)
    Fixes scaling and rotation around the given coordinate.
    void
    setAnchor(Point anchor)
    Changes the point relative to which scaling and rotation occur.
    final void
    setCenter(double x, double y)
    Moves the shape so its bounding box is centered at the given point.
    final void
    Moves the shape so its bounding box is centered at the given point.
    final void
    setPosition(double x, double y)
    Moves this object to the given position.
    final void
    setPosition(Point position)
    Moves this object to the given position.
    final void
    setRotation(double rotation)
    Changes this graphic object’s visual rotation from its neutral position.
    final void
    setScale(double scale)
    Scales this object uniformly, setting both the horizontal and vertical scaling factor to the given value.
    final void
    setScale(double scaleX, double scaleY)
    Stretches / shrinks this graphics object by the given factor.
    final void
    setScale(Point scale)
    Changes this graphics object's scaling factor.
    final void
    setX(double x)
    Changes this object's horizontal position while preserving its vertical position.
    final void
    setY(double y)
    Changes this object's vertical position while preserving its horizontal position.
    final boolean
    testHit(double x, double y)
    Tests whether the given point is on the boundary or interior of this graphic object’s shape.
    abstract boolean
    testHitInLocalCoordinates(double x, double y)
    Tests whether the given point is inside the boundary or interior of this object’s shape.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • GraphicsObject

      public GraphicsObject()
  • Method Details

    • drawInLocalCoordinates

      protected abstract void drawInLocalCoordinates(Graphics2D gc)
      For internal use. Draws this graphics object on the screen in its local coordinates, without rotation or scaling.
    • renderToBuffer

      public void renderToBuffer(BufferedImage image)
      Renders this graphics object to an offscreen pixel buffer.
      Parameters:
      image - The destination buffer.
    • getPosition

      public final Point getPosition()
      Gets the position of the object on the canvas. The “position” is typically the upper left, but this can vary. With text, for example, the y component of the position is the baseline, not the top of the text.
    • setPosition

      public final void setPosition(double x, double y)
      Moves this object to the given position.
      Parameters:
      x - position
      y - position
    • setPosition

      public final void setPosition(Point position)
      Moves this object to the given position.
    • getX

      public final double getX()
      Returns the object's current horizontal position.
      See Also:
    • setX

      public final void setX(double x)
      Changes this object's horizontal position while preserving its vertical position.
    • getY

      public final double getY()
      Returns the object's current vertical position.
      See Also:
    • setY

      public final void setY(double y)
      Changes this object's vertical position while preserving its horizontal position.
    • getCenter

      public final Point getCenter()
      Returns the center of this shape's bounding box.
    • setCenter

      public final void setCenter(double x, double y)
      Moves the shape so its bounding box is centered at the given point.
    • setCenter

      public final void setCenter(Point point)
      Moves the shape so its bounding box is centered at the given point.
    • moveBy

      public final void moveBy(double dx, double dy)
      Move the shape from its current (x, y) position to (x + dx, y + dy).
    • moveBy

      public final void moveBy(Point delta)
      Offsets the shape from its current position within its parent by the given delta.
    • getRotation

      public final double getRotation()
      This graphic object’s visual rotation in degrees from its neutral position. See setRotation() for details.
    • setRotation

      public final void setRotation(double rotation)
      Changes this graphic object’s visual rotation from its neutral position. The offset is in degrees, zero is neutral, and positive is clockwise.

      Rotation affects the entire local coordinate system of the graphics object, so any size parameters or child coordinates are within the rotated frame of reference.

      By default, the object rotates around its center, but you can change this; see setAnchor().

      This is an absolute rotation within the coordinate system of this object's parent. If you want to adjust object’s rotation relative to its current value, use rotateBy().

      See Also:
    • rotateBy

      public final void rotateBy(double angle)
      Rotates the object around its anchor point by the given number of degrees. Positive is clockwise.
      See Also:
    • getScaleX

      public final double getScaleX()
      Returns the horizontal scaling factor for this graphics object.
      See Also:
    • getScaleY

      public final double getScaleY()
      Returns the vertical scaling factor for this graphics object.
      See Also:
    • getScale

      public final Point getScale()
      Returns the scaling factor for this graphics object.
      See Also:
    • setScale

      public final void setScale(Point scale)
      Changes this graphics object's scaling factor.
      See Also:
    • setScale

      public final void setScale(double scaleX, double scaleY)
      Stretches / shrinks this graphics object by the given factor. A scale of 1 is natural size. Either factor can be zero or negative; a negative factor flips the graphics.

      Scaling affects the entire local coordinate system of the graphics object, so any size parameters, stroke sizes, child coordinates, etc. are within the scaled frame of reference.

      By default, the object scales around its center, but you can change this; see setAnchor().

      Scaling always happens before rotation, so for example if you take a circle, call setRotation(45), then call setScale(1, 0.1), you will see a narrow ellipse whose major axis is at a 45° angle.

      Parameters:
      scaleX - The horizontal scale factor. 1 = original size.
      scaleY - The vertical scale factor. 1 = original size.
      See Also:
    • setScale

      public final void setScale(double scale)
      Scales this object uniformly, setting both the horizontal and vertical scaling factor to the given value.
      See Also:
    • getAnchor

      public Point getAnchor()
      The point relative to which scaling and rotation occur. See setAnchor(Point) for details.
    • setAnchor

      public void setAnchor(Point anchor)
      Changes the point relative to which scaling and rotation occur.

      A graphics object’s anchor point is the one point that remains in a fixed position when scale and rotation change. It is in the pre-rotation, pre-scaling local coordinates of this graphics object; it is relative to getPosition() in the parent’s coordinate space.

      By default, the anchor point is the object’s center, and continually changes if the object’s center changes. This can cause unstable motion if the object’s size is changing while it is rotated and/or scaled; in this situation, you will usually want to set a fixed anchor point. To set the anchor back to the default, call setAnchor(null).

      Parameters:
      anchor - The new anchor point, or null to follow the object’s center.
      See Also:
    • setAnchor

      public void setAnchor(double x, double y)
      Fixes scaling and rotation around the given coordinate.
      See Also:
    • testHit

      public final boolean testHit(double x, double y)
      Tests whether the given point is on the boundary or interior of this graphic object’s shape. The point is in the parent object’s local coordinates, not canvas coordinates.

      Does not account for appearance, including stroke width and transparency.

    • testHitInLocalCoordinates

      public abstract boolean testHitInLocalCoordinates(double x, double y)
      Tests whether the given point is inside the boundary or interior of this object’s shape. The point is in this object’s local coordinates.
    • isInBounds

      public final boolean isInBounds(Point position)
      Returns true if the given point in the parent's coordinate space is within the bounds of this object. Note that testHit() actually checks the actual shape, whereas this method only tests the bounds. So, for example, a point in the upper left corner of an Ellipse’s bounding box could return true for isInBounds(), but false for testHit().
    • getElementAt

      public final GraphicsObject getElementAt(double x, double y)
      Returns the topmost graphical object that touches the given position in the parent’s coordinate space. If no such object exists, this method returns null. If this GraphicsObject has child elements, this method might return a child.
      Parameters:
      x - position in the coordinate space of this object’s container
      y - position in the coordinate space of this object’s container
      Returns:
      object at (x,y) or null if it does not exist.
    • getElementAtLocalCoordinates

      public GraphicsObject getElementAtLocalCoordinates(double x, double y)
      Returns the topmost graphical object that touches the given position in this object’s coordinate space.
      See Also:
    • getSize

      public final Point getSize()
      Returns the width and height of this graphics object, in its local coordinates. This does not account for any scaling and rotation of the object.
    • getWidth

      public final double getWidth()
      Returns the width of this graphics object, in its local coordinates.
    • getHeight

      public final double getHeight()
      Returns the height of this graphics object, in its local coordinates.
    • getSizeInParent

      public final Point getSizeInParent()
      Returns an upper limit on the width and height of this graphics object as it appears in its parent’s coordinate space, after applying this object’s scaling and rotation.

      Note that this may be an overestimate because it describes the size of the transformed bounding box, not the transformed shape. So, for example, a circle’s transformed size grows larger when the circle is rotated 45°.

    • getBoundsInParent

      public final Rectangle2D getBoundsInParent()
      Returns an axis-aligned bounding rectangle for this graphics object in its parent’s coordinate space, accounting for this object’s scaling and rotation.

      Note that this may be an overestimate because it describes the size of the transformed bounding box, not the transformed shape. So, for example, a circle’s transformed bounding box grows larger when the circle is rotated 45°.

    • getBounds

      public abstract Rectangle2D getBounds()
      Returns the bounding box of this graphics object in its local coordinates.
    • getCanvas

      public final CanvasWindow getCanvas()
      Returns the window that this Object is inside, or null if it does not belong to a window.
    • getEmbeddedComponent

      public JComponent getEmbeddedComponent()
      For internal use only. Supports UI components.
    • equals

      public final boolean equals(Object other)
      Two GraphicsObjects are equal if are of the same class and have the same shape (position, rotation, scale, vertices), regardless of appearance (color and stroke width).
      Overrides:
      equals in class Object
    • hashCode

      public final int hashCode()
      Overrides:
      hashCode in class Object
    • getEqualityAttributes

      protected abstract Object getEqualityAttributes()
      For internal use only. Used to compute equals() and hashCode(). Returns an object whose equals() and hashCode() methods encompass the subclass-specific values that should be used to compute equality for the whole GraphicsObject.
    • addObserver

      public final void addObserver(GraphicsObserver observer)
      Adds an observer to be notified of visual changes to this graphics object (typically for the purpose of knowing when to draw it).
    • removeObserver

      public final void removeObserver(GraphicsObserver observer)
      Removes the given observer.
    • changed

      protected void changed()
      Triggers a notifications to observers that this object's appearance has changed. Subclasses should call this whenever anything changes that would alter this object's appearance.