edu.neu.ccs.pedagogy
Class Turtle

java.lang.Object
  extended byedu.neu.ccs.pedagogy.Turtle

public class Turtle
extends Object

A class to implement turtle graphics that will allow one or more turtle objects to draw in a common BufferedPanel.

Since:
1.1
Version:
2.2

Field Summary
protected  boolean autoRepaint
          Whether or not repaint occurs after each step.
static Paint DEFAULT_PAINT
          The default pen paint.
private static double DEGREES_TO_RADIANS
          Degrees to radians conversion.
protected  double direction
          The turtle direction in degrees measured counterclockwise from the positive horizontal axis.
private  Line2D line
          The line to draw.
static int MINIMUM_SIZE
          The minimum size of the common turtle buffered panel.
(package private)  GeneralPath path
          The path to outline the turtle triangle shape.
static int PEN_DOWN
          Turtle pen state: pen down.
static int PEN_UP
          Turtle pen state: pen up.
protected  Paint penPaint
          The turtle pen paint.
protected  int penState
          The turtle pen state.
private  Point2D point1
          Endpoint 1 of the line.
private  Point2D point2
          Endpoint 2 of the line.
static int PREFERRED_SIZE
          The preferred size of the common turtle buffered panel.
protected  boolean showTurtle
          Whether or not the turtle triangle is visible.
protected static AffineTransform transform
          The affine transform used to scale all turtle drawing.
protected static BufferedPanel turtleBufferPanel
          The common buffered panel for all turtle graphics.
protected static int turtleBufferPanelSize
          The size of the common turtle buffered panel.
protected static Vector turtleList
          The list of turtle objects that have been created.
protected  double xPosition
          The turtle x position.
protected  double yPosition
          The turtle y position.
 
Constructor Summary
Turtle()
          The default turtle constructor creates the turtle buffered panel if that buffered panel has not been created by an earlier turtle and then adds this turtle to the list of all turtles.
 
Method Summary
static void clear()
          Clear the common turtle buffered panel and repaint.
protected  void drawTurtle(Graphics2D g2)
          Draw the turtle triangle at the current turtle position in the current turtle direction.
 boolean getAutoRepaint()
          Return the current auto repaint setting.
 double getDirection()
          Return the turtle direction.
 Paint getPaint()
          Return the turtle pen paint.
 int getPenState()
          Return the turtle pen state.
 Point2D getPosition()
          Return the turtle position.
static BufferedImage getTurtleBuffer()
          Return the buffer in the common turtle buffered panel.
static Graphics2D getTurtleBufferGraphics()
          Return the graphics context for the common turtle buffered panel.
static BufferedPanel getTurtleBufferPanel()
          Create and return the common buffered panel for turtle graphics.
static int getTurtleBufferPanelSize()
          Return the size of the common buffered panel for turtle graphics.
static Turtle[] getTurtles()
          Return an array of all turtles that have been created.
 double getX()
          Return the turtle x position.
 double getY()
          Return the turtle y position.
static void hideAllTurtles()
          Hide all turtle triangles.
 void hideTurtle()
          Hide the turtle triangle.
 boolean isVisible()
          Return whether or not the turtle triangle is visible.
static void repaint()
          Repaint the common turtle buffered panel.
 void reset()
          Reset the turtle to its default initial state and then repaint.
static void selfTest()
          A simple self test.
 void setAutoRepaint(boolean enabled)
          Set auto repaint, that is, whether or not to repaint after each step, to the given value.
static void setCoordinatesViaBounds(Rectangle2D world)
          Set the coordinate system in the common turtle buffered panel using the given bounds in the world coordinate system to define the affine coordinate transformation.
static void setCoordinatesViaBounds(Rectangle2D world, int inset)
          Set the coordinate system in the common turtle buffered panel using the given bounds in the world coordinate system and the given image inset to define the affine coordinate transformation.
 void setDirection(double direction)
          Set the turtle direction in degrees counterclockwise from the positive horizontal axis.
 void setPaint(int r, int g, int b)
          Set the turtle pen paint via r,g,b components.
 void setPaint(int r, int g, int b, int alpha)
          Set the turtle pen paint via r, g, b, alpha components.
 void setPaint(Paint paint)
          Set the turtle pen paint.
 void setPenState(int penState)
          Set the turtle pen state.
 void setPosition(double x, double y)
          Set the turtle position.
 void setPosition(Point2D position)
          Set the turtle position.
static void setTurtleBufferPanelSize(int size)
          Set the size of the common buffered panel for turtle graphics (this must be called before the buffered panel is created).
 void setVisible(boolean visible)
          Show or hide the turtle triangle using the given parameter to decide.
static void showAllTurtles()
          Show all turtle triangles.
 void showTurtle()
          Show the turtle triangle.
 void step(double distance)
          Move the turtle from its current position by the given distance in its current direction and if the current pen state is PEN_DOWN then draw a line using the current pen paint.
 void turn(double angle)
          Increment the turtle direction by the given angle in degrees.
