|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectTurtle
public class Turtle
Turtle is a class representing a fun-loving reptile residing in a World. It contains attributes for x and y position, direction, color, etc. The turtle is dynamic and can be moved, turned, made (in)visible, re-colored, etc. Responsibility for drawing the turtle according to its attributes belong to the World-class. The idea is to allow expansion of its capabilities by writing additional methods for more complex behavior and operations.
Field Summary | |
---|---|
static double |
RADIUS
|
Constructor Summary | |
---|---|
Turtle(World w)
Constructs a turtle and places it at the center of the world. |
|
Turtle(World w,
int x,
int y)
Constructs a turtle and places it at the specified position in the world. |
Method Summary | |
---|---|
void |
disablePath()
Disables drawing of the path when the turtle moves. |
double |
distanceTo(int x,
int y)
Calculates the Euclidean distance between a turtle and a given other point. |
void |
enablePath()
Enables drawing of the path when the turtle moves. |
Color |
getColor()
Returns the main Color of the turtle (used to draw its body). |
int |
getDirection()
Returns the direction of the turtle in degrees. |
Color |
getLimbColor()
Returns the Color of the turtle's limbs (head and legs). |
double |
getRadius()
Returns the radius corresponding to the current scaling factor of the turtle. |
double |
getSize()
Returns the scaling factor for the turtle where 1.0 is the default size and 2.0 is double the default size and 0.5 is half the default size. |
World |
getWorld()
Returns the World associated with this turtle. |
int |
getXPos()
Returns the x-coordinate of the turtle. |
int |
getYPos()
Returns the y-coordinate of the turtle. |
boolean |
isPathEnabled()
Returns true if path drawing is enabled and false if it's disabled. |
boolean |
isVisible()
Returns true if the turtle is visible and false if it's invisible. |
void |
move(int step)
Moves the turtle along its current direction by an integer step-size. |
void |
moveTo(int x,
int y)
Repositions the turtle to the given x- and y-coordinates. |
void |
setColor(int red,
int green,
int blue)
Sets the color of the turtle to the specified RGB color. |
void |
setDirection(int direction)
Sets the direction of the tutle in degrees. |
void |
setRadius(double radius)
Set the scaling factor corresponding to the specified radius. |
void |
setSize(double size)
Sets the scaling factor for the turtle where 1.0 is the default size and 2.0 is double the default size and 0.5 is half the default size. |
void |
setVisible(boolean visible)
Sets the visibility status of the turtle. |
String |
toString()
Generates a compact string representation of some of the turtle's attributes. |
void |
turn(int degrees)
Turns the turtle clockwise by the given integer number of degrees. |
void |
turnTo(int x,
int y)
Turns the turtle to face the point specified by the x- and y-coordinate. |
void |
turnTo(Turtle t)
Turns the turtle to face the position of another turtle. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final double RADIUS
Constructor Detail |
---|
public Turtle(World w)
w
- The world which the turtle will reside in.public Turtle(World w, int x, int y)
w
- The world which the turtle will reside in.x
- X-coordinate of the point where the turtle will start at.y
- Y-coordinate of the point where the turtle will start at.Method Detail |
---|
public World getWorld()
World
associated with this turtle.
public int getXPos()
public int getYPos()
public int getDirection()
[0-359]
.
public void setDirection(int direction)
direction
- The direction in degrees.public double getSize()
public void setSize(double size)
size
- Scaling factor of the turtle.public double getRadius()
public void setRadius(double radius)
radius
- The new radius to resize the turtle to.public Color getColor()
Color
of the turtle (used to draw its body).
public Color getLimbColor()
Color
of the turtle's limbs (head and legs).
public void setColor(int red, int green, int blue)
red
- The red component of the color. [0-255]green
- The green component of the color. [0-255]blue
- The blue component of the color. [0-255]public boolean isVisible()
true
if the turtle is visible and false
if it's invisible.
public void setVisible(boolean visible)
visible
- Visibility status. (false hides the turtle and true shows it.)public boolean isPathEnabled()
true
if path drawing is enabled and false
if it's disabled.
public void enablePath()
public void disablePath()
public double distanceTo(int x, int y)
x
- The x-coordinate of the other point.y
- The y-coordinate of the other point.
public String toString()
Example:
Turtle(x: 57, y: 173, direction: 180, size: 2.00000,red: 128, green: 57, blue: 34)
.
toString
in class Object
public void moveTo(int x, int y)
When moveTo is called, the following actions are performed:
x
- X-coordinate the turtle will move to.y
- Y-coordinate the turtle will move to.public void turn(int degrees)
degrees
- The number of degrees to turn.
(Clockwise if positive and counter-clockwise if negative.)public void turnTo(int x, int y)
x
- X-coordinate of the point which to turn towards.y
- Y-coordinate of the point which to turn towards.public void turnTo(Turtle t)
t
- The turtle to turn towards.public void move(int step)
step
- The delta to move the turtle by.
(Moves forward if positive and reverse if negative.)
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |