140 likes | 290 Vues
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.
E N D
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 • Imports the TurtleGraphics package which allows us to draw basic shapes. • StandardPen is the class we will be using. import TurtleGraphics.StandardPen;
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.
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.
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