acm.graphics
Class GCompound

java.lang.Object
  extended by acm.graphics.GObject
      extended by acm.graphics.GCompound
All Implemented Interfaces:
GContainer, GScalable, Serializable, Cloneable

public class GCompound
extends GObject
implements GContainer, GScalable

This class defines a graphical object that consists of a collection of other graphical objects. Once assembled, the internal objects can be manipulated as a unit.

See Also:
Serialized Form

Field Summary
 
Fields inherited from interface acm.graphics.GContainer
BACK_TO_FRONT, FRONT_TO_BACK
 
Constructor Summary
GCompound()
          Creates a new GCompound object with no internal components.
 
Method Summary
 void add(GObject gobj)
          Adds a new graphical object to this GCompound.
 void add(GObject gobj, double x, double y)
          Adds the graphical object to this canvas and sets its location to the point (xy).
 void add(GObject gobj, GPoint pt)
          Adds the graphical object to this canvas and sets its location to the specified point.
protected  boolean areMouseListenersEnabled()
          Returns true if mouse listeners have ever been assigned to this object or to any of the contained objects.
 boolean contains(double x, double y)
          Checks to see whether a point is "inside" the compound, which means that it is inside one of the components.
protected  void fireMouseListeners(MouseEvent e)
          Dispatches a mouse event to the topmost child that covers the location in the event e.
 GRectangle getBounds()
          Returns the bounding rectangle for this compound object, which consists of the union of the bounding rectangles for each of the components.
 GPoint getCanvasPoint(double x, double y)
          Converts the location of the specified point in this compound to the corresponding point in the enclosing canvas.
 GPoint getCanvasPoint(GPoint localPoint)
          Converts the location of the specified point in this compound to the corresponding point in the enclosing canvas.
 GObject getElement(int index)
          Returns the graphical object at the specified index, numbering from back to front in the the z dimension.
 GObject getElementAt(double x, double y)
          Returns the topmost graphical object that contains the point (x, y), or null if no such object exists.
 GObject getElementAt(GPoint pt)
          Returns the topmost graphical object that contains the specified point, or null if no such object exists.
 int getElementCount()
          Returns the number of graphical objects stored in this container.
 GPoint getLocalPoint(double x, double y)
          Converts the specified point on the enclosing canvas to the corresponding point in the space of this compound.
 GPoint getLocalPoint(GPoint canvasPoint)
          Converts the location of the specified point on the enclosing canvas to the corresponding point in the space of this compound.
 Iterator<GObject> iterator()
          Returns an Iterator that cycles through the elements within this container in the default direction, which is from back to front.
 Iterator<GObject> iterator(int direction)
          Returns an Iterator that cycles through the elements within this container in the specified direction, which must be one of the constants GContainer.FRONT_TO_BACK or GContainer.BACK_TO_FRONT.
 void markAsComplete()
          Calling this method makes it illegal to add or remove elements from the compound object.
 void paint(Graphics g)
          Implements the paint operation for this graphical object.
 void remove(GObject gobj)
          Removes a graphical object from this GCompound.
 void removeAll()
          Removes all graphical objects from this GCompound.
 void scale(double sf)
          Scales the object on the screen by the scale factor sf, which applies in both dimensions.
 void scale(double sx, double sy)
          Scales every object contained in this compound by the scale factors sx and sy.
protected  void sendBackward(GObject gobj)
          Implements the sendBackward function from the GContainer interface.
protected  void sendForward(GObject gobj)
          Implements the sendForward function from the GContainer interface.
protected  void sendToBack(GObject gobj)
          Implements the sendToBack function from the GContainer interface.
protected  void sendToFront(GObject gobj)
          Implements the sendToFront function from the GContainer interface.
 
Methods inherited from class acm.graphics.GObject
addActionListener, addMouseListener, addMouseMotionListener, colorName, contains, fireActionEvent, fireActionEvent, getColor, getComponent, getHeight, getLocation, getObjectColor, getParent, getSize, getWidth, getX, getY, isVisible, move, movePolar, paintObject, paramString, pause, removeActionListener, removeMouseListener, removeMouseMotionListener, repaint, sendBackward, sendForward, sendToBack, sendToFront, setColor, setLocation, setLocation, setParent, setVisible, start, start, toString, updateEnabledList
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GCompound

public GCompound()
Creates a new GCompound object with no internal components.

Method Detail

add

public void add(GObject gobj)
Adds a new graphical object to this GCompound.

Specified by:
add in interface GContainer
Parameters:
gobj - The graphical object to add

add

public final void add(GObject gobj,
                      double x,
                      double y)
Adds the graphical object to this canvas and sets its location to the point (xy).

Specified by:
add in interface GContainer
Parameters:
gobj - The graphical object to add
x - The new x-coordinate for the object
y - The new y-coordinate for the object

add

public final void add(GObject gobj,
                      GPoint pt)
Adds the graphical object to this canvas and sets its location to the specified point.

Specified by:
add in interface GContainer
Parameters:
gobj - The graphical object to add
pt - A GPoint object giving the coordinates of the point

areMouseListenersEnabled

protected boolean areMouseListenersEnabled()
Returns true if mouse listeners have ever been assigned to this object or to any of the contained objects.

Overrides:
areMouseListenersEnabled in class GObject
Returns:
true if mouse listeners have been enabled in this object

contains

public boolean contains(double x,
                        double y)
Checks to see whether a point is "inside" the compound, which means that it is inside one of the components.

Overrides:
contains in class GObject
Parameters:
x - The x-coordinate of the point being tested
y - The y-coordinate of the point being tested
Returns:
true if the point (xy) is inside the compound, and false otherwise

