Class Spider
Class Spider
java.lang.Object
|
+----Spider
- class Spider
- extends Object
This class defines the internal states of Spider objects.
The class name refers to Feldman's "Spider graphics" programs
developed in the textbook "Ada 95". Spiders are instantiated in the
graphic environment realized by objects of class Room: they move around
in a room, leaving a colored "silk" trail and small icons of themselves.
The color of the silk trail can be changed under program control.
The spider icon is shaped like a letter M, with a small circle for
the spider's belly at the point where the slanted inner lines meet.
The four lines that represent the (front) legs of the spider are always
in the foreground color (black), the belly color can be set with some
of the Spider constructor methods but cannot be changed thereafter.
Spiders have no useful life by themselves (outside of a Room).
Spiders in this implementation
are very simple creatures: they retain information but do not do
anything on their own. All graphics operations for spiders are defined
as methods of Room; these Room methods take a Spider object as their
first argument. Spider methods are limited to mutator or accessor
roles:
mutator methods allow write access to the private Spider states,
whereas accessor methods allow read access of these variables.
A more comprehensive implementation of the Spider class would give
Spider objects feedback from Room, allowing the development of spider
behavior based on that feedback.
- See Also:
- AppFrame, Room
-
Spider()
- Creates a red-bellied
spider initially drawing green "silk", targeted for the origin (0, 0)
of the drawing surface, facing NORTH (constant defined in Room, meaning
vertically up on the screen).
-
Spider(int, int, int, Color, Color)
- Places a new spider at the X, Y coordinates specified by the first two
parameters, headed in the direction specified by the third parameter,
drawing "silk" in the color specified by the fourth parameter, and
with its belly color specified by the final parameter.
-
Spider(Point, int, Color, Color)
- Places a new spider at the point specified by the first
parameter, headed in the direction specified by the second parameter,
drawing "silk" in the color specified by the third parameter, and
with its belly color specified by the final parameter.
-
getBelly()
- Returns the color of this spider's belly.
-
getHeading()
- Reports the current direction (heading) of this spider.
-
getSilk()
- Returns the current color of this spider's "silk".
-
getXY()
- Reports the current coordinates of this spider within the Room.
-
setHeading(int)
- Turn toward the specified direction.
-
setSilk(Color)
- Changes the color of this spider's "silk".
-
setXY(int, int)
- Records this spider as being at the "newX, newY" coordinates of the
Room.
-
setXY(Point)
- Records this spider as being at the "newSpot" Point of the
Room.
-
turnLeft()
- Specifies that this spider is to turn left (90 degrees) relative to
its current direction (heading).
-
turnRight()
- Specifies that this spider is to turn right (90 degrees) relative to
its current direction (heading).
Spider
public Spider()
- Creates a red-bellied
spider initially drawing green "silk", targeted for the origin (0, 0)
of the drawing surface, facing NORTH (constant defined in Room, meaning
vertically up on the screen).
Spider
public Spider(int startX,
int startY,
int heading,
Color ink,
Color bellyColor)
- Places a new spider at the X, Y coordinates specified by the first two
parameters, headed in the direction specified by the third parameter,
drawing "silk" in the color specified by the fourth parameter, and
with its belly color specified by the final parameter.
- Parameters:
- startX - the X ordinate (in pixels) of the spider's position in
the Room (no bounds checking is performed by this constructor).
- startY - the Y ordinate (in pixels) of the spider's position in
the Room (no bounds checking is performed by this constructor).
- heading - one of: Room.NORTH, Room.EAST, Room.SOUTH, Room.WEST.
Out of range values are silently converted by taking the absolute
modulo_4 of the input.
A spider headed Room.NORTH is oriented upright (like the letter M) and
would move toward the top of the screen. A spider headed Room.SOUTH
is drawn like the letter W, and would move toward the bottom of the
screen.
A spider headed Room.WEST looks like a letter M turned on its left side,
and would move toward the left.
A spider headed EAST looks like a letter M turned on its right side,
and would move toward the right.
- ink - the Color of the spider's "silk"; may change thereafter.
- bellyColor - the Color of the spider's belly; cannot change thereafter.
Spider
public Spider(Point spot,
int heading,
Color ink,
Color bellyColor)
- Places a new spider at the point specified by the first
parameter, headed in the direction specified by the second parameter,
drawing "silk" in the color specified by the third parameter, and
with its belly color specified by the final parameter.
- Parameters:
- spot - the starting Point of the spider in the Room
(no bounds checking is performed by this constructor).
- heading - one of: Room.NORTH, Room.EAST, Room.SOUTH, Room.WEST.
Out of range values are silently converted by taking the absolute
modulo_4 of the input.
A spider headed Room.NORTH is oriented upright (like the letter M) and
would move toward the top of the screen. A spider headed Room.SOUTH
is drawn like the letter W, and would move toward the bottom of the
screen.
A spider headed Room.WEST looks like a letter M turned on its left side,
and would move toward the left.
A spider headed EAST looks like a letter M turned on its right side,
and would move toward the right.
- ink - the Color of the spider's "silk"; may change thereafter.
- bellyColor - the Color of the spider's belly; cannot change thereafter.
setHeading
public void setHeading(int heading)
- Turn toward the specified direction. (As a matter of style, Java
mutator method names that select from multiple values, begin
with "set").
- Parameters:
- heading - one of: Room.NORTH, Room.EAST, Room.SOUTH, Room.WEST.
Out of range values are silently converted by taking the absolute
modulo_4 of the input.
A spider headed Room.NORTH is oriented upright (like the letter M) and
would move toward the top of the screen. A spider headed Room.SOUTH
is drawn like the letter W, and would move toward the bottom of the
screen.
A spider headed Room.WEST looks like a letter M turned on its left side,
and would move toward the left.
A spider headed EAST looks like a letter M turned on its right side,
and would move toward the right.
getHeading
public int getHeading()
- Reports the current direction (heading) of this spider.
- Returns:
- one of: Room.NORTH, Room.EAST, Room.SOUTH, Room.WEST, as the
direction in which this spider would move next.
turnRight
public int turnRight()
- Specifies that this spider is to turn right (90 degrees) relative to
its current direction (heading).
- Returns:
- one of: Room.NORTH, Room.EAST, Room.SOUTH, Room.WEST, as the
direction in which this spider would move next, once the method returns.
turnLeft
public int turnLeft()
- Specifies that this spider is to turn left (90 degrees) relative to
its current direction (heading).
- Returns:
- one of: Room.NORTH, Room.EAST, Room.SOUTH, Room.WEST, as the
direction in which this spider would move next, once the method returns.
getXY
public Point getXY()
- Reports the current coordinates of this spider within the Room.
- Returns:
- the current coordinates of this spider, as a Point object.
setXY
public void setXY(int newX,
int newY)
- Records this spider as being at the "newX, newY" coordinates of the
Room. This method is typically called by the drawing methods of Room,
with "safe" parameters that keep the spider within the "walls" of the
Room. Client programs should call Room.jumpTo() instead.
- Parameters:
- newX - the X ordinate (in pixels) of the new position of this
spider, within Room.
- newY - the Y ordinate (in pixels) of the new position of this
spider, within Room.
- See Also:
- jumpTo
setXY
public void setXY(Point newSpot)
- Records this spider as being at the "newSpot" Point of the
Room. This method is typically called by the drawing methods of Room,
with a "safe" Point parameter that keeps the spider within the "walls"
of the Room. Client programs should call Room.jumpTo() instead.
- Parameters:
- newSpot - the Point representing the spider's new coordinates in the
Room.
- See Also:
- jumpTo
getBelly
public Color getBelly()
- Returns the color of this spider's belly. This color can only be
set with a constructor.
- Returns:
- the Color of this spider's belly.
getSilk
public Color getSilk()
- Returns the current color of this spider's "silk". A different
silk color can be specified with setSilk().
- Returns:
- the current Color of this spider's silk.
- See Also:
- setSilk
setSilk
public void setSilk(Color silkShade)
- Changes the color of this spider's "silk".
- Parameters:
- silkShade - the Color object representing the desired new color of
this spider.