/home/wpollock1/public_html/restricted/Java1/Spiral.java
// Model solution to problem P7.9: Draw a square spiral
// counter-clockwise.
// Written 2/2007 by Wayne Pollock, Tampa Florida USA
import java.applet.*;
import java.awt.*;
public class Spiral extends Applet
{
static final int NUM_SIDES = 4 * 8; // 8 loops in spiral
static final int DIST = 12; // distance in pixels between lines
public void paint ( Graphics g )
{
// Start at the center:
int x = getWidth() / 2;
int y = getHeight() / 2;
// Starting direction:
int dir = 0; // 0=left, 1=up, 2=right, 3=down
int lineLength = DIST;
// Draw two sides per iteration:
for ( int i = 0; i <= NUM_SIDES; i += 2 )
{
//System.err.println("i="+i+", x="+x+", y="+y+", lineLength="+lineLength);
switch ( dir ) {
case 0: g.drawLine( x, y, x+lineLength, y ); // Left
x += lineLength;
++dir;
case 1: g.drawLine( x, y, x, y-lineLength ); // Up
y -= lineLength;
++dir;
break;
case 2: g.drawLine( x, y, x-lineLength, y ); // Right
x -= lineLength;
++dir;
case 3: g.drawLine( x, y, x, y+lineLength ); // Down
y += lineLength;
dir = 0;
}
lineLength += DIST;
}
}
}