fireMouseListeners

protected void fireMouseListeners(MouseEvent e)
Dispatches a mouse event to the topmost child that covers the location in the event e.

Overrides:
fireMouseListeners in class GObject
Parameters:
e - The MouseEvent that triggered this response

getBounds

public GRectangle getBounds()
Returns the bounding rectangle for this compound object, which consists of the union of the bounding rectangles for each of the components.

Specified by:
getBounds in class GObject
Returns:
A GRectangle that bounds the components of this object

getCanvasPoint

public GPoint getCanvasPoint(double x,
                             double y)
Converts the location of the specified point in this compound to the corresponding point in the enclosing canvas.

Parameters:
x - The x coordinate in the space of the compound
y - The y coordinate in the space of the compound
Returns:
The coordinates in the space of the enclosing GCanvas

getCanvasPoint

public final GPoint getCanvasPoint(GPoint localPoint)
Converts the location of the specified point in this compound to the corresponding point in the enclosing canvas.

Parameters:
localPoint - The coordinates in the space of the compound
Returns:
The coordinates in the space of the enclosing GCanvas

getElement

public GObject getElement(int index)
Returns the graphical object at the specified index, numbering from back to front in the the z dimension.

Specified by:
getElement in interface GContainer
Parameters:
index - The index of the component to return
Returns:
The graphical object at the specified index

getElementAt

public GObject getElementAt(double x,
                            double y)
Returns the topmost graphical object that contains the point (x, y), or null if no such object exists. Note that these coordinates are relative to the location of the compound object and not to the canvas in which it is displayed.

Specified by:
getElementAt in interface GContainer
Parameters:
x - The x-coordinate of the point being tested
y - The y-coordinate of the point being tested
Returns:
The graphical object at the specified location, or null if no such object exists

getElementAt

public final GObject getElementAt(GPoint pt)
Returns the topmost graphical object that contains the specified point, or null if no such object exists.

Specified by:
getElementAt in interface GContainer
Parameters:
pt - The coordinates being tested
Returns:
The graphical object at the specified location, or null if no such object exists

getElementCount

public int getElementCount()
Returns the number of graphical objects stored in this container.

Specified by:
getElementCount in interface GContainer
Returns:
The number of graphical objects in this container

getLocalPoint

public GPoint getLocalPoint(double x,
                            double y)
Converts the specified point on the enclosing canvas to the corresponding point in the space of this compound.

Parameters:
x - The x coordinate in the space of the space of the enclosing GCanvas
y - The y coordinate in the space of the space of the enclosing GCanvas
Returns:
The coordinates in the space of the compound

getLocalPoint

public final GPoint getLocalPoint(GPoint canvasPoint)
Converts the location of the specified point on the enclosing canvas to the corresponding point in the space of this compound.

Parameters:
canvasPoint - The coordinates in the space of the enclosing GCanvas
Returns:
The coordinates in the space of the compound

iterator

public Iterator<GObject> iterator()
Returns an Iterator that cycles through the elements within this container in the default direction, which is from back to front. You can also run the iterator in the opposite direction by using the iterator(direction) form of this method.

Returns:
An Iterator ranging over the elements of the container from back to front

iterator

public Iterator<GObject> iterator(int direction)
Returns an Iterator that cycles through the elements within this container in the specified direction, which must be one of the constants GContainer.FRONT_TO_BACK or GContainer.BACK_TO_FRONT.


      for (Iterator<GObject> i = gc.iterator(direction); i.hasNext(); )
 

Returns:
An Iterator ranging over the elements of the container in the specified direction

markAsComplete

public void markAsComplete()
Calling this method makes it illegal to add or remove elements from the compound object. Subclasses can invoke this method to protect the integrity of the structure from changes by the client.


paint

public void paint(Graphics g)
Implements the paint operation for this graphical object. This method is not called directly by clients.

Specified by:
paint in class GObject
Parameters:
g - The graphics context into which the painting is done

remove

public void remove(GObject gobj)
Removes a graphical object from this GCompound.

Specified by:
remove in interface GContainer
Parameters:
gobj - The graphical object to remove

removeAll

public void removeAll()
Removes all graphical objects from this GCompound.

Specified by:
removeAll in interface GContainer

scale

public final void scale(double sf)
Scales the object on the screen by the scale factor sf, which applies in both dimensions.

Specified by:
scale in interface GScalable
Parameters:
sf - The factor used to scale all coordinates in both dimensions

scale

public void scale(double sx,
                  double sy)
Scales every object contained in this compound by the scale factors sx and sy. Automatic repaint is turned off during the scaling operation so that at most one repaint is performed.

Specified by:
scale in interface GScalable
Parameters:
sx - The factor used to scale all coordinates in the x direction
sy - The factor used to scale all coordinates in the y direction

sendBackward

protected void sendBackward(GObject gobj)
Implements the sendBackward function from the GContainer interface. Clients should not be calling this method, but the semantics of interfaces forces it to be exported.


sendForward

protected void sendForward(GObject gobj)
Implements the sendForward function from the GContainer interface. Clients should not be calling this method, but the semantics of interfaces forces it to be exported.


sendToBack

protected void sendToBack(GObject gobj)
Implements the sendToBack function from the GContainer interface. Clients should not be calling this method, but the semantics of interfaces forces it to be exported.


sendToFront

protected void sendToFront(GObject gobj)
Implements the sendToFront function from the GContainer interface. Clients should not be calling this method, but the semantics of interfaces forces it to be exported.