infpp.oceanlife
Class OceanObject

java.lang.Object
  extended by java.lang.Thread
      extended by infpp.oceanlife.OceanObject
All Implemented Interfaces:
java.io.Serializable, java.lang.Runnable
Direct Known Subclasses:
Bubble, Fish, OilBlob, Plant, Stone

public abstract class OceanObject
extends java.lang.Thread
implements java.io.Serializable

An abstract class implementing the common properties of the different types of ocean object. In particular, an abstract constructor is used.

Author:
tdu, akue
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
protected static int COLLISION_LEFT_BOUND
          Used to signal that the object has collided with the ocean's left bound.
protected static int COLLISION_LOWER_BOUND
          Used to signal that the object has collided with the ocean's lower bound.
protected static int COLLISION_RIGHT_BOUND
          Used to signal that the object has collided with the ocean's right bound.
protected static int COLLISION_UPPER_BOUND
          Used to signal that the object has collided with the ocean's upper bound.
protected  Ocean currentOcean
          The currently-used ocean.
protected  int[] direction
          The object's direction vector.
(package private) static int id
          For unique ocean object id's.
private  boolean isHighlighted
          A flag to indicate whether the object is currently highlighted.
protected  int myID
          ID for each ocean object.
protected static int NO_COLLISION
          Used to indicate that no collision has occurred.
private  javax.swing.ImageIcon objectIcon
          ID for each ocean object.
private  int[] position
          The object's position vector.
private  boolean toDie
          A flag indicating whether the object's thread should die.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
protected OceanObject(int x, int y, javax.swing.ImageIcon objectIcon, Ocean currentOcean)
           
 
Method Summary
 java.awt.Dimension getDimensionsOfIcon()
           
 javax.swing.ImageIcon getObjectIcon()
           
 int getObjectID()
           
 java.lang.String getObjectIDString()
           
 int[] getPosition()
           
 int getPositionX()
           
 int getPositionY()
           
static int getUniqueOceanObjectID()
           
 boolean isHighlighted()
           
 boolean isToDie()
           
protected  int objectCollidesWithOceanBounds()
           
 void run()
           
 void setHighlighted(boolean isHighlighted)
           
 void setObjectIcon(javax.swing.ImageIcon objectIcon)
           
 void setPositionWhenAdding(int[] position)
           
 void setPositionWhenMoving(int[] newPosition)
          This method is responsible for setting the position of objects already in the ocean.
 void setToDie(boolean toDie)
           
 java.lang.String toString()
           
protected  void updateDirection()
           
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

id

static int id
For unique ocean object id's.


myID

protected int myID
ID for each ocean object.


objectIcon

private javax.swing.ImageIcon objectIcon
ID for each ocean object.


isHighlighted

private boolean isHighlighted
A flag to indicate whether the object is currently highlighted.


currentOcean

protected Ocean currentOcean
The currently-used ocean.


toDie

private boolean toDie
A flag indicating whether the object's thread should die.


NO_COLLISION

protected static final int NO_COLLISION
Used to indicate that no collision has occurred.

See Also:
Constant Field Values

COLLISION_UPPER_BOUND

protected static final int COLLISION_UPPER_BOUND
Used to signal that the object has collided with the ocean's upper bound.

See Also:
Constant Field Values

COLLISION_LOWER_BOUND

protected static final int COLLISION_LOWER_BOUND
Used to signal that the object has collided with the ocean's lower bound.

See Also:
Constant Field Values

COLLISION_LEFT_BOUND

protected static final int COLLISION_LEFT_BOUND
Used to signal that the object has collided with the ocean's left bound.

See Also:
Constant Field Values

COLLISION_RIGHT_BOUND

protected static final int COLLISION_RIGHT_BOUND
Used to signal that the object has collided with the ocean's right bound.

See Also:
Constant Field Values

position

private int[] position
The object's position vector.


direction

protected int[] direction
The object's direction vector.

Constructor Detail

OceanObject

protected OceanObject(int x,
                      int y,
                      javax.swing.ImageIcon objectIcon,
                      Ocean currentOcean)
               throws java.lang.Exception
Parameters:
x - The x-coordinate of the ocean object.
y - The y-coordinate of the ocean object.
objectIcon - Icons for the ocean objects.
currentOcean - The current used ocean.
Throws:
java.lang.Exception - Throws exception for the position.
Method Detail

getObjectIcon

public javax.swing.ImageIcon getObjectIcon()
Returns:
The object icon.

setObjectIcon

public void setObjectIcon(javax.swing.ImageIcon objectIcon)
Parameters:
objectIcon - the objectIcon to set

updateDirection

protected void updateDirection()

run

public void run()
Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

getUniqueOceanObjectID

public static int getUniqueOceanObjectID()
Returns:
An unused ID to use when an object's constructor requests one.

getObjectID

public int getObjectID()
Returns:
Returns the id of the ocean object.

getObjectIDString

public java.lang.String getObjectIDString()
Returns:
Returns the id of the ocean object as type string.

isHighlighted

public boolean isHighlighted()
Returns:
the highlight status of the ocean object as boolean.

setHighlighted

public void setHighlighted(boolean isHighlighted)
Parameters:
isHighlighted - whether or not the object should be highlighted.

getPosition

public int[] getPosition()
Returns:
Returns the position vector as an integer array.

getPositionX

public int getPositionX()
Returns:
the x coordinate of the object's position

getPositionY

public int getPositionY()
Returns:
the y coordinate of the object's position

setPositionWhenAdding

public void setPositionWhenAdding(int[] position)
                           throws java.lang.Exception
Parameters:
position - the position to set
Throws:
java.lang.Exception

objectCollidesWithOceanBounds

protected int objectCollidesWithOceanBounds()
Returns:
an integer specifying which, if any, bounds the object has collided with.

setPositionWhenMoving

public void setPositionWhenMoving(int[] newPosition)
                           throws java.lang.Exception
This method is responsible for setting the position of objects already in the ocean.

Parameters:
newPosition - the object's new position
Throws:
java.lang.Exception - if the object is about to swim out of the ocean

isToDie

public boolean isToDie()
Returns:
a boolean indicating whether the object's thread should die.

setToDie

public void setToDie(boolean toDie)
Parameters:
toDie - a boolean indicating whether the object's thread should die.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Thread
Returns:
a string representation of the object.

getDimensionsOfIcon

public java.awt.Dimension getDimensionsOfIcon()
Returns:
a Dimension containing the width and height of the object's icon.