gecco.server.automaton
Class AutomatonMap

java.lang.Object
  |
  +--gecco.server.automaton.AutomatonMap

public class AutomatonMap
extends Object

This class manages the map of automatons. It can create a map given an image.

Author:
Henrik Bäärnhielm, Karl Hasselström, Henrik Hägerström

Field Summary
private  Class automatonClass
          Description of the Field
 byte[] initialMapRGB
          The size of this array is 3*xsize*ysize.
 Automaton[][] theMap
          The map
private  int xBound
          Description of the Field
private  int yBound
          Description of the Field
 
Constructor Summary
AutomatonMap(Class _automatonClass, int x, int y)
          Constructor for the AutomatonMap object
AutomatonMap(Class _automatonClass, int x, int y, byte[] map, HashMap colorToState)
          Constructor for the AutomatonMap object
AutomatonMap(Class _automatonClass, int x, int y, Image map, HashMap colorToState)
          Constructor for the AutomatonMap object
AutomatonMap(Class _automatonClass, int x, int y, Reader map)
          Constructor for the AutomatonMap object
AutomatonMap(Class _automatonClass, int x, int y, String imageFileName, HashMap colorToState)
          Constructor for the AutomatonMap object
AutomatonMap(Class _automatonClass, int x, int y, URL map, HashMap colorToState)
          Constructor for the AutomatonMap object
 
Method Summary
 boolean areValidCoordinates(int x, int y)
          Returns true if the given coordinates exist on the map.
private  int calculateDistance(Color c1, Color c2)
          Calculates the distance between two colors.
 Automaton getAutomaton(int x, int y)
          Gets an automaton at a given location.
 byte[] getInitialMap()
          Gets the InitialMap attribute of the AutomatonMap object
protected  Color getNearestColor(HashMap colorToState, Color color)
          Gets the nearest color that exists on the map, given a certain color.
 int[][] getNeighbourStates(int centerX, int centerY)
          Gets the states of the automatons at a 3x3 region around a given location.
private  Automaton getNewAutomaton(int initialState)
          Allocates a new automaton.
 int getXBound()
          Gets the XBound attribute of the AutomatonMap object
 int getYBound()
          Gets the YBound attribute of the AutomatonMap object
protected  void initMap(Image map, HashMap colorToState)
          Allocates all automatons on the map.
protected  void initMap(String map)
          Allocates all automatons on the map.
 void setImage(byte[] map, HashMap colorToState)
          Sets the Image attribute of the AutomatonMap object
 void setImage(Image map, HashMap colorToState)
          Sets the Image attribute of the AutomatonMap object
 void setImage(Reader stream)
          Sets the Image attribute of the AutomatonMap object, by reading characters from a stream.
 void setImage(String imageFileName, HashMap colorToState)
          Sets the Image attribute of the AutomatonMap object
 void setImage(URL mapURL, HashMap colorToState)
          Sets the Image attribute of the AutomatonMap object
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

theMap

public Automaton[][] theMap
The map

initialMapRGB

public byte[] initialMapRGB
The size of this array is 3*xsize*ysize. It holds the red, green and blue values for the pixels in the original map in sequential order. (North-west -> south-east, row by row)

automatonClass

private Class automatonClass
Description of the Field

xBound

private int xBound
Description of the Field

yBound

private int yBound
Description of the Field
Constructor Detail

AutomatonMap

public AutomatonMap(Class _automatonClass,
                    int x,
                    int y)
Constructor for the AutomatonMap object
Parameters:
x - The width of the map.
y - The height of the map.
_automatonClass - The implemented automaton class.

AutomatonMap

public AutomatonMap(Class _automatonClass,
                    int x,
                    int y,
                    URL map,
                    HashMap colorToState)
Constructor for the AutomatonMap object
Parameters:
x - The width of the map.
y - The height of the map.
map - The map image.
colorToState - The color conversion function.
_automatonClass - The implemented automaton class.

AutomatonMap

public AutomatonMap(Class _automatonClass,
                    int x,
                    int y,
                    Image map,
                    HashMap colorToState)
Constructor for the AutomatonMap object
Parameters:
x - The width of the map.
y - The height of the map.
map - The map image.
colorToState - The color conversion function.
_automatonClass - The implemented automaton class.

