Computer Science 202
Introduction to Programming

Fall 2013, The College of Saint Rose

DrawFlags Demo

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



DrawFlags BlueJ Project

Click here to download a BlueJ project for DrawFlags.


DrawFlags Source Code

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


DrawFlags.java

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

/*
 * Example DrawFlags: draw flags when the mouse is pressed using
 * loops and private helper methods.
 *
 * Jim Teresco, The College of Saint Rose, Fall 2013, CSC 202
 *
 * $Id: DrawFlags.java 2231 2013-10-27 18:40:06Z terescoj $
 */

public class DrawFlags extends WindowController {
    
    // 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 void onMousePress(Location point) {
        
        // 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++;
        }
    }
}