ch.aplu.turtle
Class Turtle

java.lang.Object
  |
  +--ch.aplu.turtle.Turtle

public class Turtle
extends Object

The core class for Turtles. For a simple example on how to use Turtles, cf. the Java Turtle Package description.

Version:
0.1.1
Author:
Regula Hoefer-Isenegger

Field Summary
protected static int CLIP
          Represents clip mode.
protected static int DEFAULT_ANGLE_RESOLUTION
          Represents the default angle resolution.
protected static int DEFAULT_EDGE_BEHAVIOUR
          Represents the default edge behaviour (i.e.
protected static int DEFAULT_FRAMES_PER_SECOND
          Specifies how many frames per second are used for turtle animation.
protected static Color DEFAULT_PEN_COLOR
          Specifies the default pen color.
protected static double DEFAULT_SPEED
          Represents the default speed (velocity).
protected static Color DEFAULT_TURTLE_COLOR
          Specifies the default turtle color.
protected static int WRAP
          Represents wrap mode.
 
Constructor Summary
Turtle()
          Create a new Turtle in its own new Window.
Turtle(Color color)
          Create a new Turtle with specified color in its own new Window.
Turtle(Turtle otherTurtle)
          Create a new Turtle in the same TurtleContainer (Window) as otherTurtle
Turtle(Turtle otherTurtle, Color color)
          Create a new Turtle with the specified color in the same TurtleContainer (Window) as otherTurtle
Turtle(TurtleContainer turtleContainer)
          Create a new Turtle and puts it into the (already existing) turtleContainer
Turtle(TurtleContainer turtleContainer, Color color)
          Create a new Turtle with specified color in the specified turtleContainer.
 
Method Summary
 Turtle back(double distance)
          Same as bk(double distance).
 Turtle bk(double distance)
          Moves the Turtle backwards.
 Turtle clean()
          Clears away all that was painted in some way by a turtle (such as lines, fillings, text, stamps etc.)
 Turtle clip()
          Set the turtle to clip-mode.
 boolean clips()
          Tells wheter the turtle is in clip mode.
protected  LineRenderer createLineRenderer()
          Create a LineRenderer which is responsible for the correct drawing of the lines.
protected  TurtleFactory createTurtleFactory()
          Create a TurtleFactory which provides for the Turtle pictures.
protected  TurtleRenderer createTurtleRenderer()
          Create a TurtleRenderer which is responsible for the correct drawing of the Turtle.
 double curX()
          Query the current x-coordinate.
 double curY()
          Query the current y-coordinate.
 double distance(double x, double y)
          Query the distance from the current location to the given one.
 double distance(Point2D.Double p)
          Query the distance from the current location to the given one.
 Turtle fd(double distance)
          Moves the Turtle forwards.
 Turtle fill()
          Fills the region the Turtle is in.
 Turtle fill(double x, double y)
          Fills the region with coordinates x and y.
 Turtle forward(double distance)
          Same as fd(double distance)
protected  int getAngleResolution()
          Get the angle resolution.
 String[] getAvailableFontFamilies()
          Provides information about all font families (e.g.
 Color getColor()
          Query the turtle's current color.
protected  int getEdgeBehaviour()
          Returns the current edge behaviour.
 Font getFont()
          Returns the current Font.
 Pen getPen()
          Get the Turtles Pen.
 Playground getPlayground()
          Get the Playground.
 Point2D.Double getPos()
          Query the turtle's position
protected  Point2D.Double getPosition()
           
 TurtleFactory getTurtleFactory()
          Returns the TurtleFactory of this turtle.
 double getX()
          Query the turtle's x-position.
 double getY()
          Query the turtle's y-position.
 double heading()
          Query the Turtles heading.
 double heading(double degrees)
          Set the Turtles heading to the new value.
 Turtle hideTurtle()
          Hides the turtle.
 Turtle home()
          Move the Turtle back "home", i.e. set its position to the origin, facing NORTH.
 Turtle ht()
          Hides the turtle.
protected  void init(Playground playground, Color color)
          Initialize the Turtle.
protected  void init(TurtleContainer turtleContainer, Color color)
          This is only a "meta-method" for calling the init(Playground, Color) method.
protected  void internalHide()
          This is the method called by the public methods ht() and hideTurtle().
protected  void internalPenErase()
           
protected  void internalSetPos(double x, double y)
          Set the Turtles Position.
protected  void internalSetX(double x)
          Set the Turtles x-Coordinate.
protected  void internalSetY(double y)
          Set the Turtles y-Coordinate.
 boolean isHidden()
          Tells wheter the Turtle is hidden or not.
 boolean isPenUp()
          Query the Pens state (up or down).
 Turtle label(String text)
          Paints the specified Text at the current Turtle position.
 Turtle left(double degrees)
          Same as lt(double degrees)
 Turtle lt(double degrees)
          Turns the Turtle degrees degrees to the left.
 Turtle pd()
          Lowers the Turtles Pen down so it will draw a line when moving.
 Turtle pe()
          The Turtles Pen is changed to an eraser (which is in fact a pen with background color).
 Turtle penDown()
          Lowers the Turtles Pen down so it will draw a line when moving.
 Turtle penErase()
          The Turtles Pen is changed to an eraser (which is in fact a pen with background color).
 Turtle penUp()
          Lifts the Turtles Pen up so it won't draw a line anymore when moving.
 int penWidth()
          Query the pen width.
 Turtle penWidth(int newWidth)
          Set the pen width.
 Turtle pu()
          Lifts the Turtles Pen up so it won't draw a line anymore when moving.
 Turtle reinit()
          Resets the turtle to its standard settings.
 Turtle right(double degrees)
          Same as rt(double degrees).
 Turtle rt(double degrees)
          Turns the Turtle degrees degrees to the right.
 Turtle setAngleResolution(int newResolution)
          Set the angle resolution for the turtle's pictures.
 Turtle setColor(Color color)
          Set the turtle's color to the specified one.
protected  void setEdgeBehaviour(int edgeBehaviour)
          Sets the edge behaviour to the specified value;
 Turtle setFillColor(Color color)
          Set the fill color to the specified one.
 Turtle setFont(Font font)
          Sets the current Font to the specified one.
 Turtle setFont(String fontName, int style, int size)
          Change the current font to the specified one.
 Turtle setFontSize(int size)
          Sets the Font size.
 Turtle setFontStyle(int style)
          Sets the Font style.
 Turtle setH(double degrees)
          Set the Turtles heading. 0 means facing NORTH.
 Turtle setHeading(double degrees)
          This is the same as setH(double degrees).
 Turtle setLineWidth(double lineWidth)
          Set the Line Thickness.
 Turtle setLineWidth(float lineWidth)
          Set the Line Thickness.
 Turtle setPenColor(Color color)
          Set the Turtles Pen color.
 Turtle setPos(double x, double y)
          Put the turtle to a new position with specified x- and y-coordinates.
 Turtle setPos(Point2D.Double p)
          Put the turtle to a new position.
 Turtle setX(double x)
          Sets the x-coordinate of the Turtles position to the given value.
 Turtle setY(double y)
          Sets the y-coordinate of the Turtles position to the given value.
 Turtle showTurtle()
          The same as st().
 Turtle speed(double newSpeed)
          Set the Turtles speed.
 Turtle st()
          Sets the turtle to show mode.
 Turtle stampTurtle()
          Leave an imprint of the Turtle on the "canvas".
 Turtle toBottom()
          Put this turtle to the bottom.
 Turtle toTop()
          Put this turtle to the top.
 double towards(double x, double y)
          Calculates the direction to a given point.
 double towards(Point2D.Double p)
          Calculates the direction to a given point.
 Turtle wrap()
          Causes the turtle to wrap around the edges.
 boolean wraps()
          Tells wheter the turtle is in wrap mode.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CLIP

protected static final int CLIP
Represents clip mode.

See Also:
WRAP, clip(), Constant Field Values

WRAP

protected static final int WRAP
Represents wrap mode.

See Also:
CLIP, wrap(), Constant Field Values

DEFAULT_EDGE_BEHAVIOUR

protected static int DEFAULT_EDGE_BEHAVIOUR
Represents the default edge behaviour (i.e. CLIP or WRAP).

See Also:
CLIP, WRAP, clip(), wrap()

DEFAULT_SPEED

protected static double DEFAULT_SPEED
Represents the default speed (velocity).

See Also:
speed(double)

DEFAULT_ANGLE_RESOLUTION

protected static int DEFAULT_ANGLE_RESOLUTION
Represents the default angle resolution. It specifies how many different turtle pictures are generated.


DEFAULT_FRAMES_PER_SECOND

protected static int DEFAULT_FRAMES_PER_SECOND
Specifies how many frames per second are used for turtle animation.


DEFAULT_TURTLE_COLOR

protected static Color DEFAULT_TURTLE_COLOR
Specifies the default turtle color.

See Also:
setColor(java.awt.Color)

DEFAULT_PEN_COLOR

protected static Color DEFAULT_PEN_COLOR
Specifies the default pen color.

See Also:
setPenColor(java.awt.Color)
Constructor Detail

Turtle

public Turtle()
Create a new Turtle in its own new Window.


Turtle

public Turtle(Color color)
Create a new Turtle with specified color in its own new Window.


Turtle

public Turtle(TurtleContainer turtleContainer)
Create a new Turtle and puts it into the (already existing) turtleContainer


Turtle

public Turtle(TurtleContainer turtleContainer,
              Color color)
Create a new Turtle with specified color in the specified turtleContainer.


Turtle

public Turtle(Turtle otherTurtle)
Create a new Turtle in the same TurtleContainer (Window) as otherTurtle


Turtle

public Turtle(Turtle otherTurtle,
              Color color)
Create a new Turtle with the specified color in the same TurtleContainer (Window) as otherTurtle

Method Detail

init

protected void init(Playground playground,
                    Color color)
Initialize the Turtle. If you want to inherit from this class and add new Turtle attributes, you might want to overwrite this method.


init

protected void init(TurtleContainer turtleContainer,
                    Color color)
This is only a "meta-method" for calling the init(Playground, Color) method.

See Also:
init(Playground, Color)

reinit

public Turtle reinit()
Resets the turtle to its standard settings. Only the color will be kept.

Returns:
the turtle to allow chaining.

setAngleResolution

public Turtle setAngleResolution(int newResolution)
Set the angle resolution for the turtle's pictures. It specifies how many pictures are used. e.g. an angle resolution of 90 means that you get one picture for every 4 degrees (= 360/90 degrees).

See Also:
DEFAULT_ANGLE_RESOLUTION

getTurtleFactory

public TurtleFactory getTurtleFactory()
Returns the TurtleFactory of this turtle.

See Also:
TurtleFactory

createLineRenderer

protected LineRenderer createLineRenderer()
Create a LineRenderer which is responsible for the correct drawing of the lines.

Returns:
the new LineRenderer

createTurtleRenderer

protected TurtleRenderer createTurtleRenderer()
Create a TurtleRenderer which is responsible for the correct drawing of the Turtle.

Returns:
the new TurtleRenderer

createTurtleFactory

protected TurtleFactory createTurtleFactory()
Create a TurtleFactory which provides for the Turtle pictures.

Returns:
the new TurtleFactory

getAngleResolution

protected int getAngleResolution()
Get the angle resolution.

See Also:
setAngleResolution(int)

getPlayground

public Playground getPlayground()
Get the Playground.


getX

public double getX()
Query the turtle's x-position.


getY

public double getY()
Query the turtle's y-position.


getPos

public Point2D.Double getPos()
Query the turtle's position


setX

public Turtle setX(double x)
Sets the x-coordinate of the Turtles position to the given value.

Returns:
the turtle to allow chaining.

setY

public Turtle setY(double y)
Sets the y-coordinate of the Turtles position to the given value.

Returns:
the turtle to allow chaining.

internalSetX

protected void internalSetX(double x)
Set the Turtles x-Coordinate. No repainting!!!


internalSetY

protected void internalSetY(double y)
Set the Turtles y-Coordinate. No repainting!!!


internalSetPos

protected void internalSetPos(double x,
                              double y)
Set the Turtles Position. No repainting!!!


ht

public Turtle ht()
Hides the turtle.

Returns:
the turtle to allow chaining.
See Also:
st()

hideTurtle

public Turtle hideTurtle()
Hides the turtle. This is the same as ht().

Returns:
the turtle to allow chaining.
See Also:
st()

internalHide

protected void internalHide()
This is the method called by the public methods ht() and hideTurtle(). Here the actual hiding takes place.

See Also:
ht(), hideTurtle()

st

public Turtle st()
Sets the turtle to show mode. That means that the Turtle will be drawn.

Returns:
the turtle to allow chaining.
See Also:
ht()

showTurtle

public Turtle showTurtle()
The same as st().

Returns:
the turtle to allow chaining.
See Also:
st()

isHidden

public boolean isHidden()
Tells wheter the Turtle is hidden or not.

Returns:
true if the turtle is hidden, false otherwise.

setHeading

public Turtle setHeading(double degrees)
This is the same as setH(double degrees).

Returns:
the turtle to allow chaining.
See Also:
setH(double)

setH

public Turtle setH(double degrees)
Set the Turtles heading. 0 means facing NORTH.
the angles are measured clockwise.

Returns:
the turtle to allow chaining.
See Also:
setHeading(double)

heading

public double heading()
Query the Turtles heading.

See Also:
setH(double)

heading

public double heading(double degrees)
Set the Turtles heading to the new value.

Returns:
the old (previous) value.
See Also:
setH(double)

speed

public Turtle speed(double newSpeed)
Set the Turtles speed. If you try to set the speed to 0, it will be set to 1 (very slow). A negative speed means that moving the turtle (fd, bk) will not be animated.
The unit is pixels per second (up to certain bounds depending on the CPU etc.).
Remark: Dashed lines will only be painted as you expect it with speed set to -1.

Returns:
the turtle to allow chaining.
See Also:
fd(double), bk(double)

lt

public Turtle lt(double degrees)
Turns the Turtle degrees degrees to the left.

Returns:
the turtle to allow chaining.
See Also:
rt(double)

left

public Turtle left(double degrees)
Same as lt(double degrees)

Returns:
the turtle to allow chaining.
See Also:
lt(double)

rt

public Turtle rt(double degrees)
Turns the Turtle degrees degrees to the right.

Returns:
the turtle to allow chaining.
See Also:
rt(double)

right

public Turtle right(double degrees)
Same as rt(double degrees).

Returns:
the turtle to allow chaining.
See Also:
rt(double)

forward

public Turtle forward(double distance)
Same as fd(double distance)

Returns:
the turtle to allow chaining.
See Also:
fd(double)

fd

public Turtle fd(double distance)
Moves the Turtle forwards. Negative values for distance are allowed. In that case, the Turtle will move backwards.

Returns:
the turtle to allow chaining.
See Also:
bk(double)

back

public Turtle back(double distance)
Same as bk(double distance).

Returns:
the turtle to allow chaining.
See Also:
bk(double)

bk

public Turtle bk(double distance)
Moves the Turtle backwards. Negative values for distance are allowed. In that case, the Turtle will move forwards.

Returns:
the turtle to allow chaining.
See Also:
fd(double)

getPosition

protected Point2D.Double getPosition()

distance

public double distance(double x,
                       double y)
Query the distance from the current location to the given one.


distance

public double distance(Point2D.Double p)
Query the distance from the current location to the given one.


penUp

public Turtle penUp()
Lifts the Turtles Pen up so it won't draw a line anymore when moving. This is the same as pu().

Returns:
the turtle to allow chaining.
See Also:
pu(), penDown(), pd()

pu

public Turtle pu()
Lifts the Turtles Pen up so it won't draw a line anymore when moving. This is the same as penUp().

Returns:
the turtle to allow chaining.
See Also:
penUp(), penDown(), pd()

penDown

public Turtle penDown()
Lowers the Turtles Pen down so it will draw a line when moving. This is the same as pd().

Returns:
the turtle to allow chaining.
See Also:
pd(), penUp(), pu()

pd

public Turtle pd()
Lowers the Turtles Pen down so it will draw a line when moving. This is the same as penDown().

Returns:
the turtle to allow chaining.
See Also:
penDown(), penUp(), pu()

isPenUp

public boolean isPenUp()
Query the Pens state (up or down).

Returns:
true if the Pen is up, false otherwise.
See Also:
pu(), pd()

getPen

public Pen getPen()
Get the Turtles Pen. You need it if you want to change end caps etc.

See Also:
Pen

setLineWidth

public Turtle setLineWidth(double lineWidth)
Set the Line Thickness. This works only neatly in clip mode (yet).

See Also:
clip(), wrap()

setLineWidth

public Turtle setLineWidth(float lineWidth)
Set the Line Thickness. This works only neatly in clip mode (yet).

See Also:
clip(), wrap()

setColor

public Turtle setColor(Color color)
Set the turtle's color to the specified one.

Returns:
the turtle to allow chaining.

setFillColor

public Turtle setFillColor(Color color)
Set the fill color to the specified one.

Returns:
the turtle to allow chaining.
See Also:
fill()

getColor

public Color getColor()
Query the turtle's current color.


setPenColor

public Turtle setPenColor(Color color)
Set the Turtles Pen color.

Returns:
the turtle to allow chaining.

home

public Turtle home()
Move the Turtle back "home", i.e. set its position to the origin, facing NORTH. Color, PenColor etc. remain the same.

Returns:
the turtle to allow chaining.
See Also:
reinit()

penErase

public Turtle penErase()
The Turtles Pen is changed to an eraser (which is in fact a pen with background color). This is the same as pe()

Returns:
the turtle to allow chaining.
See Also:
pe()

pe

public Turtle pe()
The Turtles Pen is changed to an eraser (which is in fact a pen with background color). This is the same as penErase()

Returns:
the turtle to allow chaining.
See Also:
penErase()

internalPenErase

protected void internalPenErase()

setPos

public Turtle setPos(double x,
                     double y)
Put the turtle to a new position with specified x- and y-coordinates.

Returns:
the turtle to allow chaining.

setPos

public Turtle setPos(Point2D.Double p)
Put the turtle to a new position.

Returns:
the turtle to allow chaining.

stampTurtle

public Turtle stampTurtle()
Leave an imprint of the Turtle on the "canvas".

Returns:
the turtle to allow chaining.

towards

public double towards(double x,
                      double y)
Calculates the direction to a given point.

Returns:
the direction from the current turtle position towards the given point, measured in degrees and clockwise from the vertical upwards position.

towards

public double towards(Point2D.Double p)
Calculates the direction to a given point.

Returns:
the direction from the current turtle position towards the given point, measured in degrees and clockwise from the vertical upwards position.

curX

public double curX()
Query the current x-coordinate.


curY

public double curY()
Query the current y-coordinate.


toBottom

public Turtle toBottom()
Put this turtle to the bottom. So any other turtle in the same Playground will be drawn over it.

Returns:
the turtle to allow chaining.

toTop

public Turtle toTop()
Put this turtle to the top. So it will be drawn over any other turtle in the same Playground.

Returns:
the turtle to allow chaining.

penWidth

public Turtle penWidth(int newWidth)
Set the pen width.

Returns:
the turtle to allow chaining.

penWidth

public int penWidth()
Query the pen width.


getEdgeBehaviour

protected int getEdgeBehaviour()
Returns the current edge behaviour.

See Also:
CLIP, WRAP

setEdgeBehaviour

protected void setEdgeBehaviour(int edgeBehaviour)
Sets the edge behaviour to the specified value;

See Also:
CLIP, WRAP

clip

public Turtle clip()
Set the turtle to clip-mode. It could be that the turtle moves beyond the edge of the window. You won't see it anymore unless it finds its way back :-)
The default edge behaviour is clipping.

Returns:
the turtle to allow chaining.
See Also:
wrap(), clips()

wrap

public Turtle wrap()
Causes the turtle to wrap around the edges. This means: as soon as the turtle leaves the Window on one side, it re- appears on the opposite side.

Returns:
the turtle to allow chaining.
See Also:
clip()

clips

public boolean clips()
Tells wheter the turtle is in clip mode.

Returns:
true if in clip mode, false otherwise.
See Also:
clip(), wraps()

wraps

public boolean wraps()
Tells wheter the turtle is in wrap mode.

Returns:
true if in wrap mode, false otherwise.
See Also:
wrap(), clips()

fill

public Turtle fill()
Fills the region the Turtle is in. A Region is bounded by lines of any other color than the background color and by the border of the Window.

Returns:
the turtle to allow chaining.

fill

public Turtle fill(double x,
                   double y)
Fills the region with coordinates x and y. A Region is bounded by lines of any other color than the background color and by the border of the Window.

Returns:
the turtle to allow chaining.

clean

public Turtle clean()
Clears away all that was painted in some way by a turtle (such as lines, fillings, text, stamps etc.)

Returns:
the turtle to allow chaining.

label

public Turtle label(String text)
Paints the specified Text at the current Turtle position.

Returns:
the turtle to allow chaining.

setFont

public Turtle setFont(Font font)
Sets the current Font to the specified one.

Returns:
the turtle to allow chaining.
See Also:
Font

setFont

public Turtle setFont(String fontName,
                      int style,
                      int size)
Change the current font to the specified one. If you want to know what fonts are available on your system, call #getAvailableFontFamilies() .

See Also:
getAvailableFontFamilies(), more information about fontName, style and size.

setFontSize

public Turtle setFontSize(int size)
Sets the Font size.

Returns:
the turtle to allow chaining.
See Also:
changing the font style, changing the whole font.

setFontStyle

public Turtle setFontStyle(int style)
Sets the Font style. This is either java.awt.Font.PLAIN, java.awt.Font.BOLD, java.awt.Font.ITALIC or java.awt.Font.BOLD+java.awt.Font.ITALIC

Returns:
the turtle to allow chaining.
See Also:
changing the font size, changing the whole font.

getAvailableFontFamilies

public String[] getAvailableFontFamilies()
Provides information about all font families (e.g. Roman) currently available on your system. This might be useful when you want to change the font.

See Also:
setFont(java.awt.Font), setFont(java.lang.String, int, int)

getFont

public Font getFont()
Returns the current Font.

See Also:
changing the font size, changing the font style, changing the whole font.