Introduction
In this course, Turtle Graphics will be used as a motivating start into object-oriented programming. Turtle Graphics is an easy and fun learning programming environment. It has three attributes: a location, an orientation (or direction), and a pen. The pen, too, has attributes: color, width, and on/off state. The turtle moves with commands that are relative to its own position, such as "move forward 10 spaces" and "turn left 90 degrees". The pen carried by the turtle can also be controlled, by enabling it, setting its color, or setting its width.
The simplest programs have of a sequence of actions which are executed in the exact order of appearance. The following example shows a program in which the Turtle draws a square.
public class MyTurtleApp {
public static void main(String[] args) {
MWorld myWorld = new MWorld("My First Turtle World");
MTurtle turtle = new MTurtle(myWorld);
// draw a square
turtle.forward(100);
turtle.turnRight(90);
turtle.forward(100);
turtle.turnRight(90);
turtle.forward(100);
turtle.turnRight(90);
turtle.forward(100);
}
}
The output will be like this:
MTurtle is a special Java class that has methods to draw on the MWorld. You need to import both classes into your program. This is how to import those classes.
1. Right Click on Your Java Project àBuild PathàConfigure Build Path.
2. Click on Libraries tab, Click Add External Class Folder
3. Locate the folder where your MWorld.class and MTurtle.class.
4. Click OK