1. To begin your code, you will need to type the following:
import java.util.*;
import java.awt.*;
public class Turtle {
A template for turtle objects is provided by the Turtle class. It specifies characteristics for turtles, such as their color, position, and movement.
2. Create a world to draw in. You can make it as long and wide as needed. Just remember to change the x and y-coordinates in the parentheses.
World habitat = new World(300,300);
You can name the world anything, but in this case, it has been named “habitat.”
3. Here are some commands you should know to start drawing in Java! Just remember to put the name of the turtle and a period before. For example, if the turtle has been named yertle, then put “yertle.” before the command. Always remember to put a semicolon after each line!
a. forward() - The forward command moves the turtle forward. Place the number of pixels you want the turtle to move forward in parenthesis.
b. backward() - The backward command moves the turtle backwards. Place the number of pixels you want the turtle to move backwards in parenthesis.
c. turnLeft() - This command makes the turtle turn to the left.
d. turnRight() - This will make it turn right.
e. penUp() - The pen up command means that the pen is picked up, so that there aren’t any tracks left behind while the turtle is moving.
f. penDown() - When you want visible marks as the turtle moves, then this command. undoes the pen up command.
g. setColor() - You can change the color of the turtle’s marks with the set color command. Put the color you want the marks to be in parenthesis.
h. setWidth() - With this command, you can set the width of the tracks. In parentheses, put the number of thickness you want for the width.
4. Here is an example of what you can code using all these commands:
import java.util.*;
import java.awt.*;
public class TurtleTest
{
public static void main(String[] args)
{
// Create the habitat.
World habitat = new World(500,500);
// Create the turtle in the habitat.
Turtle yertle = new Turtle(habitat);
yertle.forward(50);
yertle.turnRight();
yertle.forward(50);
yertle.turnRight();
yertle.forward(50);
yertle.turnRight();
yertle.forward(50);
}
}
This code makes a square with side length 50.