Computer Science 120
Introduction to Programming

Fall 2011, Siena College

FlagMaker Demo

A working demo of FlagMaker will appear below. Click inside the applet to interact with it.



FlagMaker BlueJ Project

Click here to download a BlueJ project for FlagMaker.


FlagMaker Source Code

The Java source code for FlagMaker is below. Click on a file name to download it.


FlagMaker.java

/*
 * 
 * Example FlagMaker: draw some flags at click points.
 *
 * Jim Teresco, Siena College, CSIS 120, Fall 2011
 * Based on example from CSCI 134, Williams College
 *
 * $Id: FlagMaker.java 1560 2011-03-07 18:29:47Z terescoj $
 */

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

public class FlagMaker extends WindowController {

    public void begin() {

        new Text("Click to draw a flag", 10, 10,canvas);
    }

    public void onMouseClick(Location point) {

        new Flag(point, canvas);
    }
}

Flag.java

/*
 * 
 * Example FlagMaker: draw a 48-star flag using while loops
 * and private helper methods.
 *
 * Jim Teresco, Siena College, CSIS 120, Fall 2011
 * Based on example from CSCI 134, Williams College
 *
 * $Id: Flag.java 1560 2011-03-07 18:29:47Z terescoj $
 */

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;

    public Flag(Location point, DrawingCanvas canvas) {

        // white rectangle so the flag is opaque
        new FilledRect(point, FLAG_WIDTH, FLAG_HT, canvas)
            .setColor(Color.white);
        
        // draw short stripes
        drawStripes(point.getX() + STAR_FIELD_WIDTH,
            point.getY(),
            SHORT_STRIPE_WIDTH,
            NUM_SHORT_STRIPES, canvas);

        // draw long stripes
        drawStripes(point.getX(),
            point.getY()+2 * NUM_SHORT_STRIPES * STRIPE_HT,
            FLAG_WIDTH+1,
            NUM_LONG_STRIPES, canvas);

        // draw field of stars
        drawStars(point, canvas);

        // outline of the 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,
                             DrawingCanvas canvas) {
        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, DrawingCanvas canvas) {

        // 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++;
        }
    }

}