ACM Library

acm.program
Class GraphicsProgram

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by java.awt.Panel
              extended by java.applet.Applet
                  extended by javax.swing.JApplet
                      extended by acm.program.Program
                          extended by acm.program.GraphicsProgram
All Implemented Interfaces:
IOModel, ActionListener, KeyListener, MouseListener, MouseMotionListener, ImageObserver, MenuContainer, Serializable, Runnable, EventListener, Accessible, RootPaneContainer
Direct Known Subclasses:
TestableGraphicsProgram

public abstract class GraphicsProgram
extends Program

This class is a standard subclass of Program whose principal window is used for drawing graphics.

See Also:
Serialized Form

Field Summary
 
Fields inherited from class acm.program.Program
CENTER, EAST, NORTH, SOUTH, WEST
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Method Summary
 void add(Component comp, double x, double y)
          Adds the component to the canvas and sets its location to the point (xy).
 void add(Component comp, GPoint pt)
          Adds the component to the canvas and sets its location to the specified point.
 void add(GObject gobj)
          Adds a new graphical object to this container.
 void add(GObject gobj, double x, double y)
          Adds the graphical object to the canvas and sets its location to the point (xy).
 void add(GObject gobj, GPoint pt)
          Adds the graphical object to the canvas and sets its location to the specified point.
 void addKeyListeners()
          Adds the program as a KeyListener to the canvas.
 void addKeyListeners(KeyListener listener)
          Adds the specified listener as a KeyListener to the canvas.
 void addMouseListeners()
          Adds the program as both a MouseListener and MouseMotionListener to the canvas.
 void addMouseListeners(EventListener listener)
          Adds the specified listener as a MouseListener and/or MouseMotionListener, as appropriate, to the 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 GCanvas.
 GCanvas getGCanvas()
          Returns the GCanvas object used by this program.
 void init()
          Specifies the code to be executed as startup time before the run method is called.
 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 FRONT_TO_BACK or BACK_TO_FRONT from the GContainer interface.
 void remove(GObject gobj)
          Removes a graphical object from this container.
 void removeAll()
          Removes all graphical objects from this container.
 void removeAllComponents()
          Removes all components from this container.
 void repaint()
          Signals a need to repaint this window.
 void run()
          Specifies the code to be executed as the program runs.
 void setBackground(Color bg)
          Sets the background color of the GCanvas.
static void startGraphicsProgram(GObject gobj, String[] args)
          Creates a GraphicsProgram containing the specified GObject and then starts it.
 void waitForClick()
          Waits for a mouse click in the window before proceeding.
 
Methods inherited from class acm.program.Program
actionPerformed, add, addActionListeners, addActionListeners, addExitHook, destroy, exit, getCentralRegionSize, getConsole, getDialog, getHeight, getInputModel, getLayout, getMainThread, getMenuBar, getOutputModel, getParameter, getPreferredSize, getReader, getRegionPanel, getTitle, getWidth, getWriter, isAppletMode, keyPressed, keyReleased, keyTyped, main, menuAction, mouseClicked, mouseDragged, mouseEntered, mouseExited, mouseMoved, mousePressed, mouseReleased, pause, print, print, print, print, print, print, print, print, println, println, println, println, println, println, println, println, println, readBoolean, readBoolean, readBoolean, readDouble, readDouble, readDouble, readDouble, readInt, readInt, readInt, readInt, readLine, readLine, remove, remove, setConsole, setDialog, setInputModel, setLayout, setOutputModel, setParameter, setTitle, showErrorMessage, start, start, validate
 
Methods inherited from class javax.swing.JApplet
getAccessibleContext, getContentPane, getGlassPane, getJMenuBar, getLayeredPane, getRootPane, setContentPane, setGlassPane, setJMenuBar, setLayeredPane, update
 
Methods inherited from class java.applet.Applet
getAppletContext, getAppletInfo, getAudioClip, getAudioClip, getCodeBase, getDocumentBase, getImage, getImage, getLocale, getParameterInfo, isActive, newAudioClip, play, play, resize, resize, setStub, showStatus, stop
 
Methods inherited from class java.awt.Panel
addNotify
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getListeners, getMaximumSize, getMinimumSize, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, preferredSize, print, printComponents, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, transferFocusBackward, transferFocusDownCycle
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, dispatchEvent, enable, enable, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, 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, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocusInWindow, reshape, 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
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

run

public void run()
Specifies the code to be executed as the program runs. The run method is required for applications that have a thread of control that runs even in the absence of user actions, such as a program that uses console interation or that involves animation. GUI-based programs that operate by setting up an initial configuration and then wait for user events usually do not specify a run method and supply a new definition for init instead.

Specified by:
run in interface Runnable
Overrides:
run in class Program

init

public void init()
Specifies the code to be executed as startup time before the run method is called. Subclasses can override this method to perform any initialization code that would ordinarily be included in an applet init method. In general, subclasses will override init in GUI-based programs where the program simply sets up an initial state and then waits for events from the user. The run method is required for applications in which there needs to be some control thread while the program runs, as in a typical animation.

Overrides:
init in class Program

getGCanvas

public GCanvas getGCanvas()
Returns the GCanvas object used by this program.

Returns:
The GCanvas object used by the program

add

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

Parameters:
gobj - The graphical object to add

add

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

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 the canvas and sets its location to the specified point.

Parameters:
gobj - The graphical object to add
pt - The new coordinates of the point

add

public final void add(Component comp,
                      double x,
                      double y)
Adds the component to the 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 the 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

remove

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

Parameters:
gobj - The graphical object to remove

removeAll

public void removeAll()
Removes all graphical objects from this container. Note that this definition overrides the Container version of removeAll, which is replaced by removeAllComponents.

Overrides:
removeAll in class Program

getElementCount

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

Returns:
The number of graphical objects in this 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.

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.

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.

Parameters:
pt - The coordinates being tested
Returns:
The graphical object at the specified location, or null if no such object exists

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.

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.

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 FRONT_TO_BACK or BACK_TO_FRONT from the GContainer interface.

for (Iterator<GObject> i = 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.

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

addMouseListeners

public void addMouseListeners()
Adds the program as both a MouseListener and MouseMotionListener to the canvas.


addMouseListeners

public void addMouseListeners(EventListener listener)
Adds the specified listener as a MouseListener and/or MouseMotionListener, as appropriate, to the canvas.

Parameters:
listener - A listener object that is either a MouseListener, a MouseMotionListener, or both

addKeyListeners

public void addKeyListeners()
Adds the program as a KeyListener to the canvas.


addKeyListeners

public void addKeyListeners(KeyListener listener)
Adds the specified listener as a KeyListener to the canvas.

Parameters:
listener - A KeyListener object

waitForClick

public void waitForClick()
Waits for a mouse click in the window before proceeding.


repaint

public void repaint()
Signals a need to repaint this window.

Overrides:
repaint in class Program

removeAllComponents

public void removeAllComponents()
Removes all components from this container.


setBackground

public void setBackground(Color bg)
Sets the background color of the GCanvas.

Overrides:
setBackground in class Program
Parameters:
bg - The new background color

startGraphicsProgram

public static void startGraphicsProgram(GObject gobj,
                                        String[] args)
Creates a GraphicsProgram containing the specified GObject and then starts it. This code is called only by the start method in GObject.

Parameters:
gobj - The object to be inserted into the GraphicsProgram
args - The array of arguments

Last updated: Sat, Aug 22, 2009 • 10:26 PM EDT