Class Canvas

java.lang.Object
  extended byjava.awt.Component
      extended byjava.awt.Container
          extended byjava.awt.Panel
              extended byCanvas
All Implemented Interfaces:
javax.accessibility.Accessible, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable

public class Canvas
extends java.awt.Panel

The Canvas is the environment that the Turtles live in. It provides the view on the Turtle world. It also keeps a list of all living turtles, so that it can draw them all in the view and tell a turtle if it is alive. The Canvas has a scale factor that determines how much its view of the Turtle world is zoomed in or out. It may also choose to work with a Stepper to regulate how fast the turtles move. There is only ever one Canvas in existence. It can be retrieved with Canvas.getCanvas().

Version:
6/2/05
Author:
Russell Zahniser
See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class java.awt.Panel
java.awt.Panel.AccessibleAWTPanel
 
Nested classes inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
 
Fields inherited from class java.awt.Container
 
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 addTurtle(Turtle turtle)
          Add a turtle to the master list of turtles, so that it will be drawn and will be considered alive.
 void clear()
          Remove all turtles.
 void didStep()
          A Turtle should call this whenever a step has been completed.
 void drawLine(double x1, double y1, double x2, double y2, java.awt.Color color)
          Draw a line on teh canvas, given the start coordinates, end coordinates, and color.
 void drawTurtle(Turtle turtle, double x, double y, double heading)
          Draw the given turtle on this canvas, using its color and pen state.
static Canvas getCanvas()
          Only one canvas ever exists in the program.
 boolean isAlive(Turtle t)
          Check whether this turtle is alive.
 void removeTurtle(Turtle turtle)
          Remove a turtle from the master list of turtles.
 void setScale(double setting)
          The applet should also call this to tell the turtles how to draw.
 void setStepper(Stepper stepper)
          Attach this canvas to a stepper.
 void update(java.awt.Graphics g)
          Draw all the living turtles in the given graphics context.
 
Methods inherited from class java.awt.Panel
addNotify, getAccessibleContext
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getListeners, getMaximumSize, getMinimumSize, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, 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, getBackground, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isOpaque, 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, setName, 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
 

Method Detail

addTurtle

public void addTurtle(Turtle turtle)
Add a turtle to the master list of turtles, so that it will be drawn and will be considered alive.

Parameters:
turtle - Turtle to add.

clear

public void clear()
Remove all turtles. Used when the simulation is reset.


didStep

public void didStep()
A Turtle should call this whenever a step has been completed. If there is a stepper being used, it will have a chance to pause.


drawLine

public void drawLine(double x1,
                     double y1,
                     double x2,
                     double y2,
                     java.awt.Color color)
Draw a line on teh canvas, given the start coordinates, end coordinates, and color.

Parameters:
x1 - x coordinate of start point
y1 - y coordinate of start point
x2 - x coordinate of end point
y2 - y coordinate of end point
color - Color to draw with

drawTurtle

public void drawTurtle(Turtle turtle,
                       double x,
                       double y,
                       double heading)
Draw the given turtle on this canvas, using its color and pen state.

Parameters:
turtle - Turtle to draw
x - x coordinate of that turtle
y - y coordinate of that turtle
heading - Heading of that turtle

getCanvas

public static Canvas getCanvas()
Only one canvas ever exists in the program. This method will return it.

Returns:
The Canvas.

isAlive

public boolean isAlive(Turtle t)
Check whether this turtle is alive.

Parameters:
t - Turtle to look for.
Returns:
Whether this turtle is alive, true or false.

removeTurtle

public void removeTurtle(Turtle turtle)
Remove a turtle from the master list of turtles. It is no longer considered alive. Any further commands to it should result in a DeadTurtleException.

Parameters:
turtle - Turtle to remove.

setScale

public void setScale(double setting)
The applet should also call this to tell the turtles how to draw. It gives the origin in screen coordinates, and the scale factor (how many pixels is one step of the turtle).

Parameters:
setting - A number from 0 to 4 describing the scale. The default is 2.

setStepper

public void setStepper(Stepper stepper)
Attach this canvas to a stepper. This will permit pausing or stopping after each step.

Parameters:
stepper - Stepper to attach.

update

public void update(java.awt.Graphics g)
Draw all the living turtles in the given graphics context.