static void useCenterCoordinates()
          Use a coordinate system with the origin at the center of the common turtle buffered panel and with x increasing rightwards and y increasing upwards.
static void useCornerCoordinates()
          Use a coordinate system with the origin at the lower left corner of the common turtle buffered panel and with x increasing rightwards and y increasing upwards.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MINIMUM_SIZE

public static final int MINIMUM_SIZE
The minimum size of the common turtle buffered panel.

See Also:
Constant Field Values

PREFERRED_SIZE

public static final int PREFERRED_SIZE
The preferred size of the common turtle buffered panel.

See Also:
Constant Field Values

DEFAULT_PAINT

public static final Paint DEFAULT_PAINT
The default pen paint.


PEN_UP

public static final int PEN_UP
Turtle pen state: pen up.

See Also:
Constant Field Values

PEN_DOWN

public static final int PEN_DOWN
Turtle pen state: pen down.

See Also:
Constant Field Values

DEGREES_TO_RADIANS

private static final double DEGREES_TO_RADIANS
Degrees to radians conversion.

See Also:
Constant Field Values

turtleBufferPanel

protected static BufferedPanel turtleBufferPanel
The common buffered panel for all turtle graphics.


turtleBufferPanelSize

protected static int turtleBufferPanelSize
The size of the common turtle buffered panel.


transform

protected static AffineTransform transform
The affine transform used to scale all turtle drawing.


turtleList

protected static Vector turtleList
The list of turtle objects that have been created.


xPosition

protected double xPosition
The turtle x position.


yPosition

protected double yPosition
The turtle y position.


direction

protected double direction
The turtle direction in degrees measured counterclockwise from the positive horizontal axis.


penPaint

protected Paint penPaint
The turtle pen paint.


penState

protected int penState
The turtle pen state.


showTurtle

protected boolean showTurtle
Whether or not the turtle triangle is visible.


autoRepaint

protected boolean autoRepaint
Whether or not repaint occurs after each step.


line

private Line2D line
The line to draw.


point1

private Point2D point1
Endpoint 1 of the line.


point2

private Point2D point2
Endpoint 2 of the line.


path

GeneralPath path
The path to outline the turtle triangle shape.

Constructor Detail

Turtle

public Turtle()
The default turtle constructor creates the turtle buffered panel if that buffered panel has not been created by an earlier turtle and then adds this turtle to the list of all turtles. The default turtle settings are:

Method Detail

setTurtleBufferPanelSize

public static void setTurtleBufferPanelSize(int size)
Set the size of the common buffered panel for turtle graphics (this must be called before the buffered panel is created).

Parameters:
size - the new turtle buffered panel size
See Also:
getTurtleBufferPanel(), getTurtleBufferPanelSize()

getTurtleBufferPanelSize

public static int getTurtleBufferPanelSize()
Return the size of the common buffered panel for turtle graphics.

See Also:
getTurtleBufferPanel(), setTurtleBufferPanelSize(int)

getTurtleBufferPanel

public static BufferedPanel getTurtleBufferPanel()
Create and return the common buffered panel for turtle graphics.

See Also:
setTurtleBufferPanelSize(int), getTurtleBufferPanelSize()

useCenterCoordinates

public static void useCenterCoordinates()
Use a coordinate system with the origin at the center of the common turtle buffered panel and with x increasing rightwards and y increasing upwards.

See Also:
useCornerCoordinates(), setCoordinatesViaBounds(Rectangle2D), setCoordinatesViaBounds(Rectangle2D, int)

useCornerCoordinates

public static void useCornerCoordinates()
Use a coordinate system with the origin at the lower left corner of the common turtle buffered panel and with x increasing rightwards and y increasing upwards.

See Also:
useCenterCoordinates(), setCoordinatesViaBounds(Rectangle2D), setCoordinatesViaBounds(Rectangle2D, int)

setCoordinatesViaBounds

public static void setCoordinatesViaBounds(Rectangle2D world)
Set the coordinate system in the common turtle buffered panel using the given bounds in the world coordinate system to define the affine coordinate transformation.

Parameters:
world - the bounds in world coordinates
See Also:
useCenterCoordinates(), useCornerCoordinates(), setCoordinatesViaBounds(Rectangle2D, int)

setCoordinatesViaBounds

public static void setCoordinatesViaBounds(Rectangle2D world,
                                           int inset)
Set the coordinate system in the common turtle buffered panel using the given bounds in the world coordinate system and the given image inset to define the affine coordinate transformation.

Parameters:
world - the bounds in world coordinates
inset - the inset of the turtle drawing within the image
See Also:
useCenterCoordinates(), useCornerCoordinates(), setCoordinatesViaBounds(Rectangle2D)

getTurtleBufferGraphics

public static Graphics2D getTurtleBufferGraphics()
Return the graphics context for the common turtle buffered panel.


getTurtleBuffer

public static BufferedImage getTurtleBuffer()
Return the buffer in the common turtle buffered panel.


clear

public static void clear()
Clear the common turtle buffered panel and repaint.


repaint

