Transcript Slide 1

Turtle Graphics
 Turtle graphics provide a simple way to draw
pictures in a window.
 The name suggests the way in which we can
think about the drawing process.
 Imagine a turtle walking around a screen with a
pen tied to it’s tail. Commands tell it to move
and to put the pen up and down (i.e. put it’s tail
up and down.)
 This is a primitive form of graphics but gives us a
good introduction to graphic concepts.
Example TurtleGraphics
Program
import TurtleGraphics.StandardPen;
public class DrawSquare
{
public static void main(String [ ] args)
{
StandardPen pen = new StandardPen();
pen.up();
pen.move(25);
pen.turn(90);
pen.move(25);
pen.down();
pen.turn(90); pen.move(50);
pen.turn(90); pen.move(50);
pen.turn(90); pen.move(50);
pen.turn(90); pen.move(50);
}
}
Example Program Explanation
import TurtleGraphics.StandardPen;
 Imports the TurtleGraphics package which
allows us to draw basic shapes.
 StandardPen is the class we will be using.
Example Program Explanation
pen.up( );
pen.move(25);
pen.turn(90);
pen.move(25);
pen.down( );
 These statements lift the pen, move it to
the square’s top left corner, and lower to
set it to draw.
Example Program Explanation
pen.turn(90); pen.move(50);
pen.turn(90); pen.move(50);
pen.turn(90); pen.move(50);
pen.turn(90); pen.move(50);
 These statements draw a square with side
length of 50 pixels. Each turn forms the
90° turn at the corners.
Turtle Graphics Pen Messages
Pen Message
What it Does
home()
The pen jumps to the center of the graphics window
without drawing and points north.
setDirection(degrees)
The pen points in the indicated directions. Due east
corresponds to 0 degrees, north to 90, west to 180
and south to 270.
turn(degrees)
The pen adds the indicated degrees to its current
direction. Positive degrees correspond to turning
counterclockwise. The degrees can be integer or
floating point.
down()
The pen lowers itself to the drawing surface
up()
The pen raises itself from the drawing surface
move(distance)
The pen moves the specified distance in the current
direction. The distance can be an integer or floatingpoint number and is measured in pixels.
Misc. Statements on Turtle
Graphics
 When the StandardPen is instantiated, it
is always:
 In the center of the graphics window
 In the down position
 Pointing North.
Additional TurtleGraphics Features
Pen Message
What it Does
setColor(Color. color)
Sets the pen’s color to the specified color.
import java.awt.Color;
setWidth(width as int.)
Sets the pen’s width to the specified width. Must be an
integer.
move(double x,double y)
Moves the pen to the specified location relative to
home(0, 0)
TurtleGraphics Color Constants
 The Color class has a number of different colors
available.
 import java.awt.Color;
•
•
•
•
•
•
•
•
•
•
•
•
red
yellow
blue
orange
pink
cyan
magenta
black
white
gray
light gray
dark gray
Setting color and width of pen
Example
//imports needed packages
import TurtleGraphics.StandardPen;
import java.awt.Color;
public class DrawSquareColor //Start of class
{
public static void main(String [] args) //Start of Main method
{
StandardPen pen = new StandardPen(); //Instantiates a pen object
pen.setColor(Color.red);
pen.setWidth(3);
pen.turn(90); pen.move(50);
pen.setColor(Color.magenta);
pen.setWidth(6);
pen.move(50);
}
}