Turtlegraphics with Java
HomeAufgabenDruckenJava-Online

Command-overview and key words


The most important methods in the class Turtle:

Method Action
forward(distance) moves the Turtle forward
back(distance) moves the Turtle backward
left(degrees) rotates the Turtle left
right(degrees) rotates the Turtle right
hideTurtle() makes the Turtle invisible (moves and draws faster)
showTurtle() makes the Turtle visible
speed(number) sets the speed of the Turtle
penUp() lifts the pen (the Turtle moves without drawing)
penDown() puts the pen down (the Turtle moves and draws)
setColor(Color color) sets the Turtle's color
setPenColor(Color color) sets the Turtle's drawing color
penErase() erases drawn lines
fill() fills a restricted figure
setFillColor(Color color) sets the filling color
home() moves the Turtle back to its starting position
setPos(x, y) moves the Turtle to the position with the coordinates (x, y)
getPos() returns the current position of the Turtle
distance(x, y) returns the distance from the current Turtle position to the position (x, y)
clean() clears all drawings
label(text) writes a text at the current position of the Turtle
setFont(Font font) sets the font style
setFontSize(size) sets the font size

A complete description of the Turtle methods can be found at Turtle documentation (Turtle API)


Colors

Each program that uses colors must have the following import-line:

import java.awt.Color;

Default colors can be set by writing "Color." and the color's name, e.g. Color.blue, Color.pink etc. Setting the Turtle's color can be set as in the following example:
joe.setColor(Color.red);

All other colors can be defined by mixing different shares of red, green and blue. The share is defined by a number between 0 and 255. E.g.:
Color c = new Color(120, 200, 80);
joe.setColor(c);

A randomly mixed color can be defined as follows:
Color c = new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255));
joe.setColor(c);


Reserved words and key words in Java

abstract Double int strictfp
assert else interface super
boolean extends long switch
break false native synchronized
byte final new this
case finally null throw
catch float package throws
char for private transient
class goto protected true
const if public try
continue implements return void
default import short volantile
do instanceof static while

These words can not be used as variable names!

 

Modifiers

The access-modifier public, protected and private are key words which are placed before a declaration to set the visibility (access right) of an instance variable, method or class.
private visible only in its own class
public visible everywhere
protected visible in the same package and in the subclasses

If the modifier is missing, the default access right is protected.

If one wants to write in a good programming style it is important to deliberately use the modifiers and so limit the access rights as far as possible. Especially instance variables are strongly limited and mostly declared as private.