// Drawing polygon flowers with turtles. // Created by Stanzi on 11/9/97 // Updated by Lyn on 4/12/98 import java.awt.*; // Import Abstract Window Toolkit import java.applet.*; // Import Applet stuff public class FlowerWorld extends TurtleWorld { ParameterFrame params; String parameterNames [ ] = {"Petals", "Sides", "Length"}; CheckboxGroup checkboxes = new CheckboxGroup(); //Checkbox tailBox = new Checkbox("flowerTail", checkboxes, true); Checkbox whileBox = new Checkbox("flowerWhile", checkboxes, false); Checkbox forBox = new Checkbox("flowerFor", checkboxes, false); Checkbox nestForBox = new Checkbox("flowerNestedFor", checkboxes, false); public void setup() { params = new ParameterFrame("Flower Parameters", 150, 175, parameterNames); Panel northPanel = params.getNorthPanel(); northPanel.setLayout(new GridLayout(3,1)); //northPanel.setLayout(new GridLayout(4,1)); //northPanel.add(tailBox); northPanel.add(whileBox); northPanel.add(forBox); northPanel.add(nestForBox); params.show(); } public void run() { FlowerMaker flora = new FlowerMaker(); int petals = params.getIntParam("Petals"); int sides = params.getIntParam("Sides"); int length = params.getIntParam("Length"); /* if (tailBox.getState()) { flora.flower(petals, sides, length); } else */ if (whileBox.getState()) { flora.flowerWhile(petals, sides, length); } else if (forBox.getState()) { flora.flowerFor(petals, sides, length); } else { // if (forBox.getState()) flora.flowerNestedFor(petals, sides, length); } } //------------------------------------------------------------------------------ // The following main method is needed to run the applet as an application public static void main (String[] args) { runAsApplication(new FlowerWorld(), "FlowerWorld"); } } class FlowerMaker extends PolygonMaker { public void flowerWhile(int petals, int sides, int length){ double angle = 360.0/(double)sides; while (petals > 0) { polygonFor(sides,length); lt(angle); petals = petals - 1; } } // for loop implementation which uses a counter which counts up public void flowerFor(int petals, int sides, int length){ double angle = 360.0/(double)sides; for (int i = 1; i <= petals; i=i+1) { polygonWhile(sides,length); lt(angle); } } public void flowerNestedFor(int petals, int sides, int length){ double petalAngle = 360.0/(double)petals; double polyangle = 360.0/(double)sides; for (int i = 1; i <= petals; i=i+1) { for (int j = 1; j <= sides; j=j+1) { fd(length); lt(polyangle); } lt(petalAngle); } } }