/* FILE NAME: BoomerangWorld.java * AUTHOR: Stella * DATE: Oct 10 2007 * COMMENTS: Defines a recursive methods * on Turtle World, to be used in lab to draw Boomerangs. * * MODIFICATION HISTORY: Based on an older version. * Now it can run as an application and an applet. * */ import java.awt.*; public class BoomerangWorld extends TurtleWorld { String parameterNames [] = {"size", "number", "offset"}; ParameterFrame params; public void setup() { params = new ParameterFrame("Boomerang Parameters",200, 100, parameterNames); } public void run() { BoomerangThrower barb = new BoomerangThrower(); //Add code to move the turtle to good initial position, //but only if you want to! barb.throwBoomerangs(params.getDoubleParam("size"), params.getIntParam("number"), params.getDoubleParam("offset")); } //------------------------------------------------------------------------------ // The following main method is needed to run the applet as an application public static void main (String[] args) { runAsApplication(new BoomerangWorld(), "BoomerangWorld"); } } class BoomerangThrower extends Turtle { //It draws "number" of boomerangs. Each one is "size" long on each side. //Boomerangs are spaced "offset" distance apart. public void throwBoomerangs (double size, int number, double offset) { System.out.println("throwBoomerangs("+size+", "+number+", "+offset+");"); if (number == 0) return; //base case: do nothing drawOneBoomerang(size); //draw the current boomerang moveForNextBoomerang(offset); //move in position to draw next pattern throwBoomerangs(size, number-1, offset); //draw the rest of the pattern returnToStart(offset); // meet invariant: start and end in same place } // add additional methods here //It draws a boomerang of specified size with inner angle 120 degrees. //Assumes the starting point to be the top point of the upper leg of the shape. //After it is done drawing, the position and facing of the turtle //is the same as the starting ones. public void drawOneBoomerang (double size) { System.out.println("Inside drawOneBoomerang()"); //System.out.println("Position before starting: " + getPosition()); rt(60); //turn to start the upper leg of the V-shape fd(size); //draw the upper leg of the V-shape rt(60); //get in position to start the lower leg of the V-shape fd(size); //draw the lower leg of the V-shape //Now the turtle is positioned on the far tip of the lower leg, //facing douth-west. //System.out.println("Position after just drawing: " + getPosition()); //Get it back to starting position and facing. //One way of doing this is by back-tracking your steps: bd(size); lt(60); bd(size); lt(60); } //It moves the turtle "offset" ahead, in preparation of //starting the next shape. No trace is left behind. public void moveForNextBoomerang (double offset) { System.out.println("Inside moveToNextBoomerang()"); pu(); fd(offset); pd(); } //It moves the turtle "offset" backwards. //No trace is left behind. public void returnToStart (double offset) { System.out.println("Inside returnToStart()"); pu(); bd(offset); pd(); } }