/* FILE NAME: NestedWindowWorld.java * AUTHOR: Stella * DATE: Oct 15 2007 * COMMENTS: Defines a recursive methods * on Turtle World, to be used in lab to draw diagonally nested squares. * * MODIFICATION HISTORY: * */ import java.awt.*; public class NestedWindowWorld extends TurtleWorld { String parameterNames [] = {"size", "levels"}; ParameterFrame params; public void setup() { params = new ParameterFrame(" Parameters",200, 50, parameterNames); } public void run() { WindowBuilder wanda = new WindowBuilder(); // Add code to move to good initial position, if you want wanda.buildDiagonallyNestedWindows(params.getDoubleParam("size"), params.getIntParam("levels")); } //------------------------------------------------------------------------------ // The following main method is needed to run the applet as an application public static void main (String[] args) { runAsApplication(new NestedWindowWorld(), "NestedWindowWorld"); } } class WindowBuilder extends Turtle { public void buildDiagonallyNestedWindows(double size, int levels) { System.out.println("buildDiagonallyNestedWindows("+size+", "+levels+");"); // add your code here if (levels == 0) return; drawSquare(size); //draw one square, the outer-most //take care of the lower-left part of that square buildDiagonallyNestedWindows(size/2, levels-1); //position to take care of the upper-right part of the big square positionCenter(size); //take care of the upper-right part of the big square buildDiagonallyNestedWindows(size/2, levels-1); positionStart(size); //get back to where you started } // add additional methods here //It draws a square with sides of length "size". //Assumes the starting position to be the lower-left point of the square. //The final position and heading of the turtle are the same as the initial. public void drawSquare(double size) { fd(size); lt(90); fd(size); lt(90); fd(size); lt(90); fd(size); lt(90); } //Moves the turtle from the lower-left point of a sqaure, to the center of it. //The initial and final facing of the turtle are the same (facing east). public void positionCenter(double size) { pu(); fd(size/2); lt(90); fd(size/2);rt(90); pd(); } //Moves the turtle from the center of a sqaure, to the lower-left point of it. //The initial and final facing of the turtle are the same (facing east). public void positionStart(double size) { pu(); bd(size/2); lt(90); bd(size/2);rt(90); pd(); } }