AutomatonMap

public AutomatonMap(Class _automatonClass,
                    int x,
                    int y,
                    Reader map)
Constructor for the AutomatonMap object
Parameters:
x - The width of the map.
y - The height of the map.
map - The map image.
_automatonClass - The implemented automaton class.

AutomatonMap

public AutomatonMap(Class _automatonClass,
                    int x,
                    int y,
                    byte[] map,
                    HashMap colorToState)
Constructor for the AutomatonMap object
Parameters:
x - The width of the map.
y - The height of the map.
map - The map image.
colorToState - The color conversion function.
_automatonClass - The implemented automaton class.

AutomatonMap

public AutomatonMap(Class _automatonClass,
                    int x,
                    int y,
                    String imageFileName,
                    HashMap colorToState)
Constructor for the AutomatonMap object
Parameters:
x - The width of the map.
y - The height of the map.
imageFileName - The map image.
colorToState - The color conversion function.
_automatonClass - The implemented automaton class.
Method Detail

setImage

public void setImage(URL mapURL,
                     HashMap colorToState)
Sets the Image attribute of the AutomatonMap object
Parameters:
mapURL - The new Image value
colorToState - The color conversion function.

setImage

public void setImage(Image map,
                     HashMap colorToState)
Sets the Image attribute of the AutomatonMap object
Parameters:
map - The new Image value
colorToState - The color conversion function.

setImage

public void setImage(byte[] map,
                     HashMap colorToState)
Sets the Image attribute of the AutomatonMap object
Parameters:
map - The new Image value
colorToState - The color conversion function.

setImage

public void setImage(String imageFileName,
                     HashMap colorToState)
Sets the Image attribute of the AutomatonMap object
Parameters:
imageFileName - The new Image value
colorToState - The color conversion function.

setImage

public void setImage(Reader stream)
Sets the Image attribute of the AutomatonMap object, by reading characters from a stream. This is an alternative method for creating a map, if a real image is not available.
Parameters:
stream - The stream to get characters from

getInitialMap

public byte[] getInitialMap()
Gets the InitialMap attribute of the AutomatonMap object
Returns:
The InitialMap

getAutomaton

public Automaton getAutomaton(int x,
                              int y)
                       throws IllegalArgumentException
Gets an automaton at a given location.
Parameters:
x - The x coordinate.
y - The y coordinate.
Returns:
The Automaton
Throws:
IllegalArgumentException - if the location does not exist.

getNeighbourStates

public int[][] getNeighbourStates(int centerX,
                                  int centerY)
Gets the states of the automatons at a 3x3 region around a given location.
Parameters:
centerX - The x coordinate of the center automaton.
centerY - The x coordinate of the center automaton.
Returns:
The NeighbourStates

getXBound

public int getXBound()
Gets the XBound attribute of the AutomatonMap object
Returns:
The XBound value

getYBound

public int getYBound()
Gets the YBound attribute of the AutomatonMap object
Returns:
The YBound value

areValidCoordinates

public boolean areValidCoordinates(int x,
                                   int y)
Returns true if the given coordinates exist on the map.
Parameters:
x - The x coordinate to check.
y - The x coordinate to check.
Returns:
true if the location exist on the map, false otherwise

getNearestColor

protected Color getNearestColor(HashMap colorToState,
                                Color color)
Gets the nearest color that exists on the map, given a certain color.
Parameters:
colorToState - The color conversion function.
color - The color to check.
Returns:
The nearest color on the map.

initMap

protected void initMap(Image map,
                       HashMap colorToState)
Allocates all automatons on the map.
Parameters:
map - The map image.
colorToState - The color conversion function.

initMap

protected void initMap(String map)
Allocates all automatons on the map. This is the alternative way if the map has been read from a stream.
Parameters:
map - The map.

getNewAutomaton

private Automaton getNewAutomaton(int initialState)
Allocates a new automaton.
Parameters:
initialState - The initial state of the automaton.
Returns:
The newly allocated automaton.

calculateDistance

private int calculateDistance(Color c1,
                              Color c2)
Calculates the distance between two colors. The method calculates the "Euclidian distance", by returning the sum of the differences in each component of the color.
Parameters:
c1 - The first color.
c2 - The second color.
Returns:
The distance between the colors.