acm.graphics
Class GCanvas

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--acm.graphics.GCanvas

public class GCanvas extends Container implements GContainer

The GCanvas class is a lightweight component that also serves as a container for graphical objects. As such, this class provides the link between graphical objects and the window system. Conceptually, the GCanvas provides a background canvas to which other graphical objects can be added.


 
Constructor Summary
GCanvas()
Creates a new GCanvas that contains no objects.
 
Method Summary
Component add(Component comp)
Adds the component to this canvas without changing its location.
void add(Component comp, double x, double y)
Adds the component to this canvas and sets its location to the point (xy).
void add(Component comp, GPoint pt)
Adds the component to this canvas and sets its location to the specified point.
void add(GObject gobj)
Adds the graphical object to this canvas.
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 getAutoRepaintFlag()
Returns the current setting of the auto-repaint flag as described in setAutoRepaintFlag.
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 GCanvas.
int getHeight()
Returns the height of this canvas in pixels.
boolean getNativeArcFlag()
Returns the current setting of the auto-repaint flag as described in setNativeArcFlag.
int getWidth()
Returns the width of this canvas in pixels.
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 remove(Component comp)
Removes the component from the canvas.
void remove(GObject gobj)
Removes a graphical object from this GCanvas.
void removeAll()
Removes all graphical objects from this GCanvas.
void setAutoRepaintFlag(boolean state)
Changes the setting of the auto-repaint flag.
void setNativeArcFlag(boolean state)
Sets whether the redering code for GArc and GOval should use Java arcs.
 

Constructor Detail

public GCanvas()

Creates a new GCanvas that contains no objects.

 
Usage: GCanvas gc = new GCanvas(); 
 
Method Detail

public Component add(Component comp)

Adds the component to this canvas without changing its location. If the component has no size, its size is set to its preferred size. The return type is Component to match the method in the Container class, but the result is typically ignored.

 
Usage: gc.add(comp); 
Parameter: 
comp  The component to add
 

public final void add(Component comp, double x, double y)

Adds the component to this canvas and sets its location to the point (xy).

 
Usage: gc.add(comp, x, y); 
Parameters: 
comp  The component to add
 The new x-coordinate for the object
 The new y-coordinate for the object
 

public final void add(Component comp, GPoint pt)

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

 
Usage: gc.add(comp, pt); 
Parameters: 
comp  The component to add
pt  A GPoint object giving the coordinates of the point
 

public void add(GObject gobj)

Adds the graphical object to this canvas.

 
Usage: gc.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 getAutoRepaintFlag()

Returns the current setting of the auto-repaint flag as described in setAutoRepaintFlag.

 
Usage: if (gc.getAutoRepaintFlag()) . . . 
Returns: true if auto-repaint mode is enabled, and falseotherwise
 

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 = gc.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.

 
Usage: GObject gobj = gc.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 GCanvas.

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

public int getHeight()

Returns the height of this canvas in pixels. This method is defined in JDK 1.2 components, but not in earlier ones. Defining this method here makes this entry available in all browsers.

 
Usage: int height = gc.getHeight(); 
Returns: The height of this canvas in pixels
 

public boolean getNativeArcFlag()

Returns the current setting of the auto-repaint flag as described in setNativeArcFlag.

 
Usage: if (gc.getNativeArcFlag()) . . . 
Returns: true if native arcs are enabled, and falseotherwise
 

public int getWidth()

Returns the width of this canvas in pixels. This method is defined in JDK 1.2 components, but not in earlier ones. Defining this method here makes this entry available in all browsers.

 
Usage: int width = gc.getWidth(); 
Returns: The width of this canvas
 

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 remove(Component comp)

Removes the component from the canvas.

 
Usage: gc.remove(comp); 
Parameter: 
comp  The component to remove
 

public void remove(GObject gobj)

Removes a graphical object from this GCanvas.

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

public void removeAll()

Removes all graphical objects from this GCanvas.

 
Usage: gc.removeAll(); 
Specified by: removeAll in interface GContainer
Overrides:
removeAll in class Container
 

public void setAutoRepaintFlag(boolean state)

Changes the setting of the auto-repaint flag. By default, any change to a graphical object contained in this canvas automatically triggers a repaint of the canvas as a whole. While this behavior makes it much easier to use the package, it has the disadvantage that repaint requests come much more frequently than necessary. You can disable this feature by calling setAutoRepaintFlag(false), but you must then make explicit calls to repaint() whenever you want to update the display. The advantage of this model is that you can then make many different changes and have them all appear at once with a single repaint call.

 
Usage: gc.setAutoRepaintFlag(state); 
Parameter: 
state  true to enable auto-repaint mode, and falseto disable it
 

public void setNativeArcFlag(boolean state)

Sets whether the redering code for GArc and GOval should use Java arcs. By default, arcs and ovals are rendered using polygons and polylines to ensure that the same set of pixels is covered by the fill and frame. If this value is set to true, the renderers will use the native arc code, which is more efficient but less accurate.

 
Usage: gc.setNativeArcFlag(state); 
Parameter: 
state  true to enable native arcs, false to use polygons