• Blog
  • Pictures
  • Projects
  • Cocoa
Software > Tiny Java Logo

Tiny Java Logo

the “Logo” programming language in a Java applet

This page contains a Java applet. If you're reading this now, then either your browser does not support Java applets or you have Java applets disabled.

Overview

A long, long time ago, when the Java 1.0 SDK was let loose upon the world, I wrote this simple Java applet that implements the graphical parts of the Logo programming language. The text entry field below the black graphics area is where you type in commands. Instructions are provided below.

I haven't done any work on Tiny Java Logo for years, but keep it on my site since people seem to get some fun out of playing with it and occasionally ask for the source code.

Download the Source Code

The source code for Tiny Java Logo is available, with one large disclaimer: It's a toy project, written many years ago to a surely now outdated API (does anyone even use AWT anymore?), by a programmer who was just learning Java and hasn't touched the project since. Students of Java looking for a how-to example be warned: this most likely is not a shining example of good Java style or best usage of modern language and class library features.

That said, have at it, and enjoy!

TinyJavaLogo.java - 16k

Examples

Here are some basic sample programs to give you ideas if you're not familiar with Logo. (Remember, all the commands have convenient abbreviations.)

This program draws an equilateral triangle 40 units long on a side:

repeat 3 [forward 40 right 120]

This program teaches Logo how to draw a triangle, then uses the triangle definition to draw an interesting pattern:

to triangle repeat 3 [forward 40 right 120] repeat 20 [triangle right 18 forward 5]

This series of commands illustrates referencing a definition within a definition:

to arc repeat 9 [forward 5 right 10] arc clearscreen to star repeat 4 [arc left 180] star clearscreen to design repeat 8 [star right 45] design

Commands

The following commands are supported. Most commands have a two- or three-letter abbreviation you can use, shown in parentheses. All distances are in pixel units; all angles are in degrees; floating-point numbers are supported for both distances and angles.

forward back left right setpc penup pendown hideturtle showturtle clearscreen repeat to

forward (fd)

Syntax: forward distance
Example: forward 100
Tells the turtle to move forward the specified distance.

back (bk)

Syntax: back distance
Example: back 50
Tells the turtle to move backward the specified distance.

left (lt)

Syntax: left angle
Example: left 45
Tells the turtle to turn left by the specified angle. (The angle is given in degrees.)

right (rt)

Syntax: right angle
Example: right 90
Tells the turtle to turn right by the specified angle. (The angle is given in degrees.)

setpc (pc)

Syntax: setpc color-name
Example: setpc blue
Changes the current "pen" color to the color identified by color-name. Subsequent turtle trails will appear in this color. Tiny Java Logo recognizes the following common color names: blue, cyan, darkgray, gray, green, lightgray, magenta, orange, pink, red, white, yellow

penup (pu)

Syntax: penup
Tells the turtle to raise the pen. Subsequent movement commands will move the turtle without leaving a visible trail.

pendown (pd)

Syntax: pendown
Tells the turtle to lower the pen. Subsequent movement commands will cause the turtle to leave a visible trail.

hideturtle (ht)

Syntax: hideturtle
Makes the turtle invisible. It will otherwise behave normally. Hiding the turtle before performing a long series of movements speeds up drawing, because then Logo doesn't have to redraw the turtle after each movement instruction.
Logo maintains an internal "hide level" count for the turtle. Whenever you issue the hideturtle command, this count is incremented. The showturtle command decrements this count and makes the turtle visible only if the count has returned to zero. The resulting effect is that the turtle is basically only visible when you've issued the showturtle command as many times as you've issued the hideturtle command. This feature helps support nesting in command definitions.

showturtle (st)

Syntax: showturtle
Decrements the "turtle hide level" count, and makes the turtle visible again if appropriate. See the description of the hideturtle command for an explanation of the how turtle hide level works.

clearscreen (cs)

Syntax: clearscreen
Clears the graphics window, returns the turtle to its starting position and direction, and makes the turtle visible. Logo will still remember all the commands you've defined using the to command.
Because Tiny Java Logo is a modern event-driven program that needs to be able to repaint its window on demand, it has to keep a record of every line segment you've drawn on the screen. Therefore, using clearscreen to clear the graphics window not only cleans up visual clutter in your drawing area, but tells Tiny Java Logo that it can forget all previously drawn lines. This will improve performance if there are so many lines in the window that your Java-enabled browser (e.g. Netscape) is having to swap to disk frequently.