public static void repaint()
Repaint the common turtle buffered panel.


getTurtles

public static Turtle[] getTurtles()
Return an array of all turtles that have been created. If none have been created then the array returned will have zero elements.


showAllTurtles

public static void showAllTurtles()
Show all turtle triangles.


hideAllTurtles

public static void hideAllTurtles()
Hide all turtle triangles.


step

public void step(double distance)

Move the turtle from its current position by the given distance in its current direction and if the current pen state is PEN_DOWN then draw a line using the current pen paint.

The default is to set auto repaint to true. This will give immediate feedback for each step call but will be slower if lots of step calls are made.

To improve the speed of turtle graphics at the expense of immediate feedback, make the call on a Turtle named T:

Then, to repaint the turtle buffered panel when the turtle drawing is complete, make the static call:

This design dramatically improves graphics performance.

Parameters:
distance - the distance to step
See Also:
setAutoRepaint(boolean), getAutoRepaint()

turn

public void turn(double angle)
Increment the turtle direction by the given angle in degrees.

Parameters:
angle - the angle in degrees used to increment the direction
See Also:
setDirection(double)

showTurtle

public void showTurtle()
Show the turtle triangle.

See Also:
hideTurtle(), setVisible(boolean), isVisible()

hideTurtle

public void hideTurtle()
Hide the turtle triangle.

See Also:
showTurtle(), setVisible(boolean), isVisible()

setVisible

public void setVisible(boolean visible)
Show or hide the turtle triangle using the given parameter to decide.

Parameters:
visible - if true then show the turtle otherwise hide it
See Also:
showTurtle(), hideTurtle(), isVisible()

isVisible

public boolean isVisible()
Return whether or not the turtle triangle is visible.

See Also:
showTurtle(), hideTurtle(), setVisible(boolean)

setPosition

public void setPosition(double x,
                        double y)
Set the turtle position.

Parameters:
x - the turtle x position
y - the turtle y position
See Also:
setPosition(Point2D), getPosition(), getX(), getY()

setPosition

public void setPosition(Point2D position)
Set the turtle position.

Parameters:
position - the turtle position
See Also:
setPosition(double, double), getPosition(), getX(), getY()

getPosition

public Point2D getPosition()
Return the turtle position.

See Also:
setPosition(double, double), setPosition(Point2D), getX(), getY()

getX

public double getX()
Return the turtle x position.

See Also:
setPosition(double, double), setPosition(Point2D), getPosition(), getY()

getY

public double getY()
Return the turtle y position.

See Also:
setPosition(double, double), setPosition(Point2D), getPosition(), getX()

setDirection

public void setDirection(double direction)
Set the turtle direction in degrees counterclockwise from the positive horizontal axis.

Parameters:
direction - the new turtle direction
See Also:
getDirection(), turn(double)

getDirection

public double getDirection()
Return the turtle direction.

See Also:
setDirection(double)

setPaint

public void setPaint(Paint paint)
Set the turtle pen paint.

Parameters:
paint - the paint to use for drawing this turtle
See Also:
getPaint(), setPaint(int, int, int), setPaint(int, int, int, int)

setPaint

public void setPaint(int r,
                     int g,
                     int b)
Set the turtle pen paint via r,g,b components.

Parameters:
r - the red component of the Color to be used to paint
g - the green component of the Color to be used to paint
b - the blue component of the Color to be used to paint
See Also:
getPaint(), setPaint(Paint), setPaint(int, int, int, int)

setPaint

public void setPaint(int r,
                     int g,
                     int b,
                     int alpha)
Set the turtle pen paint via r, g, b, alpha components.

Parameters:
r - the red component of the Color to be used to paint
g - the green component of the Color to be used to paint
b - the blue component of the Color to be used to paint
alpha - the alpha component of the Color to be used to paint
See Also:
getPaint(), setPaint(Paint), setPaint(int, int, int)

getPaint

public Paint getPaint()
Return the turtle pen paint.

See Also:
setPaint(Paint)

setPenState

public void setPenState(int penState)
Set the turtle pen state.

Parameters:
penState - the pen state: PEN_UP or PEN_DOWN
See Also:
getPenState(), PEN_UP, PEN_DOWN

getPenState

public int getPenState()
Return the turtle pen state.

See Also:
setPenState(int), PEN_UP, PEN_DOWN

setAutoRepaint

public void setAutoRepaint(boolean enabled)
Set auto repaint, that is, whether or not to repaint after each step, to the given value.

Parameters:
enabled - the new auto repaint setting
See Also:
getAutoRepaint(), step(double)

getAutoRepaint

public boolean getAutoRepaint()
Return the current auto repaint setting.

See Also:
setAutoRepaint(boolean), step(double)

reset

public void reset()
Reset the turtle to its default initial state and then repaint. The default settings are:


drawTurtle

protected void drawTurtle(Graphics2D g2)

Draw the turtle triangle at the current turtle position in the current turtle direction.

This method is called by the paintOver method of the common turtle buffered panel during repaint actions.


selfTest

public static void selfTest()
A simple self test.