alpiv.trucks
Class RoadMap

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Canvas
              |
              +--alpiv.trucks.RoadMap
All Implemented Interfaces:
javax.accessibility.Accessible, java.awt.image.ImageObserver, java.awt.MenuContainer, alpiv.trucks.RoadObserver, java.io.Serializable

public class RoadMap
extends java.awt.Canvas
implements alpiv.trucks.RoadObserver

Objects of this class represent a map of connected roads. This class contains factory methods to create certain scenario maps, and a main method where test runs can be performed.

See Also:
Serialized Form

Field Summary
 
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
RoadMap(java.lang.String scenario)
          Main constructor.
 
Method Summary
 Road getStart(int index)
          Get the indicated start Road of the map.
 int getStarts()
          Get the number of start positions in the map.
protected static void link(Road source, int dir, Road dest)
          A helper method to bidirectionally link two road sections.
static int offsetX(int dir)
          A helper method to determine the X offset of the given direction.
static int offsetY(int dir)
          A helper method to determine the Y offset of the given direction.
 void paint(java.awt.Graphics g)
          Draw the map in the main display canvas.
 void roadChanged()
          Callback method for observing traffic changes.
 
Methods inherited from class java.awt.Canvas
addNotify, createBufferStrategy, createBufferStrategy, getAccessibleContext, getBufferStrategy, update
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

RoadMap

public RoadMap(java.lang.String scenario)
Main constructor. This creates the given scenario map.

Parameters:
scenario - the name of the scenario to create
Method Detail

getStarts

public int getStarts()
Get the number of start positions in the map.

Returns:
the number of start positions.

getStart

public Road getStart(int index)
Get the indicated start Road of the map.

Returns:
a Road object of thype START, or null for no such number

paint

public void paint(java.awt.Graphics g)
Draw the map in the main display canvas. Lines indicate exits, icons represent the types of the road sections, or the trucks moving on them in a specific direction.

Overrides:
paint in class java.awt.Canvas
Parameters:
g - the graphcis context to draw with

roadChanged

public void roadChanged()
Callback method for observing traffic changes. This is called by road objects when trucks are placed on them or removed again. It schedules a repainting of the display.

Specified by:
roadChanged in interface alpiv.trucks.RoadObserver

offsetX

public static int offsetX(int dir)
A helper method to determine the X offset of the given direction. This assumes a coordinate system with origin in the upper left corner.

Parameters:
dir - the requested direction

offsetY

public static int offsetY(int dir)
A helper method to determine the Y offset of the given direction. This assumes a coordinate system with origin in the upper left corner.

Parameters:
dir - the requested direction

link

protected static void link(Road source,
                           int dir,
                           Road dest)
A helper method to bidirectionally link two road sections. This is used in road map construction. An exit is added to the starting road in the given direction, leading to the target road. An exit in the opposite direction is added to the target road, leading back to the start road. Also, the location coordinates of the target road are set relative to the coordinates of the start road, shifted by the appropriate X and Y offset of the direction.

Parameters:
source - the starting road
dir - the direction of the exit from the source road
dest - the target road reachable by the exit