Class GraphicsGroup

java.lang.Object
edu.macalester.graphics.GraphicsObject
edu.macalester.graphics.GraphicsGroup
All Implemented Interfaces:
GraphicsObserver

public class GraphicsGroup extends GraphicsObject implements 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 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

      public void add(GraphicsObject gObject)
      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

      public 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.
      Parameters:
      gObject - the graphical object to be drawn
      x - the x position of graphical object
      y - the y position of graphical object
    • remove

      public void remove(GraphicsObject gObject)
      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

      public GraphicsObject getElementAt(Point p)
      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

      public GraphicsObject getElementAtLocalCoordinates(double x, double y)
      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 class GraphicsObject
      See Also:
    • drawInLocalCoordinates

      protected void drawInLocalCoordinates(Graphics2D gc)
      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 class GraphicsObject
    • testHitInLocalCoordinates

      public boolean testHitInLocalCoordinates(double x, double y)
      Tests whether the point (x, y) hits some shape inside this group.
      Specified by:
      testHitInLocalCoordinates in class GraphicsObject
    • getBounds

      public Rectangle2D getBounds()
      Description copied from class: GraphicsObject
      Returns the bounding box of this graphics object in its local coordinates.
      Specified by:
      getBounds in class GraphicsObject
    • iterator

      public Iterator<GraphicsObject> 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 class GraphicsObject
    • graphicChanged

      public void graphicChanged(GraphicsObject changedObject)
      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 interface GraphicsObserver
    • getEqualityAttributes

      protected Object 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 class GraphicsObject
    • toString

      public String toString()
      Overrides:
      toString in class Object