Class Caterpillar

java.lang.Object
  extended byCaterpillar

public class Caterpillar
extends java.lang.Object

Caterpillar class. Represents the caterpillar and barriers in the grid where the caterpillar is moving.


Field Summary
static int TO_DOWN
          Constant representing caterpillar moving direction downwards.
static int TO_LEFT
          Constant representing caterpillar moving direction from right to left.
static int TO_RIGHT
          Constant representing caterpillar moving direction from left to right.
static int TO_UP
          Constant representing caterpillar moving direction upwards.
 
Constructor Summary
Caterpillar(int width, int height, int positionX, int positionY, java.awt.Color backgroundColor, Grid grid, int numberOfBarriers, java.awt.Graphics g)
          Creates a new instance of Caterpillar
 
Method Summary
 void Delete(java.awt.Graphics g)
          Call this method to remove caterpillar from grid correctly.
 boolean IsCorrectHeadPosition()
          Tests if caterpillar's head is on a barrier or not in a grid (incorrect position) or is somewhere else in a grid (correct position).
 void Move(java.awt.Graphics g)
          Moves caterpillar's body one cell in direction that has been set by force of SetMoveDirection(int).
 void paint(java.awt.Graphics g)
          Repaints whole caterpillar and all barriers.
 void SetMoveDirection(int direction)
          Sets future moving direction of caterpillar.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TO_RIGHT

public static final int TO_RIGHT
Constant representing caterpillar moving direction from left to right.

See Also:
Constant Field Values

TO_LEFT

public static final int TO_LEFT
Constant representing caterpillar moving direction from right to left.

See Also:
Constant Field Values

TO_DOWN

public static final int TO_DOWN
Constant representing caterpillar moving direction downwards.

See Also:
Constant Field Values

TO_UP

public static final int TO_UP
Constant representing caterpillar moving direction upwards.

See Also:
Constant Field Values
Constructor Detail

Caterpillar

public Caterpillar(int width,
                   int height,
                   int positionX,
                   int positionY,
                   java.awt.Color backgroundColor,
                   Grid grid,
                   int numberOfBarriers,
                   java.awt.Graphics g)
Creates a new instance of Caterpillar

Parameters:
width - Width of grid where caterpillar can move. (Number of cells.)
height - Height of grid where caterpillar can move. (Number of cells.)
positionX - x-coordinate in pixels of upper-left corner of a grid.
positionY - y-coordinate in pixels of upper-left corner of a grid.
backgroundColor - Background color of a form.
grid - Refererence to Grid object instance where caterpillar is moving.
numberOfBarriers - Number of barriers that will be created in a grid.
g - Graphics object that is used to paint caterpillar and barriers.
Method Detail

paint

public void paint(java.awt.Graphics g)
Repaints whole caterpillar and all barriers.

Parameters:
g - Graphics object that is used to painting.

Move

public void Move(java.awt.Graphics g)
Moves caterpillar's body one cell in direction that has been set by force of SetMoveDirection(int). If this movement causes that head is on a barrier or not in a grid, head isn't painted. If is not correct head position, Move(Graphics) does nothing.

Parameters:
g - Graphics object that is used to repaint caterpillar's position

IsCorrectHeadPosition

public boolean IsCorrectHeadPosition()
Tests if caterpillar's head is on a barrier or not in a grid (incorrect position) or is somewhere else in a grid (correct position).

Returns:
True if head has correct position, false otherwise.

Delete

public void Delete(java.awt.Graphics g)
Call this method to remove caterpillar from grid correctly. Deletes caterpillar from a form. Background color passed to constructor is used.

Parameters:
g - Graphics object used to painting.

SetMoveDirection

public void SetMoveDirection(int direction)
Sets future moving direction of caterpillar.

Parameters:
direction - Direction. Use constants provided by this class (TO_RIGHT etc.)