Flag.java
/*
Class representing a U.S. flag from the 50's
(when there were only 48 states).
Illustrates while loops and use of private methods
as helpers.
*/
import java.awt.*;
import objectdraw.*;
public class Flag {
// number of stars per row and column
private static final int NUM_ROWS_STARS = 6;
private static final int NUM_COLS_STARS = 8;
// size of stars (really circles!)
private static final int STAR_DIAM = 6;
// space btn start of successive stars
private static final int STAR_JUMP = 8;
// space btn edge of star field and first star
private static final int OFFSET = 1;
// dimensions of star field
private static final int STAR_FIELD_WIDTH =
NUM_COLS_STARS * STAR_JUMP+OFFSET;
private static final int STAR_FIELD_HT =
NUM_ROWS_STARS * STAR_JUMP+OFFSET;
// dimensions of flag
private static final double FLAG_WIDTH =
2.5 * STAR_FIELD_WIDTH;
private static final int FLAG_HT = STAR_FIELD_HT*13/7;
// dimensions of stripes
private static final double SHORT_STRIPE_WIDTH =
FLAG_WIDTH - STAR_FIELD_WIDTH;
private static final int STRIPE_HT = STAR_FIELD_HT/7;
// number of red stripes of each sort in flag
private static final int NUM_SHORT_STRIPES = 4;
private static final int NUM_LONG_STRIPES = 3;
private DrawingCanvas canvas; // where to draw
// Draw a flag at the coordinates point on theCanvas
public Flag( Location point, DrawingCanvas theCanvas )
{
canvas = theCanvas; // save canvas
// draw short stripes
drawStripes(point.getX() + STAR_FIELD_WIDTH,
point.getY(),
SHORT_STRIPE_WIDTH,
NUM_SHORT_STRIPES);
// draw long stripes
drawStripes(point.getX(),
point.getY()+2 * NUM_SHORT_STRIPES * STRIPE_HT,
FLAG_WIDTH+1,
NUM_LONG_STRIPES);
// draw field of stars
drawStars(point);
// outline flag
new FramedRect(point.getX()-1,
point.getY()-1,
FLAG_WIDTH+1,
FLAG_HT+1,
canvas);
}
// Draw numStripes red stripes starting at (x,y)
// which are "width" pixels wide.
private void drawStripes(double x, double y,
double width, int numStripes)
{
int stripeNum = 0;
while (stripeNum < numStripes) {
new FilledRect(x, y, width, STRIPE_HT,
canvas).setColor(Color.red);
stripeNum++;
y = y + 2 * STRIPE_HT;
}
}
// Draw blue background and stars, framed in black, starting at point
private void drawStars(Location point)
{
// Draw and frame background
new FilledRect(point,
STAR_FIELD_WIDTH,
STAR_FIELD_HT,
canvas).setColor(Color.blue);
new FramedRect(point.getX()-1,
point.getY()-1,
STAR_FIELD_WIDTH+1,
STAR_FIELD_HT+1,
canvas);
int row = 0;
int col;
double x;
double y = point.getY()+OFFSET;
// draw stars
while (row < NUM_ROWS_STARS) {
col = 0;
x = point.getX()+OFFSET;
while (col < NUM_COLS_STARS) {
new FilledOval(x,y,STAR_DIAM,
STAR_DIAM,canvas).setColor(Color.white);
col++;
x = x + STAR_JUMP;
}
y = y + STAR_JUMP;
row++;
}
}
}
MakeFlags.java
import objectdraw.*;
import java.awt.*;
// program to draw a Flag upon mouse click.
public class MakeFlags extends WindowController {
// draws a Flag with upper left at point of click
public void onMouseClick( Location point )
{
new Flag(point, canvas);
}
}