import java.awt.*;
import java.applet.*;
public class Circles extends Applet
{
private int counter; // loop control variable
private int stopValue; // loop termination variable
private int xCoord = 150; // initial value for xCoord
private int yCoord = 150; // initial value for yCoord
int diameter; // used to calculate circle diameter
final int change = 20; // change in diameter each time thru loop
public void paint(Graphics g)
{
int numCircles; // used to save teh number of circles to be drawn
stopValue = 1; // set the loop termination value
counter = (int)(Math.random() * 15 + 1); // how many circles?
numCircles = counter; // copy number of circles
xCoord = xCoord - counter * (change / 2);//X coord for outer circle
yCoord = yCoord - counter * (change / 2);//Y coord for outer circle
while (counter >= stopValue) // descending loop
{
diameter = counter * change; // circle diameter this iteration
if (counter % 2 == 0) // if counter is even then
{
g.setColor(Color.blue); // set drawing colour blue
}
else // otherwise
{
g.setColor(Color.red); // set drawing colour red
}
// draw a circle with this diameter at
// these X and Y coordinates
g.fillOval(xCoord, yCoord, diameter, diameter);
counter = counter - 1; // subtract one from counter
xCoord = xCoord + change / 2; // move X coord in by half the change in the diameter
yCoord = yCoord + change / 2; // move Y coord in by half the change in the diameter
}
g.drawString(numCircles + " Nice circles", change, counter * diameter + change);
}
} |