jturtle
Class JTurtle

java.lang.Object
  extended by jturtle.JTurtle

public class JTurtle
extends java.lang.Object

This class is a simple SLogo turtle model for illustrating rudimentary MVC for turtles and simple Graphics2D concepts


Field Summary
(package private) static int HEIGHT
           
(package private)  double myHeading
           
private  java.util.List<TurtleListener> myListeners
           
(package private)  java.awt.geom.Point2D myNextPoint
           
(package private)  java.awt.geom.Point2D myPoint
           
(package private)  Renderer myRenderer
           
(package private) static int START_X
           
(package private) static int START_Y
           
(package private) static int WIDTH
           
 
Constructor Summary
  JTurtle()
          Create a turtle using the default (triangle) image.
  JTurtle(java.awt.Image img)
           
private JTurtle(Renderer renderer)
           
 
Method Summary
 void addTurtleListener(TurtleListener listener)
          Add a listener.
 void draw(java.awt.Graphics g)
          Draw the turtle, pass in a Graphics2D object.
 java.awt.Rectangle getBounds(java.awt.Rectangle r)
          Mirrors the functionality of JComponent/Component.
 double getHeading()
          Returns the naive view of heading, 0 degrees = north
 java.awt.geom.Point2D getPoint()
          Returns the turtle's point (should be centroid)
 void move(int steps)
          Moves the turtle forward.
 void notifyListeners()
          Ask all registered listeners to process this turtle
 void reset()
          Reposition turtle at the origin, facing north.
 void setHeading(double radians)
          Set heading in radians, notify listeners.
 void setHeading(int degrees)
          Set heading in degrees, notify listeners.
 void setPoint(java.awt.geom.Point2D p)
          Sets the turtles point, notifies listeners.
protected  double trueHeading()
          Return the real/true heading of the turtle.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

myHeading

double myHeading

myPoint

java.awt.geom.Point2D myPoint

myNextPoint

java.awt.geom.Point2D myNextPoint

myRenderer

Renderer myRenderer

START_X

static final int START_X
See Also:
Constant Field Values

START_Y

static final int START_Y
See Also:
Constant Field Values

WIDTH

static final int WIDTH
See Also:
Constant Field Values

HEIGHT

static final int HEIGHT
See Also:
Constant Field Values

myListeners

private java.util.List<TurtleListener> myListeners
Constructor Detail

JTurtle

public JTurtle()
Create a turtle using the default (triangle) image. In a more general architecture the image/shape might be passed in at construction time too.


JTurtle

public JTurtle(java.awt.Image img)

JTurtle

private JTurtle(Renderer renderer)
Method Detail

getBounds

public java.awt.Rectangle getBounds(java.awt.Rectangle r)
Mirrors the functionality of JComponent/Component. The current turtle isn't a Component, but it sort of acts like one (it's drawable).

See Also:
Component.getBounds()

reset

public void reset()
Reposition turtle at the origin, facing north.


draw

public void draw(java.awt.Graphics g)
Draw the turtle, pass in a Graphics2D object.

Parameters:
g - is the graphics context in which Turtle is drawn

trueHeading

protected double trueHeading()
Return the real/true heading of the turtle. A Turtle's "heading" is what naive users would expect, i.e., 0 degrees == north, 90 degrees = east, -90 degrees = west. The trueheading translates to what Graphics/geometry expects

Returns:
the true heading in radians

move

public void move(int steps)
Moves the turtle forward.

Parameters:
steps - is the number of steps moved

setHeading

public void setHeading(int degrees)
Set heading in degrees, notify listeners.

Parameters:
degrees - will be Turtle's new heading

setHeading

public void setHeading(double radians)
Set heading in radians, notify listeners.

Parameters:
radians - Turtle's new heading

getHeading

public double getHeading()
Returns the naive view of heading, 0 degrees = north

Returns:
the heading

getPoint

public java.awt.geom.Point2D getPoint()
Returns the turtle's point (should be centroid)

Returns:
the center point

notifyListeners

public void notifyListeners()
Ask all registered listeners to process this turtle


addTurtleListener

public void addTurtleListener(TurtleListener listener)
Add a listener.

Parameters:
listener - is the listener added

setPoint

public void setPoint(java.awt.geom.Point2D p)
Sets the turtles point, notifies listeners.

Parameters:
p - is the new center point