import objectdraw.*; import java.awt.*; // Class to recursively draw animated parsley class ParsleyTree extends ActiveObject { private static final int PAUSETIME = 1000; private static final double MINSIZE = 20.0; // constants for determining where and what size are the recursive children private static final double PI = Math.PI; private static final double LEFTANGLE = PI/6; // angle of left branch to the stem private static final double LEFTFRAC = 0.35; // at what fraction of the stem it starts private static final double LEFTSIZE = 0.75; // what fraction of the stem will the left branch size be? private static final double CENTERANGLE = PI/4; // continue from end of stem at this angle to current private static final double CENTERFRAC = 0.55; // what fraction of the stem the new center stem is private static final double RIGHTANGLE = -PI/5; // angle of right branch to the stem private static final double RIGHTFRAC = 0.65; // at what fraction of the stem it starts private static final double RIGHTSIZE = 0.45; // what fraction of the stem will the left branch size be? private Location startLocation; // starting coordinates of stem private AngLine stem; // stem of parsley plant private ParsleyTree left, center, right;// Three branches of parsley private DrawingCanvas canvas; // canvas to draw on private double size; // size of stem private double direction; // angle of stem with positive x-axis // Draw stem of parsley in anticipation of later growth ParsleyTree(Location startLocation, double size, double direction, DrawingCanvas canvas) { stem = new AngLine(startLocation,size,direction,canvas); // Draw stem and color green stem.setColor(Color.green); this.canvas = canvas; // Save parameters for execute method this.direction = direction; this.size = size; this.startLocation = startLocation; start(); } // After pause, start growing parsley from stem public void run() { Location destLocation = stem.getEnd(); // end of stem double x = startLocation.getX(); // Location of startLocation double y = startLocation.getY(); double dx = destLocation.getX()-x; // Location of end of stem double dy = destLocation.getY()-y; pause(PAUSETIME); if ( size > MINSIZE ) { // Big enough to keep growing left = new ParsleyTree(new Location(x + LEFTFRAC*dx, y + LEFTFRAC*dy), size * LEFTSIZE, direction + LEFTANGLE, canvas); center = new ParsleyTree(destLocation, size * CENTERFRAC, direction + CENTERANGLE, canvas); right = new ParsleyTree(new Location(x + RIGHTFRAC*dx, y + RIGHTFRAC*dy), size * RIGHTSIZE, direction + RIGHTANGLE, canvas); } } // param x,y amount to move parsley public void move( double x, double y) { stem.move(x,y); // move stem if (center!=null) { // move rest of parsley if grown left.move(x,y); center.move(x,y); right.move(x,y); } } }