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

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

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JApplet
JApplet.AccessibleJApplet
 
Nested classes/interfaces inherited from class java.applet.Applet
Applet.AccessibleApplet
 
Nested classes/interfaces inherited from class java.awt.Panel
Panel.AccessibleAWTPanel
 
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 acm.program.Program
CENTER, EAST, NORTH, SOUTH, WEST
 
Fields inherited from class javax.swing.JApplet
accessibleContext, rootPane, rootPaneCheckingEnabled
 
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
 
Constructor Summary
protected GraphicsProgram()
          Creates a new graphics program.
 
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.
protected  GCanvas createGCanvas()
          Creates the GCanvas used by the GraphicsProgram.
protected  void endHook()
          Ensures that the window is repainted at the end of the program.
 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.
protected  boolean isStarted()
          Checks to see whether this program has started, usually by checking to see whether some pane exists.
 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, addImpl, createConsole, createDialogIO, createMenuBar, createParameterTable, createProgramFrame, destroy, exit, getAppletStub, getArgumentArray, getBorder, getCentralRegionSize, getConsole, getDialog, getHeight, getInputModel, getLayout, getMainThread, getMenuBar, getOutputModel, getParameter, getParameterTable, getPreferredSize, getReader, getRegionPanel, getStartupObject, 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, runHook, setAppletStub, setConsole, setDialog, setInputModel, setLayout, setOutputModel, setParameter, setParameterTable, setStartupObject, setTitle, showErrorMessage, start, start, startHook, startRun, validate
 
Methods inherited from class javax.swing.JApplet
createRootPane, getAccessibleContext, getContentPane, getGlassPane, getJMenuBar, getLayeredPane, getRootPane, isRootPaneCheckingEnabled, paramString, setContentPane, setGlassPane, setJMenuBar, setLayeredPane, setRootPane, setRootPaneCheckingEnabled, 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, processContainerEvent, processEvent, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, transferFocusBackward, transferFocusDownCycle, 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, 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, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, 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
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GraphicsProgram

protected GraphicsProgram()
Creates a new graphics program.

Method Detail

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

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

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

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

createGCanvas

protected GCanvas createGCanvas()
Creates the GCanvas used by the GraphicsProgram. Subclasses can override this method to create their own GCanvas types.

Returns:
The GCanvas to be inserted into the program

endHook

protected void endHook()
Ensures that the window is repainted at the end of the program.

Overrides:
endHook in class Program

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

getElementCount

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

Returns:
The number of graphical objects in this GCanvas

getGCanvas

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

Returns:
The GCanvas object used by the 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

isStarted

protected boolean isStarted()
Checks to see whether this program has started, usually by checking to see whether some pane exists. Subclasses can override this method to ensure that their structures are visible before proceeding.

Overrides:
isStarted in class Program

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

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

removeAllComponents

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


repaint

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

Overrides:
repaint in class Program

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

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

waitForClick

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