JAVA: Drawing



Logo and Turtles

Logo is an educational programming language, designed in 1967 by Wally Feurzeig, Seymour Papert, and Cynthia Solomon. Logo is not an acronym: the name was coined by Feurzeig while he was at Bolt, Beranek and Newman, and derives from the Greek logos, meaning word or thought.

A general-purpose language, Logo is widely known for its use of turtle graphics, in which commands for movement and drawing produced line or vector graphics, either on screen or with a small robot termed a turtle. The language was conceived to teach concepts of programming related to Lisp and only later to enable what Papert called "body-syntonic reasoning", where students could understand, predict, and reason about the turtle's motion by imagining what they would do if they were the turtle.

We will be using a java class called Turtle, created to mimic the controls originated in Logo, but functional in a Java environment.

Getting Started

In order to use the Turtle functionality in a program, the Turtle.java file needs to be downloaded and saved to your computer. This file, along with a link to the documentation, can be found in the Files/Links icon above. Click on the Turtle.java link and save the file to your computer.

Next, that file will need to be added to your working environment. So, in your current project or folder, add the Turtle.java file by uploading or copying the file.

Creating a Turtle

Before drawing, we must create a Turtle object in our program. This is done inside the main method of the file.
Turtle turtle = new Turtle();
In the line of code above, the turtle reference is a variable name and can be replaced with any valid name. The Turtle instances are referring to the file we downloaded and must remain as written. This line of code will automatically create a canvas for drawing that is 500x500. The turtle will be placed in the center of the canvas (denoted as 0,0) facing east (denoted as 0 deg).

Pen Basics

In Turtle graphics, the turtle representas a pen. The pen can be up or down. When up, the pen can move without leaving a trail. Movement when the pen is down leaves a trail and draws. In this way, the turtle acts much like a real life pen on a piece of paper.

To control the drawing capability of the pen, we call the up() and down() methods from the turtle variable.
turtle.up(); //lifts the pen to avoid drawing
turtle.down(); //lowers the pen for drawing

Basic Turtle Actions

The basic movements in Turtle graphics are to move forward, backward and to turn.
Position Actions
turtle.forward(100);
This code will move the turtle forward, in the direction that it is facing for 100 pixels. The value of '100' can be replaced with any integer or decimal value. This is relative movement, based on where the turtle was when the command is executed.
turtle.backward(50);
This code will move the turtle backward, in the opposite direction that it is facing for 50 pixels. The value of '50' can be replaced with any integer or decimal value. This is relative movement, based on where the turtle was when the command is executed.
turtle.setPosition(100, -50);
This code will move the turtle 100 pixels to the left of the center and 50 pixels down. The values of 100 and -50 can be replaced with any integer or decimal value. This is absolute movement as the current location and direction have no effect on the execution of this command.
Direction Actions
turtle.left(30);
This code will turn the turtle, in a counter-clockwise direction, 30 degrees. The value of '30' can be replaced with any integer or decimal value. This is relative movement, based on where the turtle was facing when the command is executed.
turtle.right(90);
This code will turn the turtle, in a clockwise direction, 90 degrees. The value of '90' can be replaced with any integer or decimal value. This is relative movement, based on where the turtle was facing when the command is executed.
turtle.setDirection(90);
This code will set the turtle to face 90 degrees (North). The value of 90 can be replaced with any integer or decimal value. This is absolute movement as the current direction has no effect on the execution of this command.
turtle.face(25, 30);
This code will turn the direction of the turtle to face the point that is 25px to the left and 30px up from the origin. The values of 25 and 30 can be replaced with any integer or decimal value. This is absolute movement as the current direction has no effect on the execution of this command.
Resetting the Turtle
turtle.home();
This code returns the turtle to (0,0) facing east. This is absolute movement as the current direction has no effect on the execution of this command.

Drawing Attributes

These functions will change the appearance of the lines drawn by the turtle.
turtle.width(15);
This code will set the width of the trail (drawing) left by the turtle in pixels. The default size is 1.
turtle.penColor("red");
This code will set the color of the trail (drawing) left by the turtle. The default color is black. A list of possible color values can be found starting in line 239 of the Turtle.java file. Most basic colors can be used without reference. Colors should be written in double quotes.