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:
| Command | Operand | Meaning |
| 1 | blank | Pen up |
| 2 | blank | Pen down |
| 3 | blank | Turn right |
| 4 | blank | Turn left |
| 5 | n | Move forward n spaces (for 1 <= n ) |
| 6 | blank | Paint the 20 X 20 array |
| 7 | blank | Clear the floor array (re-initialize to zeros) |
- As the turtle moves with the pen down, set the appropriate elements of the floor array to 1. When the 6 command
(display on the applet) is given, wherever there is a 1 in the floor array, fillRect that square with a random
color. Wherever there is a 0, fillRect with black.
- The Move command (5) is implemented with wrap-around: If your turtle moves off the floor array on one edge, it
comes back onto the array on the opposite edge. For example, if your turtle were in position 1, 15 facing south,
and executed a 5 command with an 11 operand, then the turtle would move to vertical coordinate (15 + 11) % 20,
which is 6: Its new position would be 1,6. Be sure your applet implements wrap-around in all 4 directions.
- You do not need to check either of the TextFields to ensure that they are integers.
- When processing a Move command (5), you may assume that the Operand is a positive integer.
- When processing all other commands, you may ignore the Operand TextField.
- If the contents of the Command TextField is an integer, but not a valid command, display an error message in the
status bar, without changing the state of the system (don't change the position of the turtle or the pen).
- Although not part of what you deliver, you should write several turtle graphics programs that draw interesting
shapes. Most importantly, thoroughly test your applet to exercise every feature of the turtle graphics specification
above. To simplify testing, add features incrementally, and test each one before adding the next. Your display
command (6) should be one of the first that you test.
- Suppose that the turtle is somewhere near the center of the floor. The following "program" would draw and
display a 12 X 12 square, leaving the pen in the up position:
| Command | Operand |
| 2 | blank |
| 5 | 12 |
| 3 | blank |
| 5 | 12 |
| 3 | blank |
| 5 | 12 |
| 3 | blank |
| 5 | 12 |
| 1 | blank |
| 6 | blank |
Optional Challenge
- Enhance your applet so that it allows negative operands.
- Add other commands to increase the power of your turtle graphics language.
Turn In Procedure
- Email your TurtleGraphics.java file to cs10@cs.ucsb.edu. Emailing to the wrong address is not an acceptable
reason for a late turn-in. Be sure to email your file while logged into your own account. To test your applet, we
will use an applet whose width and height is 400.
- Submit a hard copy of your TurtleGraphics.java file to the CS10 homework box (on the 2nd floor of Engineering I,
next to the Computer Science Departmental office).