import java.awt.*; /**
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. @author Michael B. Feldman, The George Washington University (Ada 95) @author Istvan Mohos (Java translation) @see AppFrame @see Room */ class Spider { /* Spider internal states; these can be accessed by client objects only through accessor (get...) and mutator (set...) methods. */ private int currentX; // horizontal position, pixels private int currentY; // vertical position, pixels private int heading; // spider's direction private Color ink; // current drawing color private Color bellyColor = Color.red; // fixed at instantiation /** 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). */ public Spider() { this.setSilk(Color.green); } /** 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. @param startX the X ordinate (in pixels) of the spider's position in the Room (no bounds checking is performed by this constructor). @param startY the Y ordinate (in pixels) of the spider's position in the Room (no bounds checking is performed by this constructor). @param 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. @param ink the Color of the spider's "silk"; may change thereafter. @param bellyColor the Color of the spider's belly; cannot change thereafter. */ public Spider(int startX, int startY, int heading, Color ink, Color bellyColor) { currentX = startX; currentY = startY; this.heading = Math.abs(heading) % 4; this.ink = ink; this.bellyColor = 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. @param spot the starting Point of the spider in the Room (no bounds checking is performed by this constructor). @param 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. @param ink the Color of the spider's "silk"; may change thereafter. @param bellyColor the Color of the spider's belly; cannot change thereafter. */ public Spider(Point spot, int heading, Color ink, Color bellyColor) { currentX = spot.x; currentY = spot.y; this.heading = Math.abs(heading) % 4; this.ink = ink; this.bellyColor = bellyColor; } /** Turn toward the specified direction. (As a matter of style, Java mutator method names that select from multiple values, begin with "set"). @param 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. */ public void setHeading(int heading) { heading = Math.abs(heading) % 4; } /** Reports the current direction (heading) of this spider. @return one of: Room.NORTH, Room.EAST, Room.SOUTH, Room.WEST, as the direction in which this spider would move next. */ public int getHeading() { return heading; } /** Specifies that this spider is to turn right (90 degrees) relative to its current direction (heading). @return one of: Room.NORTH, Room.EAST, Room.SOUTH, Room.WEST, as the direction in which this spider would move next, once the method returns. */ public int turnRight() { if (++heading > Room.WEST) heading = Room.NORTH; return heading; } /** Specifies that this spider is to turn left (90 degrees) relative to its current direction (heading). @return one of: Room.NORTH, Room.EAST, Room.SOUTH, Room.WEST, as the direction in which this spider would move next, once the method returns. */ public int turnLeft() { if (--heading < Room.NORTH) heading = Room.WEST; return heading; } /** Reports the current coordinates of this spider within the Room. @return the current coordinates of this spider, as a Point object. */ public Point getXY() { return (new Point(currentX, currentY)); } /** 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. @param newX the X ordinate (in pixels) of the new position of this spider, within Room. @param newY the Y ordinate (in pixels) of the new position of this spider, within Room. @see Room.jumpTo */ public void setXY(int newX, int newY) { currentX = newX; currentY = newY; } /** 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. @param newSpot the Point representing the spider's new coordinates in the Room. @see Room.jumpTo */ public void setXY(Point newSpot) { currentX = newSpot.x; currentY = newSpot.y; } /** Returns the color of this spider's belly. This color can only be set with a constructor. @return the Color of this spider's belly. */ public Color getBelly() { return bellyColor; } /** Returns the current color of this spider's "silk". A different silk color can be specified with setSilk(). @return the current Color of this spider's silk. @see setSilk */ public Color getSilk() { return ink; } /** Changes the color of this spider's "silk". @param silkShade the Color object representing the desired new color of this spider. */ public void setSilk(Color silkShade) { ink = silkShade; // set up the color pen } } // end definition of class Spider