Similar to the Etch-a-Sketch toy of the 1950s, Turtle Graphics is a desktop application that allows users to command a virtual turtle to move around a virtual canvas. If the turtle's pen is down, then he leaves a colored path in his wake.
The application panel contains two sub-panels. On the left is the control panel. On the right is the view panel. The view panel shows the canvas, the turtle, and the polylines it has drawn.
The control panel contains seven buttons and a text field.
· The Pen button toggles the pen up (don't draw) and down (draw). This changes the head of the turtle from filled-in to empty.
· The Clear button erases the drawing.
· The North, East, West, and South buttons change the heading of the turtle (indicated by which side of the shell the head is on).
· The Color button allows the user to set the color the turtle's pen (indicated by the color of the head).
Typing a number into the text field moves the turtle the specified number of steps in his current direction. If the user enters something inappropriate a dialog box appears containing an error message:
· A "turtle" has
o a location (a point on the canvas),
o a heading (NORTH, SOUTH, EAST, WEST)
o a path (a list of stopping points),
o a color (the color to make lines),
o a pen flag (true if pen is down, else false).
· A point has
o integer x and y coordinates,
o a color,
o a flag indicating if its the end of a polyline drawn by the turtle.
Here's a simplified UML class diagram showing the design of Turtle Graphics:
Notes:
· The design of Turtle Graphics follows the Model-View-Controller design pattern, with the turtle playing the role of the model, the canvas view playing the role of the view, and the app panel playing the role of the controller.
· AppPanel contains a static main method. When called, it creates an AppPanel. The AppPanel constructor creates the turtle and sub-panels then sets its visibility to true which makes it appear on the desktop.
· The location of the turtle is a single point. Its path is a list of points. These are the points where the turtle stopped.
· There are no arrows pointing from the turtle back to the user interface (i.e., the panels). This is good. It means we can potentially reuse the Turtle class in other applications with different user interfaces. We use the Publisher-Subscriber Pattern to notify the user interface when the turtle changes any of its properties:
The same Publisher-Subscriber Pattern is used by Swing. User interface controls (buttons, text fields, menu items, etc.) are publishers. They publish messages called action events when activated by the user (i.e., clicked, selected, etc.). User interface components (e.g. panels) that implement the ActionListener interface may subscribe to these controls.
Notes:
· JPanel is a special Swing component that can contain other components such as controls and sub-panels.
The TurtleShape class has a draw method:
public void draw(Graphics2D gc) {...}
It draws the shell and head of the turtle in an invisible bounding box.
The bounding box, made visible in this test viewer, leaves room on all four sides of the shell for the head. In my implementation the head is one fourth the width of the bounding box.
Version 2.0 adds File, Edit, and Help menus:
· The File menu contains the options New, Save, SaveAs, Open, and Quit.
· Save, SaveAs, and Open prompt the user for a file name using the File Chooser Dialog:
· Save remembers the file name entered by the user so that it doesn't need to be entered each time.
· Open and Quit warn the user of unsaved changes in the current turtle.
The Help menu contains the items Help and About.
· About should display an information dialog containing your name and the date.
· The Help item should display an information dialog with one line of explanation for each command on the Edit menu.
· The Edit menu duplicates the buttons. The exception is the "steps" command, which must first prompt the user for the number of steps to take:
·
The name on a button or the name of a menu item
is the name of the command it includes with the action event it generates. This means that you don't need separate handlers for the buttons and menu
items:
void actionPerformed(ActionEvent
e) {
String cmmd = e.getActionCommand();
if
(cmmd == "North")
{
// Edit.North and North button
handled here
}
// etc.
}
· Turtles will need to be serializable as well as any points they contain.
· Here's how to work the color chooser:
Color newColor = JColorChooser.showDialog(null, "Choose a color", turtle.getColor());
· Java's graphics coordinate system is weird. The positive x-axis runs across the top of the canvas, and the positive y-axis runs down the left side of the canvas. This means that as the turtle moves south, his y-coordinate increases.
· The turtle never leaves the canvas. Instead, he wraps around to the other side.
· I included the dimensions of the turtle's world/canvas as a static field inside World class:
class World {
public static Integer SIZE = 250;
// more global constants can go here
}
· Much of the AppPanel.java from the Stoplight example is reusable.
· You will be graded on the design of your code. Ask yourself how much of your code could be reused in another application.
I recommend having two packages in your project: tg (turtle graphics) and tools (reusable tools needed by tg and future projects. Currently, my tools package contains the following files:
· Utilities.java : static methods for creating dialogs and menus.
· SafeFrame.java : A frame for the app panel that warns users when the close button is clicked.
1. Add Subscriber.java and Publisher.java to your tools package. Here’s a test driver:
2. After completing Turtle.java try implementing TurtleShape.java. Here’s a test driver:
Implement Turtle Graphics. Submit your source code in a zip file.