acm.graphics
Class GCompound

java.lang.Object
  |
  +--acm.graphics.GObject
        |
        +--acm.graphics.GCompound

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.


 
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.
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.
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 iterator()
Returns an Iterator that cycles through the elements within this container in the default direction, which is from back to front.
Iterator 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 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.
 
Inherited Method Summary
voidaddMouseListener(MouseListener listener)
Adds a mouse listener to this graphical object.
voidaddMouseMotionListener(MouseMotionListener listener)
Adds a mouse motion listener to this graphical object.
booleancontains(GPoint pt)
Checks to see whether a point is inside the object.
doublegetHeight()
Returns the height of this object, which is defined to be the height of the bounding box.
GPointgetLocation()
Returns the location of this object as a GPoint.
GDimensiongetSize()
Returns the size of the bounding box for this object.
doublegetWidth()
Returns the width of this object, which is defined to be the width of the bounding box.
doublegetX()
Returns the x-coordinate of the object.
doublegetY()
Returns the y-coordinate of the object.
booleanisVisible()
Checks to see whether the object is visible.
voidmove(double dx, double dy)
Moves the object on the screen using the displacements dx and dy.
voidmovePolar(double r, double theta)
Moves the object using displacements given in polar coordinates.
voidremoveMouseListener(MouseListener listener)
Removes a mouse listener from this graphical object.
voidremoveMouseMotionListener(MouseMotionListener listener)
Removes a mouse motion listener from this graphical object.
voidsendBackward()
Moves this object one step toward the back in the z dimension.
voidsendForward()
Moves this object one step toward the front in the z dimension.
voidsendToBack()
Moves this object to the back of the display in the z dimension.
voidsendToFront()
Moves this object to the front of the display in the z dimension.
voidsetLocation(GPoint pt)
Sets the location of this object to the specified point.
voidsetLocation(double x, double y)
Sets the location of the GCompound to the point (x, y).
voidsetVisible(boolean visible)
Sets the visibility status of the GCompound.
 

Constructor Detail

public GCompound()

Creates a new GCompound object with no internal components.

 
Usage: GCompound gcomp = new GCompound(); 
 
Method Detail

public void add(GObject gobj)

Adds a new graphical object to this GCompound.

 
Usage: gcomp.add(gobj); 
Parameter: 
gobj  The graphical object to add
Specified by: add in interface GContainer
 

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).

 
Usage: gc.add(gobj, x, y); 
Parameters: 
gobj  The graphical object to add
 The new x-coordinate for the object
 The new y-coordinate for the object
Specified by: add in interface GContainer
 

public final void add(GObject gobj, GPoint pt)

Adds the graphical object to this canvas and sets its location to the specified point.

 
Usage: gc.add(gobj, pt); 
Parameters: 
gobj  The graphical object to add
pt  A GPoint object giving the coordinates of the point
Specified by: add in interface GContainer
 

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.

 
Usage: if (gcomp.contains(x, y)) . . . 
Parameters: 
 The x-coordinate of the point being tested
 The y-coordinate of the point being tested
Returns: true if the point (xy) is inside the compound, and false otherwise
 

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.

 
Usage: GRectangle bounds = gcomp.getBounds(); 
Returns: A GRectangle that bounds the components of this object
 

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.

 
Usage: canvasPoint = gcomp.getCanvasPoint(x, y); 
Parameters: 
 The x coordinate in the space of the compound
 The y coordinate in the space of the compound
Returns: The coordinates in the space of the enclosing GCanvas
 

public final GPoint getCanvasPoint(GPoint localPoint)

Converts the location of the specified point in this compound to the corresponding point in the enclosing canvas.

 
Usage: canvasPoint = gcomp.getCanvasPoint(localPoint); 
Parameter: 
localPoint  The coordinates in the space of the compound
Returns: The coordinates in the space of the enclosing GCanvas
 

public GObject getElement(int index)

Returns the graphical object at the specified index, numbering from back to front in the the z dimension.

 
Usage: GObject gobj = gcomp.getElement(index); 
Parameter: 
index  The index of the component to return
Returns: The graphical object at the specified index
Specified by: getElement in interface GContainer
 

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.

 
Usage: GObject gobj = gcomp.getElementAt(x, y); 
Parameters: 
 The x-coordinate of the point being tested
 The y-coordinate of the point being tested
Returns: The graphical object at the specified location, or nullif no such object exists
Specified by: getElementAt in interface GContainer
 

public final GObject getElementAt(GPoint pt)

Returns the topmost graphical object that contains the specified point, or null if no such object exists.

 
Usage: GObject gobj = gc.getElementAt(pt); 
Parameter: 
pt  The coordinates being tested
Returns: The graphical object at the specified location, or nullif no such object exists
Specified by: getElementAt in interface GContainer
 

public int getElementCount()

Returns the number of graphical objects stored in this container.

 
Usage: int n = gcomp.getElementCount(); 
Returns: The number of graphical objects in this container
Specified by: getElementCount in interface GContainer
 

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.

 
Usage: localPoint = gcomp.getCanvasPoint(x, y); 
Parameters: 
 The x coordinate in the space of the space of the enclosing GCanvas
 The y coordinate in the space of the space of the enclosing GCanvas
Returns: The coordinates in the space of the compound
 

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.

 
Usage: localPoint = gcomp.getLocalPoint(canvasPoint); 
Parameter: 
canvasPoint  The coordinates in the space of the enclosing GCanvas
Returns: The coordinates in the space of the compound
 

public Iterator 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.

Applets that want to run in browsers, however, should avoid using this method, because Iterator is not supported on 1.1 browsers. For maximum portability, you should rely instead on the getElementCount and getElement methods, which provide the same functionality in a browser-compatible way.


 
Usage: Iterator i = gc.iterator(); 
Returns: An Iterator ranging over the elements of the container from back to front
 

public Iterator 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 i = gc.iterator(direction); i.hasNext(); )

Applets that want to run in browsers, however, should avoid using this method, because Iterator is not supported on 1.1 browsers. For maximum portability, you should rely instead on the getElementCount and getElement methods, which provide the same functionality in a browser-compatible way.


 
Usage: Iterator i = gc.iterator(direction); 
Returns: An Iterator ranging over the elements of the container in the specified direction
 

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.

 
Usage: gcomp.markAsComplete(); 
 

public void remove(GObject gobj)

Removes a graphical object from this GCompound.

 
Usage: gcomp.remove(gobj); 
Parameter: 
gobj  The graphical object to remove
Specified by: remove in interface GContainer
 

public void removeAll()

Removes all graphical objects from this GCompound.

 
Usage: gcomp.removeAll(); 
Specified by: removeAll in interface GContainer
 

public final void scale(double sf)

Scales the object on the screen by the scale factor sf, which applies in both dimensions.

 
Usage: gcomp.scale(sf); 
Parameter: 
sf  The factor used to scale all coordinates in both dimensions
Specified by: scale in interface GScalable
 

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.

 
Usage: gcomp.scale(sx, sy); 
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
Specified by: scale in interface GScalable