|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JPanel
appletbots.World
public class World
This class represents a world in which multiple agents can exist and interact with each other or with objects in the world. The positions and velocities of objects and agents are kept internally. The positions cannot be accessed by the agents or the objects themselves. This keeps all agent algorithms completely vector-based and, therefore, scalable.
Nested Class Summary |
---|
Nested classes/interfaces inherited from class javax.swing.JPanel |
---|
javax.swing.JPanel.AccessibleJPanel |
Nested classes/interfaces inherited from class javax.swing.JComponent |
---|
javax.swing.JComponent.AccessibleJComponent |
Nested classes/interfaces inherited from class java.awt.Container |
---|
java.awt.Container.AccessibleAWTContainer |
Nested classes/interfaces inherited from class java.awt.Component |
---|
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy |
Field Summary | |
---|---|
protected int |
delay
The number of milliseconds to sleep in between time cycles |
protected java.util.List |
listeners
Registered listeners |
protected static int |
MAX_ADD_OBJECT_ATTEMPTS
The maximum number of attempts (1000) that should be made trying to add an object randomly to the world. |
protected java.util.Map |
objectsTable
A hashtable mapping each object to its location and velocity datum |
protected WorldObject |
selectedObject
The object that is currently selected |
protected java.awt.Color |
selectedObjectColor
The color to paint the object that is selected |
protected java.awt.Color |
selectedSight
The color in which to paint the "sight circle" of the selected object |
protected WorldThread |
thread
The thread driving the world |
protected long |
time
The number of time cycles that have passed |
Fields inherited from class javax.swing.JComponent |
---|
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW |
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 | |
---|---|
World(int width,
int height)
Constructs a new world with the given dimensions |
Method Summary | |
---|---|
void |
addListener(WorldListener listener)
Registers a listener for this world |
void |
addObject(WorldObject object)
Adds an object to a random position in the world. |
void |
addObject(WorldObject object,
Point location,
Vector velocity)
Adds an object to the world at the given position with the given initial velocity |
void |
addObject(WorldObject object,
Vector velocity)
Adds an object to a random position in the world with the given initial velocity |
void |
dropItem(CarrierAgent agent,
CarriableObject item)
Drops an item from a CarrierAgent |
WorldObject |
getClosestObjectOfType(Agent agent,
java.lang.Class type)
Returns the closest object of the given type to the given agent that is seen by the agent |
protected WorldObjectData |
getData(WorldObject object)
Returns the location and velocity data for the given object |
int |
getDelay()
Returns the number of milliseconds to wait between time cycles |
protected double |
getDistanceBetweenObjects(WorldObject a,
WorldObject b)
Returns the distance between the two objects |
java.util.List |
getNeighbors(Agent agent)
Returns all the other agents that can be seen by the given agent, normally called by the given agent himself. |
protected WorldObject |
getObjectAt(int x,
int y)
Returns the object at the given x and y coordinates. |
protected Point |
getRandomPoint()
Returns a random point in the world |
protected Point |
getRandomPoint(int distance)
Returns a random point in the world within a given distance from the boundaries |
java.util.List |
getSeenObjects(Agent agent)
Returns all the objects that can be seen by the given agent, normally called by the given agent himself. |
long |
getTime()
Returns the number of elapsed time cycles |
Vector |
getVectorToObject(WorldObject a,
WorldObject b)
Returns a vector from the first object to the second object. |
Vector |
getVelocity(WorldObject object)
Returns the velocity for the given object |
int |
getWorldHeight()
Returns the height (in pixels) of the world |
int |
getWorldWidth()
Returns the width (in pixels) of the world |
void |
importObjects(World world)
Imports all the objects from another world into this one |
void |
incrementTime()
Called by the WorldThread. |
protected boolean |
inWorld(Point location,
int size)
Returns whether or not an object with the given size at the given location would be complete inside the boundaries of the world |
boolean |
isRunning()
Returns whether or not the world thread is running |
java.util.Comparator |
objectDistanceComparator(WorldObject object)
Returns a comparator to sort other objects by their distance to the given object |
void |
paint(java.awt.Graphics g)
Paints the world (invoked by Swing) |
protected void |
paintObject(WorldObject object,
java.awt.Graphics g)
Paints an individual object in the world |
protected void |
paintObjects(java.awt.Graphics g)
Paints the objects in the world |
void |
removeObject(WorldObject object)
Removes an object from the world |
void |
selectObject(WorldObject object)
Selects the given object |
void |
selectObjectAt(int x,
int y)
Selects the object at the given location |
void |
setDelay(int delay)
Sets the number of milliseconds to wait between time cycles |
void |
setSelectedObjectColor(java.awt.Color selectedObjectColor)
Sets the color to paint the selected object |
void |
setSelectedSight(java.awt.Color selectedSight)
Sets the color with which to paint the selected object's "sight circle" |
void |
startThread()
Starts the world thread |
void |
stopThread()
Stops the world thread |
Methods inherited from class javax.swing.JPanel |
---|
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI |
Methods inherited from class javax.swing.JComponent |
---|
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update |
Methods inherited from class java.awt.Container |
---|
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, 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, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, 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, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected static final int MAX_ADD_OBJECT_ATTEMPTS
protected java.util.Map objectsTable
protected int delay
protected WorldThread thread
protected long time
protected java.util.List listeners
protected WorldObject selectedObject
protected java.awt.Color selectedObjectColor
protected java.awt.Color selectedSight
Constructor Detail |
---|
public World(int width, int height)
width
- The width of the world in pixelsheight
- The height of the world in pixelsMethod Detail |
---|
public void paint(java.awt.Graphics g)
paint
in class javax.swing.JComponent
g
- The graphics object on which to paint the worldprotected void paintObjects(java.awt.Graphics g)
g
- The graphics object on which to paint the objectsprotected void paintObject(WorldObject object, java.awt.Graphics g)
object
- The object to paintg
- The graphics object on which to paint the objectpublic void addObject(WorldObject object) throws CollisionException
object
- The object to add
CollisionException
- Thrown if the object cannot be added randomly
to the world in MAX_ADD_OBJECT_ATTEMPTSpublic void addObject(WorldObject object, Vector velocity) throws CollisionException
object
- The object to addvelocity
- The initial velocity
CollisionException
- Thrown if the object cannot be added randomly
to the world in MAX_ADD_OBJECT_ATTEMPTSpublic void addObject(WorldObject object, Point location, Vector velocity) throws CollisionException, OutOfThisWorldException
object
- The object to addlocation
- The location to add the objectvelocity
- The initial velocity
CollisionException
- Thrown if the location given is at least
partially occupied by another object
OutOfThisWorldException
- Thrown if the location given will place
at least part of the object outside the
boundaries of the worldprotected Point getRandomPoint()
protected Point getRandomPoint(int distance)
distance
- The minimum distance from the boundaries that the point
can be
public void removeObject(WorldObject object)
object
- The object to removeprotected WorldObjectData getData(WorldObject object)
object
- The object to get the data for
public Vector getVelocity(WorldObject object)
object
- The object to get the velocity of
public void incrementTime()
public int getDelay()
public void setDelay(int delay)
delay
- The number of milliseconds to wait between time cyclespublic void startThread()
public void stopThread()
public boolean isRunning()
public java.util.List getSeenObjects(Agent agent)
agent
- The agent for which to get the seen objects
public java.util.List getNeighbors(Agent agent)
agent
- The agent for which to return the seen neighbors
public Vector getVectorToObject(WorldObject a, WorldObject b)
a
- The first objectb
- The second object
protected double getDistanceBetweenObjects(WorldObject a, WorldObject b)
a
- An objectb
- Another object
public int getWorldWidth()
public int getWorldHeight()
public long getTime()
public void importObjects(World world)
world
- The world to import the objects fromprotected boolean inWorld(Point location, int size)
location
- The location of the hypothetical objectsize
- The size of the hypothetical object
protected WorldObject getObjectAt(int x, int y)
x
- The x coordinatey
- The y coordinate
public void selectObjectAt(int x, int y)
x
- The x coordinatey
- The y coordinatepublic void selectObject(WorldObject object)
object
- The object to selectpublic void setSelectedObjectColor(java.awt.Color selectedObjectColor)
selectedObjectColor
- The color to paint the selected objectpublic void setSelectedSight(java.awt.Color selectedSight)
selectedSight
- The color with which to paint the selected object's
"sight circle"public void dropItem(CarrierAgent agent, CarriableObject item) throws CollisionException
agent
- The agent dropping the itemitem
- The object beind dropped
CollisionException
- Thrown if the item cannot be placed within the "pickup distance"public void addListener(WorldListener listener)
public WorldObject getClosestObjectOfType(Agent agent, java.lang.Class type)
agent
- The agent to get an object neartype
- The type of objects to search for
public java.util.Comparator objectDistanceComparator(WorldObject object)
object
- The object to compare distances to
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |