Class GraphicsObject
- Direct Known Subclasses:
Arc,Button,Ellipse,GraphicsGroup,GraphicsText,Image,Line,Path,Rectangle,TextField
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal voidaddObserver(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).protected voidchanged()Triggers a notifications to observers that this object's appearance has changed.protected abstract voidFor internal use.final booleanTwo 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 Rectangle2DReturns the bounding box of this graphics object in its local coordinates.final Rectangle2DReturns 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 PointReturns the center of this shape's bounding box.final GraphicsObjectgetElementAt(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 ObjectFor internal use only.final doubleReturns the height of this graphics object, in its local coordinates.Returns the group that contains this graphics object, or null if it does not belong to a group.final PointGets the position of the object on the canvas.final doubleThis graphic object’s visual rotation in degrees from its neutral position.final PointgetScale()Returns the scaling factor for this graphics object.final doubleReturns the horizontal scaling factor for this graphics object.final doubleReturns the vertical scaling factor for this graphics object.final PointgetSize()Returns the width and height of this graphics object, in its local coordinates.final PointReturns 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 doublegetWidth()Returns the width of this graphics object, in its local coordinates.final doublegetX()Returns the object's current horizontal position.final doublegetY()Returns the object's current vertical position.final inthashCode()final booleanisInBounds(Point position) Returns true if the given point in the parent's coordinate space is within the bounds of this object.final voidmoveBy(double dx, double dy) Move the shape from its current (x, y) position to (x + dx, y + dy).final voidOffsets the shape from its current position within its parent by the given delta.final voidremoveObserver(GraphicsObserver observer) Removes the given observer.voidrenderToBuffer(BufferedImage image) Renders this graphics object to an offscreen pixel buffer.final voidrotateBy(double angle) Rotates the object around its anchor point by the given number of degrees.voidsetAnchor(double x, double y) Fixes scaling and rotation around the given coordinate.voidChanges the point relative to which scaling and rotation occur.final voidsetCenter(double x, double y) Moves the shape so its bounding box is centered at the given point.final voidMoves the shape so its bounding box is centered at the given point.final voidsetPosition(double x, double y) Moves this object to the given position.final voidsetPosition(Point position) Moves this object to the given position.final voidsetRotation(double rotation) Changes this graphic object’s visual rotation from its neutral position.final voidsetScale(double scale) Scales this object uniformly, setting both the horizontal and vertical scaling factor to the given value.final voidsetScale(double scaleX, double scaleY) Stretches / shrinks this graphics object by the given factor.final voidChanges this graphics object's scaling factor.final voidsetX(double x) Changes this object's horizontal position while preserving its vertical position.final voidsetY(double y) Changes this object's vertical position while preserving its horizontal position.final booleantestHit(double x, double y) Tests whether the given point is on the boundary or interior of this graphic object’s shape.abstract booleantestHitInLocalCoordinates(double x, double y) Tests whether the given point is inside the boundary or interior of this object’s shape.
-
Constructor Details
-
GraphicsObject
public GraphicsObject()
-
-
Method Details
-
drawInLocalCoordinates
For internal use. Draws this graphics object on the screen in its local coordinates, without rotation or scaling. -
renderToBuffer
Renders this graphics object to an offscreen pixel buffer.- Parameters:
image- The destination buffer.
-
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- positiony- position
-
setPosition
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
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
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
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. SeesetRotation()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
Returns the scaling factor for this graphics object.- See Also:
-
setScale
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 callsetScale(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
The point relative to which scaling and rotation occur. SeesetAnchor(Point)for details. -
setAnchor
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
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
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 containery- position in the coordinate space of this object’s container- Returns:
- object at (x,y) or null if it does not exist.
-
getElementAtLocalCoordinates
Returns the topmost graphical object that touches the given position in this object’s coordinate space.- See Also:
-
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
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
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
Returns the bounding box of this graphics object in its local coordinates. -
getParent
Returns the group that contains this graphics object, or null if it does not belong to a group. -
getCanvas
Returns the window that this Object is inside, or null if it does not belong to a window. -
getEmbeddedComponent
For internal use only. Supports UI components. -
equals
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). -
hashCode
public final int hashCode() -
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
Adds an observer to be notified of visual changes to this graphics object (typically for the purpose of knowing when to draw it). -
removeObserver
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.
-