prev | TOC | next

Working with Turtles

files: Box.java, BorderBox.java, FancyBox.java (help)


Now that we have a bit of knowledge about objects in general, let's talk about Turtles in particular.  As mentioned previously, Turtle is a special Java class that has methods to draw on the Jurtle Display area.  Turtles are specific to Jurtle, so if you talk to your non-Jurtle friends about them, they probably won't have any idea what you're talking about.

You never create an instance of Turtle itself.  When you program in Jurtle you are generally creating subclasses of Turtle.  Because it is a subclass, this means your turtle will know how to do everything a Turtle can do as well as the specific capabilities you add to your subclass.  The subclass must implement at least one method: runTurtle().  This is the entry point to the code of your subclass.  When you have Jurtle create a new Turtle subclass via the File -> New -> New Turtle... menu command, the skeleton code it creates even has the runTurtle() method already inserted and waiting to be filled out.

By default, Turtles are visible on the display as a black triangle.  When the runTurtle() method is called and your code first starts running, the turtle is centered in the display heading North (up).  The turtle can be moved around the display area using commands such as forward(), left(), and right(). There is a pen associated with the turtle.  If the pen is in the down position, it leaves a trail onscreen as the turtle moves.  This method of drawing on the screen by moving a Turtle around is called Turtle Graphics and was popularized by the Logo programming language.

The Jurtle Display Area

  • Unlike the familiar x-y axis you've used when graphing equations or plotting points on a graph, when you draw on a computer screen, the values on the y-axis increase in a downward direction rather than upward! The x-axis is more familiar, as it increases in value as you move from left to right.
  • In Turtle Graphics, a heading of 0° is considered due north, 90° is due east, 180° is due south, and 270° is due west.
  • When a new Turtle is created, it starts facing north at 0°, it is located in the middle of the display area, its pen is down, the pen width is 1 pixel, and pen color is black.

      

  • You don't know the actual width and height of your drawing area until you ask the Turtle to give it to you. You can do this using the getDisplaySize() method which returns a Dimension object.  This object has two instance variables, its width and its height.


Turtles have an extensive API (Application Programmer's Interface).  This is just a fancy name for the set of commands a class understands.  You can see the basic commands by choosing Basic Commands from Jurtle's Help menu.  You can see the full API by choosing the Show Jurtle API from the Help menu.  We will discuss some of the more important ones:

Movement Commands

Description

forward( distance )

Moves the turtle forward the specified distance

backward( distance )

Move the turtle backward the specified distance

right( degrees)

Turns the turtle right the specified number of degrees

left( degrees)

Turns the turtle left the specified number of degrees

setPosition(x, y)

Sets the turtle's location to (x,y) in the display's coordinate system.


 


 

Information Commands

Description

getDisplaySize()

Returns the display's size in pixels as a Dimension object

getPos()

Returns the turtle's position as a Point object


 


 

Drawing Commands

Description

penDown()

Sets the turtle's pen in the down position

penUp()

Sets the turtle's pen in the up position

setPenColor( color )

Sets the turtle's pen's drawing color to the specified Color object

setPenWidth( width )

Sets the turtle's pen's width to the specified integer


 


 

Updating Commands

Description

setAutoUpdatePause( milliseconds )

Sets the length of time in milliseconds the turtle will pause after every AutoUpdate.  AutoUpdates happen when autoUpdate is true and a command is called that has a visible effect on-screen.




The best way to learn about these commands is to experiment with them.

Drawing a box

Download the Box turtle referenced at the top of this lesson:

import com.otherwise.jurtle.*;

public class Box extends Turtle
{
 
    public void runTurtle()
    {
        forward(100);
        right(90);
 
        forward(100);
        right(90);
 
        forward(100);
        right(90);
 
        forward(100);
        right(90);
 
        hideTurtle();
    }

}

If you run Box you will see it draws a simple four-sided box on-screen.  To draw this box you:

  1. move forward 100 pixels
  2. turn right 90 degrees
  3. repeat steps 1-2 three more times.

The final statement in the Box turtle code is to hide the turtle on the screen so you see only the finished box.

Now let's look at a fancier box:

import java.awt.*;
import com.otherwise.jurtle.*;

public class FancyBox extends Turtle
{
   
    public void runTurtle()
    {
        setPenWidth(10);
        setAutoUpdatePause(400);

        setPenColor(Color.green);
        forward(100);
        right(90);

        setPenColor(Color.red);
        forward(100);
        right(90);

        setPenColor(Color.blue);
        forward(100);
        right(90);

        setPenColor(Color.yellow);
        forward(100);
        right(90);

        hideTurtle();       
    }

}

By running FancyBox, you will see it adds some color and the lines are thicker.  Here are the steps:

  1. set the pen width to 10 pixels
  2. set the auto update pause to 400 milliseconds.  This means that after every drawing operation the turtle will pause  0.4 seconds.  This gives you more of a chance to see the individual steps.
  3. set the pen color to the appropriate color for this side
  4. move forward 100 pixels
  5. turn right 90 degrees
  6. repeat steps 3-5 three more times


Absolute positioning and using the Display size

So far our turtle's movement and drawing have been relative to the turtle's current location.  However, for some types of programs we need to draw at specific locations on the display.

Suppose we wanted to draw a border around the display area before drawing something inside it.  To do so we would need to get the size of the display.  The Turtle class has a method getDisplaySize() to return the current size of the display area.  The method returns a Dimension object which has components of width and height.  If the result returned was stored in a variable called size, you would access the width and height as size.width and size.height.

import java.awt.*;
import com.otherwise.jurtle.*;

public class BorderBox extends Turtle
{

    public void runTurtle()
    {
        // Do some setup.
        setAutoUpdatePause( 200 );
        setPenWidth( 20 );
        setPenColor( Color.orange );

        // Get the display's size.
        Dimension size = getDisplaySize();

        // Position the turtle in the upper left
        penUp();
        setPosition( 20, 20 );
        penDown();

        // Now move to the four corners with the pen down.
        setPosition( size.width - 10, 10 );
        setPosition( size.width - 10, size.height - 10 );
        setPosition( 10, size.height - 10 );
        setPosition( 10, 10);
        hideTurtle();
    }

}


The line with getDisplaySize() above makes use of the assignment operator "=" which we haven't seen yet.  This operator looks somewhat like an equation such as 2+3 = 5.  However, rather than stating equality, the assignment operator takes the value that is to its right and assigns it to the variable on the left.

Exercises

Create a new turtle using the File -> New -> New Turtle... menu command.  Give your turtle whatever name you wish.  The name should start with a uppercase letter and cannot contain spaces or punctuation characters.  Now use some of the turtle movement commands discussed above to move your turtle around.  Compile and run your program.  Experiment with different things.