Presentation is loading. Please wait.

Presentation is loading. Please wait.

More methods, more capabilities

Similar presentations


Presentation on theme: "More methods, more capabilities"— Presentation transcript:

1 More methods, more capabilities
Turtles More methods, more capabilities Copyright © Curt Hill

2 Introduction Basics were covered last time but there is more
Signatures Coordinates Constructors Alternate methods Pen motion Colors Copyright © Curt Hill

3 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 © Curt Hill

4 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 © Curt Hill

5 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 © Curt Hill

6 Default World Orientation
(0,0) (640,0) (0,480) (640,480) Copyright © Curt Hill

7 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 © Curt Hill

8 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 © Curt Hill

9 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 © Curt Hill

10 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 © Curt Hill

11 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 © Curt Hill

12 Example Copyright © Curt Hill

13 Commentary The last screen demonstrated A different sized world
Three parameter Turtle constructor forward with no parameters moveTo changing position but not heading Copyright © Curt Hill

14 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 © Curt Hill

15 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 © Curt Hill

16 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 © Curt Hill

17 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 © Curt Hill

18 Example Copyright © Curt Hill

19 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 © Curt Hill

20 Color Constructors Color(int red,int green, int blue);
All integers in 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 is bright/opaque Copyright © Curt Hill

21 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 © Curt Hill

22 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 © Curt Hill

23 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 © Curt Hill

24 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 © Curt Hill

25 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 © Curt Hill

26 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 © Curt Hill

27 What it looks like Copyright © Curt Hill

28 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 © Curt Hill

29 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 © Curt Hill

30 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 © Curt Hill

31 Result of Example Code Copyright © Curt Hill

32 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 © Curt Hill

33 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 © Curt Hill


Download ppt "More methods, more capabilities"

Similar presentations


Ads by Google