repeat (rep)

Syntax: repeat n [ commands ]
Example: repeat 4 [forward 30 right 90]
Tells Logo to repeat one or more commands n times. The square brackets are currently required, even if commands is really just a single command.

Logo can only draw shapes made of straight line segments, but you can use the repeat command to approximate circular arcs using short line segments with small turns in between. For example:

repeat 9 [forward 5 right 10]

draws a quarter-circle. (To make it draw a complete circle, change the repeat count to 36.)

to

Syntax: to command-name commands
Example: to square repeat 4 [forward 30 right 90]
Tells Logo to remember the given sequence of commands, assigning them the name command-name. You can subsequently instruct Logo to execute the command sequence by typing in command-name anywhere you would otherwise type in the commands.

About Logo

Logo is an elegantly simple programming language that began life as one of a number of ideas Seymour Papert of MIT had in the 1960s for games children could play to learn about computers — provided computers ever became so widely available that children could have access to them! Logo is primarily a graphics language, and the interpreter I've implemented here supports only Logo's graphics commands. (Some other versions of Logo also provide support for evaluating expressions, executing statements conditionally, looping, and doing console I/O.)

The main purpose of Logo is to draw pretty pictures while learning something about basic geometry. To draw pictures, you instruct a "turtle" (represented by a triangle on the screen) to move forward and back, and turn left and right by different angles. As the turtle moves, it leaves a visible trail of straight line segments behind it. (In the original implementation of Logo at MIT, the "turtle" was a computer-controlled robot that received its instructions through a tether and drew on paper with a pen!)

The turtle understands a few other commands as well. penup tells the turtle to "raise its pen", so that subsequent movement commands only move the turtle and don't draw any lines on the screen. pendown tells the turtle to lower its pen so drawing can resume. hideturtle makes the turtle invisible, and showturtle makes it visible again. (If you hide the turtle before having it run a long series of commands, the drawing happens faster because the interpreter doesn't have to erase and redraw the image of the turtle before and after each instruction.)

There's also a repeat command that lets you repeat a command or series of commands any number of times. For example, to get the turtle to draw a square with sides of length 60, you could type:

repeat 4 [forward 60 right 90]

You can change the current pen color using the setpc command. Tiny Java Logo recognizes a dozen common color names.

After you've been drawing for a while, the graphics window can start to get cluttered. You can use the clearscreen command when you want Logo to clean up. This erases everything in the graphics window, returns the turtle to its starting position, and makes the turtle visible.

Because Tiny Java Logo is a modern event-driven program that needs to be able to repaint its window on demand, it has to keep a record of every line segment you've drawn on the screen. Therefore, using clearscreen to clear the graphics window not only cleans up visual clutter in your drawing area, but tells Tiny Java Logo that it can forget all previously drawn lines. This will improve performance if there are so many lines in the window that your Java-enabled Web browser is having to swap to disk frequently.

When the interpreter first starts, the turtle only "knows" how to perform the simple operations described above. It doesn't know how to draw circles, triangles, squares, etc. However, you can teach it to draw these figures using the to command. For instance:

to square repeat 4 [forward 30 right 90]

Once you've taught the turtle how to draw a given shape, you can instruct it to draw the shape by simply typing in the name you gave the shape. For example:

repeat 20 [square right 18 forward 10]

draws a pretty pattern made of squares, using the definition of square given above. (More generally, you can use the to command to give a name to any series of Logo commands you want to save yourself from typing repeatedly.) Since Tiny Java Logo doesn't yet support command definitions that take numerical parameters, a given command must always draw a shape with a fixed size. However, command names can include numerical digits in all places but the first character, so you can do something like:

to square20 repeat 4 [forward 20 right 90] to square40 repeat 4 [forward 40 right 90]

as an inelegant but useful kludge.

Logo can only draw shapes made out of straight line segments, but you can approximate circular arcs by having Logo draw series of short line segments with small turns in between (see example).

© 2008 Troy N. Stephens
xhtml · css