Docsity

Implementing Objects in Turtle Graphics: SpiralWorld and TreeWorld, Study notes of Computer Science

Toggle sidebar
bg1
1
CS111 Computer Programming
Department of Computer Science
Wellesley College
Turtle graphics
TurtleWorld as an object example
Friday, December 1, 2006
TurtleWorld graphics 22-2
TurtleWorld revisited
oBuggles’ smaller, more
carefree cousins return to
teach an object lesson.
oRecall that Turtles are born
centered, pointing EAST,
brushDown (red).
oIn this lecture, we design
and implement a world for
Turtles.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Implementing Objects in Turtle Graphics: SpiralWorld and TreeWorld and more Study notes Computer Science in PDF only on Docsity!

CS111 Computer Programming

Department of Computer Science

Wellesley College

Turtle graphics

TurtleWorld as an object example

Friday, December 1, 2006 TurtleWorld graphics 22- 2

TurtleWorld revisited

o Buggles’ smaller, more

carefree cousins return to

teach an object lesson.

o Recall that Turtles are born

centered, pointing EAST,

brushDown (red).

o In this lecture, we design

and implement a world for

Turtles.

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); } } }

Overrides the

run() method

in TurtleWorld

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); } } }

Also overrides

run() method

in TurtleWorld

TurtleWorld graphics 22- 7 First, Turtle’s box o TurtleWorld is an Applet whose contract contains one method:

public void run().

o We use a Graphics object, to draw the Turtle’s art. x y

400 X 400

Applet window

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

turtleGraphics

init()

paint()

run()

run()

TurtleWorld

SpiralWorld

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

import java.awt.*; // Import Abstract Window Toolkit

public class Turtle

// Instance variables?

// Constructor methods?

// Instance methods?

private or public?

TurtleWorld graphics 22- 13 Birth of a Turtle

public class Turtle

private double x; // x position of turtle

private double y; // y position of turtle

private double heading; // heading in degrees

private boolean pendown; // is turtle in drawing mode?

private Color color; // Red is the default

public Turtle()

this.color = Color.red;

this.x = 0.0;

this.y = 0.0;

this.heading = 0.0;

this.pendown = true;

// Instance methods?

TurtleWorld graphics 22- 14 Instance methods*

public class Turtle

// Instance variables and

// Constructor methods

// (been there, done that)

// Instance methods?

private or public?

*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); } }

casting

ints into

doubles