FileDocCategorySizeDatePackage
nestedSquares.javaAPI DocExample1484Tue Aug 08 17:17:48 BST 2000None

nestedSquares

public class nestedSquares extends Applet

Fields Summary
private int
squareXPosition
private int
squareYPosition
private int
width
Constructors Summary
Methods Summary
public voidpaint(java.awt.Graphics g)

		// initialise variables
		squareXPosition = 150 ;	 // middle of applet window
		squareYPosition = 150 ;	 // middle of applet window
		width = 20 ;				 // side length of smallest square
		
		g.setColor(Color.blue) ;	// set colour
		for (int count = 1 ; count <= 10 ; count++)
		{
			// draw rectangle
			g.drawRect(squareXPosition,squareYPosition,width,width) ;
			// reset rectangle parameters
			width = width + 20 ;
			squareXPosition = squareXPosition - 10 ;
			squareYPosition = squareYPosition - 10 ;		
		}
		
		// draw diagonals
		squareXPosition = squareXPosition + 10 ;	// undo last increment
		squareYPosition = squareYPosition + 10 ;
		width = width - 20 ;
		
		g.setColor(Color.magenta) ;
		// top left to bottom right
		g.drawLine(squareXPosition, squareYPosition,
						squareXPosition + width, squareYPosition + width) ;
		// top right to bottom left
		g.drawLine(squareXPosition + width, squareYPosition,
						squareXPosition, squareYPosition + width) ;