// Drawing trees with turtles. import java.awt.*; // Import Abstract Window Toolkit import java.applet.*; // Import Applet stuff public class TreeWorld extends TurtleWorld { String parameterNames [] = {"Levels", "Length", "Angle", "ShrinkFactor"}; String parameterFields [] = {"7", "75", "30", "0.8"}; ParameterFrame params; //------------------------------------------------------------------------------ // The following main method is needed to run the applet as an application public static void main (String[] args) { runAsApplication(new TreeWorld(), "TreeWorld"); } //------------------------------------------------------------------------------ public void setup() { params = new ParameterFrame("Tree Parameters", TurtleWorld.frameSize, 0, 225, 170, parameterNames, parameterFields); } public void run() { TreeMaker trina = new TreeMaker(); // Move to position in bottom center, facing up trina.pu(); trina.lt(90); trina.bd((this.getSize().height)/2); trina.pd(); // Draw tree trina.tree(params.getIntParam("Levels"), params.getDoubleParam("Length"), params.getDoubleParam("Angle"), params.getDoubleParam("ShrinkFactor")); } } class TreeMaker extends Turtle { public void tree (int levels, double length, double angle, double shrink) { if (levels <= 0) { // Do nothing } else { fd(length); rt(angle); tree(levels - 1, length * shrink, angle, shrink); lt(2*angle); tree(levels - 1, length * shrink, angle, shrink); rt(angle); bd(length); } } }