Turtle Graphics Commands


The package provides classes and methods for manipulating a pen in a graphics window. The pen or turtle is an invisible device, initially positioned at the center of a sketchpad. This center point, also called home, is at the origin of a Cartesian coordinate system. The programmer draws images with the pen by sending it messages. There are several types of pens. All implement the Pen interface. We begin with a StandardPen which has a default color of blue, is initially positioned at home, facing north, and is down on the pen's surface. However, the following different types of pens are available and all use the commands listed below.

The Pen interface specifies the methods that all of the types of pens support. A pen is initially positioned at the home position (the origin or center of the sketchpad), faces due north, and is placed down on the sketchpad's surface. A nonrainbow pen's default color is blue. When placed down and moved, the pen draws; when picked up and moved, the pen simply moves without drawing.


The following messages may be sent to a pen object. These are the methods that are listed in the Pen interface.

 

Here is some sample code for TurtleGraphics programs:

pen. home( );        // pen jumps to center of sketch pad without drawing and points north

pen.setDirection(90);       // Make the pen point due north

pen.turn(-45);        // Rotate the pen 45 degrees clockwise Appendix

pen.down( );          // lowers the pen to the drawing surface.

pen.up( );          // raises the pen off the drawing surface.

pen.move(100);       // moves the pen 100 pixels in the current direction

pen.move(50, 50);       // moves the pen to the point (50, 50) of the sketch pad. (0, 0) is the center of the sketch pad.

pen.drawString ( "Draw 16 Square ");       // Draws the string at the pen's current position in the current color.

pen.setColor(Color.red);       // Sets the pen's color to the specified color.

pen.setwidth(8);        // Sets the pen's width to the specified width.

 

You can also customize the size the a sketchpad window that a pen is associated with. Here is the code to do so:

SketchPadWindow s = new SketchPadWindow(600, 600)); // creates a SketchPadWindow object 600 pixels by 600 pixels refered to by the variable s

StandardPen pen = new StandardPen(s); // creates a StandardPen object referred to by the variable pen for the SketchPadWindow referred to by s


Additional Information on TurtleGraphics programming is available in Appendix I ... pages I1 - I5. (eye1 thru eye5)


Back to Top