// LollipopTurtle.java // A subclass of Turtle with the ability // to draw lollipops import java.awt.Color; import java.util.Random; public class LollipopTurtle extends Turtle { // constants private static final double START_LENGTH = 20; private static final int START_LEVELS = 50; private static final int ANGLE = 30; private static final double SHRINK_FACTOR = 0.95; private static final int STICK_LENGTH = 150; // this is an array of Color objects that we // immediately initialize to the 7 colors of the rainbow private static final Color[] rainbowColors = { Color.red, Color.orange, Color.yellow, Color.green, Color.blue, new Color(75, 0, 130), new Color(148, 0, 211) } ; // instance variable private Random rnd = new Random(); // constructor public LollipopTurtle () { // pick a random heading (in degrees) setHeading(rnd.nextInt(360)); // pick a random color of the ones in the rainbowColors array setColor(rainbowColors[rnd.nextInt(rainbowColors.length)]); } // draw a spiral lollipop public void drawLollipop() { forward(STICK_LENGTH); right(75); drawSpiral(START_LENGTH, START_LEVELS); } // draw a spiral public void drawSpiral(double sideLength, int levels) { if (levels > 0) { forward(sideLength); left(ANGLE); drawSpiral(sideLength * SHRINK_FACTOR, levels-1); } } }