/* FILE NAME: EiffelWorld.java * AUTHOR: Stella * DATE: Oct 15 2007 * COMMENTS: Defines a recursive methods * on Turtle World, to be used in lab to draw Eiffel Towers. * * MODIFICATION HISTORY: This file is a simple modification of an * older similar file. It can run as an application (in addition to an applet) * * */ import java.awt.*; public class EiffelWorld extends TurtleWorld { String parameterNames [] = {"size", "shrink factor", "angle", "levels", "offset"}; ParameterFrame params; public void setup() { params = new ParameterFrame(" Parameters",200, 150, parameterNames); } public void run() { EiffelBuilder ellie = new EiffelBuilder(); ellie.eiffelTower(params.getDoubleParam("size"), params.getDoubleParam("shrink factor"), params.getDoubleParam("angle"), params.getIntParam("levels"), params.getDoubleParam("offset")); } //------------------------------------------------------------------------------ // The following main method is needed to run the applet as an application public static void main (String[] args) { runAsApplication(new EiffelWorld(), "EiffelWorld"); } } class EiffelBuilder extends Turtle { //It builds an Eiffel Tower with the specified number of levels, //spaced offset distance apart. //The inner angle of each level is the same, and given as a param. //The side length size of the base level is specified. //Each higher level has it's side length shrunk by a specified //shrink factor. public void eiffelTower (double size, double shrinkFactor, double angle, int levels, double offset) { System.out.println("eiffelTower("+size+", "+shrinkFactor+ ", "+angle+", "+levels+", "+offset+");"); // add your code here if (levels == 0) return; // base case eiffelBase(size, angle); // draw the base story of the Eiffel Tower moveToNextStory(offset); // move in position to draw next pattern //draw next pattern eiffelTower(size*shrinkFactor, shrinkFactor, angle, levels-1, offset); returnToStart(offset); // meet invariant: start and end in same place } // add additional methods here //It draws one story of an Eiffel Tower //with the specified inner "angle" and side length of "size". //Assumes the starting point to be the tip point, of the corner. //The facing is EAST, starting out. //Final position and facing have to be the same as the starting ones. public void eiffelBase (double size, double angle) { System.out.println("In eiffelBase()"); System.out.println("eiffelBase size "+size); rt((180-angle)/2); fd(size); bd(size); rt(angle); fd(size); bd(size); lt(angle+(180-angle)/2); } //Moves the turtle by "offset" distance, to a direction 90 degrees left //from the initial position and facing. //The final facing remains the same as the initial. //No trace is left behind. public void moveToNextStory (double offset) { System.out.println("In moveToNextStory()"); pu(); lt(90); fd(offset); rt(90); pd(); } //Moves the turtle by "offset" backward distance, to a direction 90 degrees left //from the initial position and facing. //The final facing remains the same as the initial. //No trace is left behind. public void returnToStart (double offset) { System.out.println("returnToStart"); pu(); lt(90); bd(offset); rt(90); pd(); } }