turtle graphics turtle graphics provide a simple way to draw pictures in a window. the name...

9
Turtle Graphics Turtle Graphics Turtle graphics provide a simple way to Turtle graphics provide a simple way to draw pictures in a window. draw pictures in a window. The name suggests the way in which we can The name suggests the way in which we can think about the drawing process. think about the drawing process. Imagine a turtle walking around a screen Imagine a turtle walking around a screen with a pen tied to it’s tail. Commands with a pen tied to it’s tail. Commands tell it to move and to put the pen up and tell it to move and to put the pen up and down (i.e. put it’s tail up and down.) down (i.e. put it’s tail up and down.) This is a primitive form of graphics but This is a primitive form of graphics but gives us a good introduction to graphic gives us a good introduction to graphic concepts. concepts.

Upload: antony-evans

Post on 23-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 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

Turtle GraphicsTurtle Graphics

Turtle graphics provide a simple way to draw Turtle graphics provide a simple way to draw pictures in a window.pictures in a window.

The name suggests the way in which we can The name suggests the way in which we can think about the drawing process.think about the drawing process.

Imagine a turtle walking around a screen with a Imagine a turtle walking around a screen with a pen tied to it’s tail. Commands tell it to move 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 and to put the pen up and down (i.e. put it’s tail up and down.)up and down.)

This is a primitive form of graphics but gives us a This is a primitive form of graphics but gives us a good introduction to graphic concepts.good introduction to graphic concepts.

Page 2: 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

Example TurtleGraphics Example TurtleGraphics ProgramProgram

import TurtleGraphics.StandardPen;import TurtleGraphics.StandardPen;

public class DrawSquarepublic class DrawSquare{{

public static void main(String [ ] args)public static void main(String [ ] args){{

StandardPen pen = new StandardPen();StandardPen pen = new StandardPen();pen.up();pen.up();pen.move(25);pen.move(25);pen.turn(90);pen.turn(90);pen.move(25);pen.move(25);pen.down();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); pen.turn(90); pen.move(50); pen.turn(90); pen.move(50); pen.turn(90); pen.move(50); pen.turn(90); pen.move(50);

}}}}

Page 3: 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

Example Program ExplanationExample Program Explanation

Imports the TurtleGraphics package which Imports the TurtleGraphics package which allows us to draw basic shapes.allows us to draw basic shapes.

StandardPen is the class we will be using.StandardPen is the class we will be using.

import TurtleGraphics.StandardPen;

Page 4: 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

Example Program ExplanationExample Program Explanation

These statements lift the pen, move it to These statements lift the pen, move it to the square’s top left corner, and lower to the square’s top left corner, and lower to set it to draw.set it to draw.

pen.up();pen.move(25);pen.turn(90);pen.move(25);pen.down();

Page 5: 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

Example Program ExplanationExample Program Explanation

These statements draw a square with side These statements draw a square with side length of 50 pixels. Each turn forms the length of 50 pixels. Each turn forms the 9090° turn at the corners.° turn at the corners.

pen.turn(90); pen.move(50);pen.turn(90); pen.move(50); pen.turn(90); pen.move(50); pen.turn(90); pen.move(50);

Page 6: 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

Turtle Graphics Pen MessagesTurtle Graphics Pen MessagesPen MessagePen Message What it DoesWhat it Does

home()home() The pen jumps to the center of the graphics window The pen jumps to the center of the graphics window without drawing and points north.without drawing and points north.

setDirection(degrees)setDirection(degrees) The pen points in the indicated directions. Due east The pen points in the indicated directions. Due east corresponds to 0 degrees, north to 90, west to 180 corresponds to 0 degrees, north to 90, west to 180 and south to 270.and south to 270.

turn(degrees)turn(degrees) The pen adds the indicated degrees to its current The pen adds the indicated degrees to its current direction. Positive degrees correspond to turning direction. Positive degrees correspond to turning counterclockwise. The degrees can be integer or counterclockwise. The degrees can be integer or floating point.floating point.

down()down() The pen lowers itself to the drawing surfaceThe pen lowers itself to the drawing surface

up()up() The pen raises itself from the drawing surfaceThe pen raises itself from the drawing surface

move(distance)move(distance) The pen moves the specified distance in the current The pen moves the specified distance in the current direction. The distance can be an integer or floating-direction. The distance can be an integer or floating-point number and is measured in pixels.point number and is measured in pixels.

Page 7: 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

Misc. Statements on Turtle Misc. Statements on Turtle GraphicsGraphics

When the StandardPen is instantiated, it When the StandardPen is instantiated, it is always:is always: In the center of the graphics windowIn the center of the graphics window In the down positionIn the down position Pointing North.Pointing North.

Page 8: 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

Additional TurtleGraphics FeaturesAdditional TurtleGraphics Features

Pen MessagePen Message What it DoesWhat it Does

setColor(Color. setColor(Color. colorcolor)) Sets the pen’s color to the specified color.Sets the pen’s color to the specified color.import java.awt.Color;import java.awt.Color;

setWidth(setWidth(width as int.)width as int.) Sets the pen’s width to the specified width. Must be an Sets the pen’s width to the specified width. Must be an integer.integer.

move(double x,double y)move(double x,double y) The pen moves the specified distance in the current The pen moves the specified distance in the current direction. The distance can be an integer or floating-direction. The distance can be an integer or floating-point number and is measure in pixels. The size of the point number and is measure in pixels. The size of the pixel depends on the monitor’s resolution.pixel depends on the monitor’s resolution.

Page 9: 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

TurtleGraphics Color ConstantsTurtleGraphics Color Constants The Color class has a number of different colors The Color class has a number of different colors

available.available. import java.awt.Color;import java.awt.Color;

• redred• yellowyellow• blueblue• orangeorange• pinkpink• cyancyan• magentamagenta• blackblack• whitewhite• graygray• light graylight gray• dark graydark gray