Assignment 6: Turtle Graphics

Assigned Tuesday, November 12; Due Tuesday, November 21, 5pm

The Problem

(This applet is a variation of exercise 5.21 in our text book.) The Logo language, which is popular among young computer users, made the concept of turtle graphics famous. Imagine a mechanical turtle that walks around the room under the control of a Java program. The turtle holds a pen in one of 2 positions: up or down. While the pen is down, the turtle traces out shapes as it moves; while the pen is up, the turtle moves about freely without writing anything. In this assignment, you will create an applet, called TurtleGraphics, that simulates the operation of the turtle, creating a computerized sketchpad as well.

Use a 20 X 20 int array, floor, which is initialized to zeros. Your applet has 2 TextFields, one labeled Command, and the other labeled Operand. You type commands into the Command TextField. Your applet's action method processes a new command only when the user presses Enter or Return while in the Command TextField. Processing includes erasing both TextFields after their data is retrieved.

When a 5 command is input (see below), your applet fetches its operand from the Operand TextField. Keep track of the the turtle's position at all times, and whether it is in the pen up or pen down state. Assume that the turtle always starts in the 0,0 position of the floor, facing south, with its pen up. The set of turtle commands your applet must process is:

CommandOperandMeaning
1blankPen up
2blankPen down
3blankTurn right
4blankTurn left
5nMove forward n spaces (for 1 <= n )
6blankPaint the 20 X 20 array
7blankClear the floor array (re-initialize to zeros)
CommandOperand
2blank
512
3blank
512
3blank
512
3blank
512
1blank
6blank

Optional Challenge


Turn In Procedure