acm.graphics
Class GCanvas

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by acm.graphics.GCanvas
All Implemented Interfaces:
GContainer, ImageObserver, MenuContainer, Serializable

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.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface acm.graphics.GContainer
BACK_TO_FRONT, FRONT_TO_BACK
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
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.
protected  void conditionalRepaint()
          Repaints the canvas if auto-repaint is in effect.
protected  void dispatchMouseEvent(MouseEvent e)
          Dispatches this mouse event to the uppermost graphical object for which the active point is within the object bounds.
 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 native-arc flag as described in setNativeArcFlag.
 int getWidth()
          Returns the width of this canvas in pixels.
protected  void initOffscreenImage()
          Initializes the offscreen memory image used for double buffering.
 boolean isOpaque()
          Returns a boolean value indicating whether this canvas is opaque.
 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 paint(Graphics g)
          Paints the canvas.
 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.
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.
 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.
 void setOpaque(boolean flag)
          Sets a flag indicating whether this canvas is opaque, which means that it obscures anything behind it.
 void update(Graphics g)
          Updates the canvas.
protected  void updateEnabledList()
          Reconstructs the enabledList list in the correct order.
 
Methods inherited from class java.awt.Container
add, add, add, add, addContainerListener, addImpl, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getListeners, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getBackground, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GCanvas

public GCanvas()
Creates a new GCanvas that contains no objects.

Method Detail

add

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.

Overrides:
add in class Container
Parameters:
comp - The component to add

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

Parameters:
comp - The component to add
x - The new x-coordinate for the object
y - The new y-coordinate for the object

add

public final void add(Component comp,
                      GPoint pt)
Adds the component to this canvas and sets its location to the specified point.

Parameters:
comp - The component to add
pt - A GPoint object giving the coordinates of the point

add

public void add(GObject gobj)
Adds the graphical object to this canvas.

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

conditionalRepaint

protected void conditionalRepaint()
Repaints the canvas if auto-repaint is in effect. This method is called only by the repaint method in GObject and is not accessible outside the package.


dispatchMouseEvent

protected void dispatchMouseEvent(MouseEvent e)
Dispatches this mouse event to the uppermost graphical object for which the active point is within the object bounds.

Parameters:
e - The event that triggered this response

getAutoRepaintFlag

public boolean getAutoRepaintFlag()
Returns the current setting of the auto-repaint flag as described in setAutoRepaintFlag.

Returns:
true if auto-repaint mode is enabled, and false otherwise

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.

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

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

getHeight

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.

Overrides:
getHeight in class Component
Returns:
The height of this canvas in pixels

getNativeArcFlag

public boolean getNativeArcFlag()
Returns the current setting of the native-arc flag as described in setNativeArcFlag.

Returns:
true if native arcs are enabled, and false otherwise

getWidth

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.

Overrides:
getWidth in class Component
Returns:
The width of this canvas

initOffscreenImage

protected void initOffscreenImage()
Initializes the offscreen memory image used for double buffering.


isOpaque

public boolean isOpaque()
Returns a boolean value indicating whether this canvas is opaque.

Overrides:
isOpaque in class Component
Returns:
true if this canvas is opaque, and false if it is transparent

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

paint

public void paint(Graphics g)
Paints the canvas. This method is not ordinarily called by clients.

Overrides:
paint in class Container
Parameters:
g - The graphics context into which the canvas is painted

remove

public void remove(Component comp)
Removes the component from the canvas.

Overrides:
remove in class Container
Parameters:
comp - The component to remove

remove

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

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

removeAll

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

Specified by:
removeAll in interface GContainer
Overrides:
removeAll in class Container

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.


setAutoRepaintFlag

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.

Parameters:
state - true to enable auto-repaint mode, and false to disable it

setNativeArcFlag

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.

Parameters:
state - true to enable native arcs, false to use polygons

setOpaque

public void setOpaque(boolean flag)
Sets a flag indicating whether this canvas is opaque, which means that it obscures anything behind it. Setting this flag to false makes the GCanvas transparent, so that any other lightweight components behind it show through.

Parameters:
flag - true to make this canvas opaque, and false to make it transparent

update

public void update(Graphics g)
Updates the canvas. This method is overridden here to support transparency in the Swing style.

Overrides:
update in class Container
Parameters:
g - The graphics context into which the canvas is painted

updateEnabledList

protected void updateEnabledList()
Reconstructs the enabledList list in the correct order.