/* Spiral.java (L.McCann, 2007/03/29) * * A short demo of the ACM Java Task Force's acm.graphics package's GTurle * class. The idea can be traced back to Seymour Papert's "Mindstorms" * book. Even small children can grasp the idea of a pen moving forward, * turning left or right, moving forward some more, etc. Beyond introducing * computer programming to kids, the model is also useful for constructing * various figures, such as the spiral produced by this program. * * The JTF tutorial (Aug 2006 version) has a brief introduction to the * GTurtle class on pages 38-42. */ import acm.graphics.*; import acm.program.*; import java.awt.*; // Needed for Color public class Spiral extends GraphicsProgram { static final int CARDINALITY = 150; // # members in segment length sequence static final int SCALE = 6; // zoom factor; spreads segments apart public void run () { GTurtle turtle; // Drawing model; imagine a turtle wielding a pen System.out.println("\nA simple spiral to demo the GTurtle Class\n"); turtle = new GTurtle( getWidth()/2, getHeight()/2 ); // window middle add(turtle); turtle.hideTurtle(); // turtle is visible by default turtle.setDirection(180); // 0 deg = East, 90 = north, etc. turtle.penDown(); // draw when the turtle moves turtle.setColor(Color.BLACK); // draw black lines turtle.setSpeed(0.75); // range is 0 .. 1 // The length of the line segments matches the sequence // 1, 1, 2, 2, 3, 3, 4, 4, ..., which is easily generated // by the rule d(i) = i/2 + 1, where i = 0, 1, 2, ... for (int i=0; i solid spiral turtle.left(90); } System.out.println("The sprial is complete!\n"); } }