CIT 594 GUI for Logo
Spring 2014, 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:
toString()method in your Tree class to produce the necessary string (but remember DRY).
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
A "command" is any object that implements the
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 (and previously saved) x, y location on the Graphics object
Not all Logo "commands" are saved as
Only those commands that must be remembered by the
in order to reproduce a drawing, are saved as
set(assignment) commands affect the drawing only indirectly, in that they control which drawing commands are issued. They are not
penupcommand prevents subsequent movement commands from being displayed (or remembered); the
pendowncommand causes subsequent movement commands to be displayed and remembered by the
forwardcommand draws a line (if the "pen" is "down"), so it is a
homecommands affect the way things are subsequently drawn, so they must be remembered by the
DrawingArea. They are
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,
paint method draws the "turtle."
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
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
class, it has to make its state available to that class.
DrawingArea is a kind of
JPanels are what you would typically use in Swing in order to
DrawingArea object keeps a list of
TurtleCommand objects that have been sent to it by the Turtle.
paint(Graphics g) method is called, it draws all those
objects (by calling their
execute() methods), and finishes off
by drawing the Turtle.