






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Friday, December 1, 2006 TurtleWorld graphics 22- 2
TurtleWorld graphics 22- 3 A world sophisticate enough to spiral public class SpiralWorld extends TurtleWorld { public void run() { SpiralMaker spiro = new SpiralMaker(); spiro.spiral(4, 90, 0, 3) } } class SpiralMaker extends Turtle { public void spiral(int steps, int angle, int length, int increment) { if (steps > 0) { fd(length); lt(angle); spiral(steps- 1 , angle, length+increment, increment); } } }
TurtleWorld graphics 22- 4 Or grow trees public class TreeWorld extends TurtleWorld { public void run() { TreeMaker tina = new TreeMaker(); tina.tree(4, 100, 45, 0.6); } } public class TreeMaker extends Turtle { public void tree(int levels, double length, double angle, double shrink) { if (levels > 0) { fd(length); rt(angle); tree(levels- 1 , lengthshrink, angle, shrink); lt(2angle); tree(levels- 1 , length*shrink, angle, shrink); rt(angle); bd(length); } } }
TurtleWorld graphics 22- 7 First, Turtle’s box o TurtleWorld is an Applet whose contract contains one method:
o We use a Graphics object, to draw the Turtle’s art. x y
TurtleWorld graphics 22- 8 TurtleWorld import javax.swing.; import java.awt.; public class TurtleWorld extends JApplet { private static Container canvas; public void init() { canvas = getContentPane(); } public void paint (Graphics g) { this.run(); } public void run() { // Users will override this method } public static Graphics getTurtleGraphics() { return canvas.getGraphics(); } public static int getCanvasHeight() { return canvas.getHeight(); } … }
TurtleWorld graphics 22- 9
A SpiralWorld object public class SpiralWorld extends TurtleWorld { public void run() { SpiralMaker spiro = new SpiralMaker(); spiro.spiral( 4 , 90 , 0 , 3 ) } } class SpiralMaker extends Turtle { public void spiral(int steps, int angle, int length, int increment) { if (steps > 0 ) { fd(length); lt(angle); spiral(steps-1, angle, length+increment, increment); } } } TurtleWorld graphics 22- 10 Now for the main event
TurtleWorld graphics 22- 13 Birth of a Turtle
TurtleWorld graphics 22- 14 Instance methods*
*How do you decide what these should be?
TurtleWorld graphics 22- 15 A few methods to implement public class Turtle { // Instance variables and constructor methods public void fd(double dist) { … } public void bd(double dist) { … } public void lt (double degrees) { … } public void rt(double angle) { … } public void pu() { … } public void pd() { … } } TurtleWorld graphics 22- 16 fd(double dist) public void fd(double dist) { double rads = degreesToRadians(this.heading); this.moveTo(x + distMath.cos(rads), y + distMath.sin(rads)); } (x, y) y x dist rads
TurtleWorld graphics 22- 19 The rest are a piece of cake public class Turtle { // Instance variables and constructor methods // fd() method as above public void bd(double dist) { } public void lt (double degrees) { } public void rt(double angle) { } public void pu() { } public void pd() { } } TurtleWorld graphics 22- 20 There is a problem public class SpiralWorld extends TurtleWorld { public void run() { SpiralMaker spiro = new SpiralMaker(); spiro.spiral( 4 , 90 , 0 , 3 ) } } class SpiralMaker extends Turtle { public void spiral(int steps, int angle, int length, int increment) { if (steps > 0 ) { fd(length); lt(angle); spiral(steps-1, angle, length+increment, increment); } } }
TurtleWorld graphics 22- 21 Overloading methods public class Turtle { // Instance variables and constructor methods // fd() method as above // Backward public void bd(double dist) { this.fd(- dist); } public void bd (int dist) { bd((double) dist); } // Left public void lt(double degrees) { this.heading = heading + degrees; } public void lt (int degrees) { lt((double) degrees); } }