FileDocCategorySizeDatePackage
spiral.javaAPI DocExample2250Tue Aug 08 17:19:28 BST 2000None

spiral.java

import java.awt.*;		// use existing graphics code
import java.applet.*;	// use existing applet code

// Create an applet and draw a spiral.

public class spiral extends Applet  // this is the applet
{
	private int lengthIncrement = 5 ; // the increment in line length after a line is drawn
	private int length ;		// the length of the line to be drawn
	private int startx ;		// the start x coord of the line
	private int starty ;		// the start y coord of the line
	private int endx ; 		// the end x coord of the line
	private int endy ;		// the end y position of the line

	public void paint(Graphics g)		// this does the drawing
	{
		length = lengthIncrement ;	// initialise length
		startx = 150 ;					// initialise position to centre of
		starty = 150 ;					// applet window
		
		g.setColor(Color.blue) ;	// set the pen colour
		
		// start drawing lines, 4 per iteration
		for ( int count = 1 ; count <= 40  ; count += 4)
		{
			// start by drawing a vertical line upwards
			endx = startx ;			// set the end coords of the line
			endy = starty - length ;
			g.drawLine(startx,starty,endx,endy) ; // draw the line
			length = length + lengthIncrement ; // update length
			startx = endx ;	// reset the start of the line
			starty = endy ;
			
			// then draw a horizontal line to right
			endx = startx + length ;	// set the end coords of the line
			endy = starty ;
			g.drawLine(startx,starty,endx,endy) ; // draw the line
			length = length + lengthIncrement ;	// update length
			startx = endx ;	// reset the start of the line
			starty = endy ;
			
			// then draw a vertical line downwards 
			endx = startx ;	// set the end coords of the line
			endy = starty + length ;
			g.drawLine(startx,starty,endx,endy) ; // draw the line
			length = length + lengthIncrement ;   	// update length
			startx = endx ;	// reset the start of the line
			starty = endy ;
			
			// finally draw a horizontal line to left
			endx = startx - length ; // set the end coords of the line
			endy = starty ;
			g.drawLine(startx,starty,endx,endy) ;  // draw the line
			length = length + lengthIncrement ;	// update length
			startx = endx ;	// reset the start of the line
			starty = endy ;
		}		
	}
}