DrawShapesWLoops Applet

Demonstrates the use of nested loops to draw a pattern of circles on the canvas.


Your browser is ignoring the <APPLET> tag!


import objectdraw.*;
import java.awt.*;

/** Draw a rectangular pattern of circles that look like
* knitted scarf on the canvas.
* The purpose of this example is to demonstrate a while loop.

* @author Lisa Ballesteros

*@version April, 2009
*/
public class DrawShapesWLoops extends WindowController {

// The width of the shape
private static final int SHAPE_WIDTH = 20;

// The length of a rail tie
private static final int SHAPE_LENGTH = 20;

// The amount of space between shapes.
private static final int SHAPE_SPACING = -5;

private static final int NUM_COLUMNS = 10;
private static final int NUM_ROWS = 10;

/** Resize the canvas */
public void begin()
{
resize (600, 600);
}

/* On mouse click, cause pattern to be drawn @param point. */
public void onMouseClick (Location point){

drawPattern(point);
}

/* Draw NUM_ROWS of NUM_COLUMNS circles beginning
* @ param point
*/
public void drawPattern (Location point){

double x;
double y = point.getY();

for (int rows = 0; rows < NUM_ROWS; rows++){
x = point.getX();
for (int cols = 0; cols < NUM_COLUMNS; cols++){
new FramedOval(x, y, SHAPE_WIDTH, SHAPE_LENGTH, canvas);
x = x + SHAPE_WIDTH + SHAPE_SPACING;
}
y = y + SHAPE_LENGTH + SHAPE_SPACING;
}
}
}