Package edu.macalester.graphics
Class GraphicsGroup
java.lang.Object
edu.macalester.graphics.GraphicsObject
edu.macalester.graphics.GraphicsGroup
- All Implemented Interfaces:
GraphicsObserver
A group of graphical objects that can be added, moved, and removed as a single unit.
The group defines its own coordinate system, so the positions of objects added to it are relative
to the whole group's position.
Calling GraphicsObject.setPosition(edu.macalester.graphics.Point)
on a GraphicsGroup sets where the group’s local (0,0) shows up
within its parent. This means that a group’s position is not necessarily the upper left, the
center, or any other fixed relationship with the shapes inside the group. Instead, you determine
how the group’s graphics relate to the whole group’s position when you set the position of each
element within the group.
A graphics group’s size includes the extent of the bounding boxes of all its contents. The size does not necessarily include the origin: a group containing only a 1x1 rectangle at (100, 100) has a width and height of 1.
-
Constructor Summary
ConstructorDescriptionConstructs a new group positioned at (0, 0).GraphicsGroup
(double x, double y) Constructs a new group. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(GraphicsObject gObject) Adds given graphical object to the list of objects drawn on the canvas.void
add
(GraphicsObject gObject, double x, double y) Adds the graphical object to the list of objects drawn on the canvas at the position x, y.protected void
changed()
Triggers a notifications to observers that this object's appearance has changed.protected void
For internal use.Returns the bounding box of this graphics object in its local coordinates.Returns the topmost graphical object that touches the given position.getElementAtLocalCoordinates
(double x, double y) Returns the topmost child of this group that touches position x, y.protected Object
For internal use only.void
graphicChanged
(GraphicsObject changedObject) Implementation of GraphicsObserver method.iterator()
Returns an iterator over the contents of this group, in the order they will be drawn.void
remove
(GraphicsObject gObject) Removes the object from being drawnvoid
Removes all of the objects in this groupboolean
testHitInLocalCoordinates
(double x, double y) Tests whether the point (x, y) hits some shape inside this group.toString()
Methods inherited from class edu.macalester.graphics.GraphicsObject
addObserver, equals, getAnchor, getBoundsInParent, getCanvas, getCenter, getElementAt, getEmbeddedComponent, getHeight, getPosition, getRotation, getScale, getScaleX, getScaleY, getSize, getSizeInParent, getWidth, getX, getY, hashCode, isInBounds, moveBy, moveBy, removeObserver, renderToBuffer, rotateBy, setAnchor, setAnchor, setCenter, setCenter, setPosition, setPosition, setRotation, setScale, setScale, setScale, setX, setY, testHit
-
Constructor Details
-
GraphicsGroup
public GraphicsGroup(double x, double y) Constructs a new group. Each group has its own local coordinate system. The group is positioned on the canvas at canvas position (x, y) when it is added. -
GraphicsGroup
public GraphicsGroup()Constructs a new group positioned at (0, 0). When later used with CanvasWindow's add(GraphicsObject gObject, double x, double y), this group will get placed at x, y.
-
-
Method Details
-
add
Adds given graphical object to the list of objects drawn on the canvas. The last object added is the one that will appear on top. -
add
Adds the graphical object to the list of objects drawn on the canvas at the position x, y.- Parameters:
gObject
- the graphical object to be drawnx
- the x position of graphical objecty
- the y position of graphical object
-
remove
Removes the object from being drawn- Throws:
NoSuchElementException
- if gObject is not a part of the graphics group.
-
removeAll
public void removeAll()Removes all of the objects in this group -
getElementAt
Returns the topmost graphical object that touches the given position. If no such object exists, returns null.- Parameters:
p
- position in the coordinate space of the container of this group- Returns:
- object at (x,y) or null if it does not exist.
-
getElementAtLocalCoordinates
Returns the topmost child of this group that touches position x, y. If no such object exists, returns null. A GraphicsGroup will only return child elements; it never returns itself.- Overrides:
getElementAtLocalCoordinates
in classGraphicsObject
- See Also:
-
drawInLocalCoordinates
Description copied from class:GraphicsObject
For internal use. Draws this graphics object on the screen in its local coordinates, without rotation or scaling.- Specified by:
drawInLocalCoordinates
in classGraphicsObject
-
testHitInLocalCoordinates
public boolean testHitInLocalCoordinates(double x, double y) Tests whether the point (x, y) hits some shape inside this group.- Specified by:
testHitInLocalCoordinates
in classGraphicsObject
-
getBounds
Description copied from class:GraphicsObject
Returns the bounding box of this graphics object in its local coordinates.- Specified by:
getBounds
in classGraphicsObject
-
iterator
Returns an iterator over the contents of this group, in the order they will be drawn. -
changed
protected void changed()Description copied from class:GraphicsObject
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.- Overrides:
changed
in classGraphicsObject
-
graphicChanged
Implementation of GraphicsObserver method. Notifies Java to repaint the image if any of the objects drawn on the canvas have changed.- Specified by:
graphicChanged
in interfaceGraphicsObserver
-
getEqualityAttributes
Description copied from class:GraphicsObject
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.- Specified by:
getEqualityAttributes
in classGraphicsObject
-
toString
-