More methods, more capabilities Turtles More methods, more capabilities Copyright © 1997-2010 Curt Hill
Introduction Basics were covered last time but there is more Signatures Coordinates Constructors Alternate methods Pen motion Colors Copyright © 1997-2010 Curt Hill
Signatures A method signature consists of: Name of the method Number and type of the parameters In most previous languages it was the name only In Java two methods may have same name but must have different signatures This is known as overloading Copyright © 1997-2010 Curt Hill
Example The Turtle method forward has two signatures: forward(int k); Move forward k pixels forward(); Move forward 100 pixels Thus: Sam.forward(100); // and Sam.forward(); Do exactly the same thing Copyright © 1997-2010 Curt Hill
Coordinate Systems The drawing surface is different than the coordinates used in algebra The upper left corner is point (0,0) The X coordinate gets larger as we go right The Y gets larger as we go down The default World is 640 pixels wide (x value) and 480 pixels high (y value) Copyright © 1997-2010 Curt Hill
Default World Orientation (0,0) (640,0) (0,480) (640,480) Copyright © 1997-2010 Curt Hill
World Sizes The default constructor for World gives a 640 by 480 size panel World w = new World(); There is another constructor that allows different sizing: World w2 = new World(1000, 700); This gives width of 1000 pixels and height of 700 Two constructors with two signatures Copyright © 1997-2010 Curt Hill
Turtle Constructors The first constructor only used the world The location defaulted to the center Second gives the location and then world Signature: Turtle(int x, int y, World w); Example: yertle = new Turtle(50,50,myW); Copyright © 1997-2010 Curt Hill
Movement If you can move forward why not backward? It has the same form: backward(int pixels) yertle.backward(50); backward() Moves 100 pixels backward Copyright © 1997-2010 Curt Hill
Relative Movement Forward and backward are relative Start at turtles location and move in the turtles current heading There is also a moveTo method Takes a target location and moves to that absolute location Does not change heading Ignores it Copyright © 1997-2010 Curt Hill
Example Consider the following code: World w = new World(500,300); Turtle t = new Turtle(80,150,w); t.turn(75); t.forward(); t.moveTo(200,200); t.forward(100); This would produce the picture in next screen Copyright © 1997-2010 Curt Hill
Example Copyright © 1997-2010 Curt Hill
Commentary The last screen demonstrated A different sized world Three parameter Turtle constructor forward with no parameters moveTo changing position but not heading Copyright © 1997-2010 Curt Hill
Pen Movement After construction the pen is down Thus any move leaves a trail Two commands: penUp() penDown() Like all methods the object must occur first: yertle.penUp(); yertle.forward(200); yertle.penDown(); Copyright © 1997-2010 Curt Hill
Hiding and displaying The hide() method will make the turtle invisible This will not affect the previously drawn lines or the status of the pen up or down The show() method will make the turtle visible Also does not affect lines or pen status Copyright © 1997-2010 Curt Hill
Turning The turn method is the most useful but there are others turnLeft() is the same as turn(-90) turnRight() is the same as turn(90); turnToFace(int x, int y) sets the heading so that the turtle is pointed at that location turnToFace(Turtle) sets the heading so that the turtle is pointed at a second turtle Copyright © 1997-2010 Curt Hill
Another Example The following code: World w = new World(500,300); Turtle t = new Turtle(100,150,w); t.turnRight(); t.forward(); t.penUp(); // Move but don't draw t.moveTo(200,200); t.penDown(); t.forward(100); Produces next screen Copyright © 1997-2010 Curt Hill
Example Copyright © 1997-2010 Curt Hill
Colors The color of the turtle and the pen may be changed This requires some knowledge of how Java handles colors Almost everything in Java is a class Color is a class as well A color is produced on a monitor or screen by making each pixel glow with certain red, green and blue values Copyright © 1997-2010 Curt Hill
Color Constructors Color(int red,int green, int blue); All integers in 0-255 range The larger the value the brighter 255,255,255 is white Color(int red, int green, int blue, int alpha); Alpha is a transparency indicator Used with multiple color planes 0 is black/invisible - 255 is bright/opaque Copyright © 1997-2010 Curt Hill
Color Constructors A constructor is always used following the new keyword Most times this is in an assignment But not usually when dealing with colors Example: new Color(255, 0, 0) This produces bright red Copyright © 1997-2010 Curt Hill
Color Constants As a convenience several constant colors are available black, blue, cyan, darkGray, gray, green, lightGray, etc These must be referred to with a Color class or just Color prefixed Color.cyan These may be all lower or all upper case Copyright © 1997-2010 Curt Hill
Imports The Color class requires an import statement import java.awt.Color; This is placed before the first public class Placed after the package statement, if there is one Copyright © 1997-2010 Curt Hill
Turtle colors The setColor method changes the turtle color and the pen color The parameter is either a color constant or a newly created special color: yertle.setColor(Color.RED); yertle.setColor(new Color(255, 128, 10)); Constants do not need the new The constructor does need the new Copyright © 1997-2010 Curt Hill
Example Notes The import statement is needed for colors There is both a constant and constructor color Use the constant ones for common colors Use the constructor for greater refinement Copyright © 1997-2010 Curt Hill
Example import java.awt.Color; public class TurtleExample2 { public static void main(String []a){ World w = new World(500,300); Turtle t = new Turtle(100,150,w); t.turnRight(); t.forward(); t.setColor(Color.RED); t.turn(90); t.setColor(new Color(128,64,255)); t.forward(100); } Copyright © 1997-2010 Curt Hill
What it looks like Copyright © 1997-2010 Curt Hill
More Color Commands The turtle icon does not have to match the pen color setPenColor(Color) will change the pen color without changing the turtle color setShellColor(Color) changes the turtle shell without changing the pen or feet color Copyright © 1997-2010 Curt Hill
Line Width So far our turtle has only drawn lines that are one pixel wide This is OK and maybe even what we want We can control the width with the setPenWidth(int) method It takes an integer as a parameter Example: yertle.setPenWidth(5) Copyright © 1997-2010 Curt Hill
Example World w = new World(500,300); Turtle t = new Turtle(100,150,w); t.turnRight(); t.forward(); t.setColor(Color.RED); t.setPenWidth(5); t.turn(-90); t.setColor(new Color(128,64,255)); t.forward(100); t.setPenColor(Color.yellow); Copyright © 1997-2010 Curt Hill
Result of Example Code Copyright © 1997-2010 Curt Hill
Bounds The turtle must stay within the bounds of the world If the turtle is pointed towards an edge and then moves forward it will exceed the edge The turtle will then just disappear Copyright © 1997-2010 Curt Hill
Summary We can now create worlds of various sizes and turtles starting in different positions The turtles can move with or without a trail We have several ways to move them or turn them We can make their trails and themselves any color we like Copyright © 1997-2010 Curt Hill