edu.sc.ants
Class Environment

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--java.awt.Panel
                    |
                    +--edu.sc.ants.Environment

public class Environment
extends java.awt.Panel
implements java.lang.Runnable

The Environment holds a vector of Thread, each represents a Thing that is running in the world. Environment is responsible for starting/stopping all of them if the user request a stop(). It contains two local classes RunningThing and RunningThingContainer. I am trying to get rid of deadlock situations by moving all synchronization down to the RunningThing class.

See Also:
Serialized Form

Inner Class Summary
 class Environment.RunningThing
          This class embodies one running thing (i.e.
 class Environment.RunningThingContainer
          Contains all the running things (i.e.
 
Inner classes inherited from class java.awt.Component
java.awt.Component.AWTTreeLock
 
Field Summary
(package private)  java.lang.Thread animator
          the thread
(package private)  Environment.RunningThing lastPick
          The last agent that was picked.
(package private)  java.awt.Graphics offgraphics
           
(package private)  java.awt.Image offscreen
           
(package private)  java.awt.Dimension offscreensize
          We use page flipping to avoid flickering
(package private)  Environment.RunningThing pick
          has someone been picked by the user, who?
(package private)  boolean running
          True if this thread should be running
(package private)  boolean suspended
          Should this agent just wait?
(package private)  Environment.RunningThingContainer theThings
          The things in this environment
(package private) static int xSize
          the size of the enviroment, in pixels
(package private) static int ySize
          the size of the enviroment, in pixels
 
Fields inherited from class java.awt.Panel
base, nameCounter, serialVersionUID
 
Fields inherited from class java.awt.Container
component, containerListener, containerSerializedDataVersion, dispatcher, layoutMgr, maxSize, ncomponents, serialVersionUID
 
Fields inherited from class java.awt.Component
actionListenerK, adjustmentListenerK, appContext, assert, background, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, changeSupport, componentListener, componentListenerK, componentOrientation, componentSerializedDataVersion, containerListenerK, cursor, dropTarget, enabled, eventMask, focusListener, focusListenerK, font, foreground, hasFocus, height, incRate, inputMethodListener, inputMethodListenerK, isInc, isPacked, itemListenerK, keyListener, keyListenerK, LEFT_ALIGNMENT, locale, LOCK, minSize, mouseListener, mouseListenerK, mouseMotionListener, mouseMotionListenerK, name, nameExplicitlySet, newEventsOnly, ownedWindowK, parent, peer, peerFont, popups, prefSize, RIGHT_ALIGNMENT, serialVersionUID, textListenerK, TOP_ALIGNMENT, valid, visible, width, windowListenerK, x, y
 
Constructor Summary
(package private) Environment()
           
 
Method Summary
 void createAndAddAgent(java.lang.String type)
          Randomly generates a thing (in this case, an agent) of the given type it to the environment
 void deleteAgent()
          Deletes the thing that was last picked
 void die()
          Is this ever called?
(package private)  void fixCoords(Observation l)
          If the coordinates are out of bounds, set them back within bounds.
 java.util.Vector getInput(int agentID, double range)
           
 Message getMessage(int agentID)
          Is there a message waiting for an agent?
 void init()
          Initialize.
 void resume()
          Tell this thing to continue executing its run() loop
 void run()
          The main loop
 void scramble()
          Scramble the agents around.
 void sendMessage(Message m, double range)
          An agent calls this when it wants to send a message.
 void shake()
          Shake, rattle and roll
 void start()
          This is called when we start for the first time.
(package private)  void startThreads()
          Resume all the agent(Thing) threads
 void stop()
          Tell this Thing to exit its run method
(package private)  void stopThreads()
          Stop each of the threads
 void suspend()
          Tell this thing to wait and to nothing in its run loop
 void takeAction(int agentID, Action a)
          The agents call this when they want to take an action.
 void update(java.awt.Graphics g)
          Refresh the screen, we call each agent to draw itself where it is.
 
Methods inherited from class java.awt.Panel
, addNotify, constructComponentName
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, applyOrientation, countComponents, deliverEvent, dispatchEventImpl, dispatchEventToSelf, doLayout, eventEnabled, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents_NoClientCode, getComponents, getInsets, getLayout, getMaximumSize, getMinimumSize, getMouseEventTarget, getPreferredSize, getWindow, initIDs, insets, invalidate, invalidateTree, isAncestorOf, layout, lightweightPrint, list, list, locate, minimumSize, nextFocus, paint, paintComponents, paramString, postProcessKeyEvent, postsOldMouseEvents, preferredSize, preProcessKeyEvent, print, printComponents, printOneComponent, processContainerEvent, processEvent, proxyEnableEvents, proxyRequestFocus, readObject, remove, remove, removeAll, removeContainerListener, removeNotify, setFocusOwner, setFont, setLayout, transferFocus, validate, validateTree, writeObject
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addPropertyChangeListener, addPropertyChangeListener, areInputMethodsEnabled, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, getBackground, getBounds, getBounds, getColorModel, getComponentOrientation, getCursor, getDropTarget, getFont_NoClientCode, getFont, getFontMetrics, getForeground, getGraphics, getHeight, getInputContext, getInputMethodRequests, getLocale, getLocation, getLocation, getLocationOnScreen, getName, getNativeContainer, getParent_NoClientCode, getParent, getPeer, getSize, getSize, getToolkit, getToolkitImpl, getTreeLock, getWidth, getWindowForObject, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isDisplayable, isDoubleBuffered, isEnabled, isEnabledImpl, isFocusTraversable, 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, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, remove, removeComponentListener, removeFocusListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setForeground, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

animator

java.lang.Thread animator
the thread

theThings

Environment.RunningThingContainer theThings
The things in this environment

xSize

static int xSize
the size of the enviroment, in pixels

ySize

static int ySize
the size of the enviroment, in pixels

pick

Environment.RunningThing pick
has someone been picked by the user, who?

lastPick

Environment.RunningThing lastPick
The last agent that was picked. This is used by deleteAgent()

running

boolean running
True if this thread should be running

suspended

boolean suspended
Should this agent just wait?

offscreen

java.awt.Image offscreen

offscreensize

java.awt.Dimension offscreensize
We use page flipping to avoid flickering

offgraphics

java.awt.Graphics offgraphics
Constructor Detail

Environment

Environment()
Method Detail

stop

public void stop()
Tell this Thing to exit its run method

suspend

public void suspend()
Tell this thing to wait and to nothing in its run loop

resume

public void resume()
Tell this thing to continue executing its run() loop

createAndAddAgent

public void createAndAddAgent(java.lang.String type)
Randomly generates a thing (in this case, an agent) of the given type it to the environment
Parameters:
type - the type of the agent we want to create.

deleteAgent

public void deleteAgent()
Deletes the thing that was last picked

fixCoords

void fixCoords(Observation l)
If the coordinates are out of bounds, set them back within bounds.

takeAction

public void takeAction(int agentID,
                       Action a)
The agents call this when they want to take an action. Update the grid of the agents' true locations using a.
Parameters:
agentID - the agent taking the action
a - the action

getInput

public java.util.Vector getInput(int agentID,
                                 double range)

sendMessage

public void sendMessage(Message m,
                        double range)
An agent calls this when it wants to send a message. We add it to the of all the recipients within range.
Parameters:
m - the message to send
range - the radius, in pixels

getMessage

public Message getMessage(int agentID)
Is there a message waiting for an agent?
Parameters:
agentID - the agent
Returns:
any message (Map) that might be waiting for agentID.null if none

scramble

public void scramble()
Scramble the agents around.

shake

public void shake()
Shake, rattle and roll

stopThreads

void stopThreads()
Stop each of the threads

startThreads

void startThreads()
Resume all the agent(Thing) threads

start

public void start()
This is called when we start for the first time.

init

public void init()
Initialize. Add the event listeners.

die

public void die()
Is this ever called?

run

public void run()
The main loop
Specified by:
run in interface java.lang.Runnable

update

public void update(java.awt.Graphics g)
Refresh the screen, we call each agent to draw itself where it is. To avoid flicker, write the agents to offscreen, then draw the whole thing at once.
Overrides:
update in class java.awt.Container