CIT 594 GUI for Logo Interpreter
Spring 2009, David Matuszek


I assume that you already know how to program a GUI. Since that isn't what CIT 594 is all about, I'm providing some GUI code for your use in the Interpreter project. It looks something like this (I shrank the window a bit):


The left panel is a display area; this is where things are drawn. The right panel contains the Logo program that does the drawing. In this example, the program draws a multicolored star.

The File menu contains the usual Load, Save, and Save As... commands. The buttons are as follows:

The TurtleCommand interface

One thing you must understand about Java GUIs is that they have no "memory." If, for example, a window is moved, or partially obscured and then uncovered, it is up to the program to keep track of what was in the window, and redraw everything. To do this, the GUI must keep a list of "commands." In the supplied code, this list is maintained in the DrawingArea class.

A "command" is any object that implements the TurtleCommand interface. This interface specifies one method, public void execute(Graphics g). Classes that implement this interface typically contain additional information.

For example, a DrawStringCommand is created with three pieces of information: the x and y location at which the String is to be drawn, and the text of the String. When the execute message is sent to a DrawStringCommand object, its execute method draws the text of the String at the specified x, y location on the Graphics object g.

Not all Logo "commands" are saved as TurtleCommand objects. Only those commands that must be remembered by the DrawingArea, in order to reproduce a drawing, are saved as TurtleCommands.

The Turtle class

In Logo, we pretend that the drawing is done by a "Turtle." As the Turtle moves, it draws a line behind it, in the current color. In our GUI, the turtle is represented by the small, light gray triangle.

In reality, the actual drawing is done by the public void paint(Graphics g) method in the DrawingArea class. This method loops through all the stored TurtleCommands and calls the execute method for each. After all the stored TurtleCommands have been executed, the paint method draws the "turtle."

The Turtle object does have responsibilities, however. When the Interpreter interprets a command that directly affects the drawing, it tells the Turtle to perform that action. The Turtle then creates an appropriate TurtleCommand, which it passes to the DrawingArea. It also maintains its own state, which consists of its x, y location, the direction in which it is facing, whether its "pen" is up or down, and whether it is paused. Finally, since the Turtle can be painted by the paint method in the DrawingArea class, it has to make its state available to that class.

The DrawingArea class

The DrawingArea is a kind of JPanel. JPanels are what you would typically use in Swing in order to draw things.

The DrawingArea object keeps a list of TurtleCommand objects that have been sent to it by the Turtle. When its paint(Graphics g) method is called, it draws all those objects (by calling their execute() methods), and finishes off by drawing the Turtle.