Computer Science 252
Problem Solving with Java

Fall 2013, The College of Saint Rose

MovingFlags Demo

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



MovingFlags BlueJ Project

Click here to download a BlueJ project for MovingFlags.


MovingFlags Source Code

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


MovingFlags.java

/*
 * 
 * Example FlagMaker: draw some flags at click points.
 *
 * Jim Teresco, Siena College, CSIS 120, Spring 2011
 * Based on example from CSCI 134, Williams College
 *
 * $Id: MovingFlags.java 2218 2013-10-18 14:06:39Z terescoj $
 */

import java.awt.*;
import objectdraw.*;
import java.util.ArrayList;

public class MovingFlags extends WindowController {

    private ArrayList<Flag> flags;
    private boolean dragging;
    private Flag selectedFlag;
    private Location lastMouse;
    
    public void begin() {

        flags = new ArrayList<Flag>();
        
        new Text("Click to draw a flag", 10, 10,canvas);
    }

    public void onMousePress(Location point) {

        dragging = false;
        
        // if point is on a flag, start dragging it
        for (int flagNum = 0; flagNum < flags.size(); flagNum++) {
            if (flags.get(flagNum).contains(point)) {
                dragging = true;
                selectedFlag = flags.get(flagNum);
                lastMouse = point;
            }
        }
        // if not, create a new flag
        if (!dragging) {
            flags.add(new Flag(point, canvas));
        }
    }
    
    public void onMouseDrag(Location point) {
        
        if (dragging) {
            selectedFlag.move(point.getX() - lastMouse.getX(),
                              point.getY() - lastMouse.getY());
            lastMouse = point;
        }
    }
}

Flag.java

/*
 * 
 * Example FlagMaker: draw a 48-star flag using while loops
 * and private helper methods.
 *
 * Jim Teresco, Siena College, CSIS 120, Spring 2011
 * Based on example from CSCI 134, Williams College
 *
 * $Id: Flag.java 2218 2013-10-18 14:06:39Z terescoj $
 */

import java.awt.*;
import objectdraw.*;
import java.util.ArrayList;

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;

    // instance variables for all parts of the flag
    private ArrayList<FilledOval> stars;
    private ArrayList<FilledRect> stripes;
    private FilledRect background;
    private FilledRect starBackground;
    private FramedRect starBorder;
    private FramedRect flagBorder;
    
    public Flag(Location point, DrawingCanvas canvas) {

        stars = new ArrayList<FilledOval>();
        stripes = new ArrayList<FilledRect>();
        
        // white rectangle so the flag is opaque
        background = new FilledRect(point, FLAG_WIDTH, FLAG_HT, canvas);
        background.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
        flagBorder = 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)
        {
            FilledRect stripe = 
                new FilledRect(x, y, width, STRIPE_HT, canvas);
            stripe.setColor(Color.red);
            stripes.add(stripe);
            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
        starBackground = new FilledRect(point, STAR_FIELD_WIDTH,
            STAR_FIELD_HT, canvas);
        starBackground.setColor(Color.blue);

        starBorder = 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) {
                FilledOval star = new FilledOval(x,y,STAR_DIAM,
                    STAR_DIAM,canvas);
                star.setColor(Color.white);
                stars.add(star);
                col++;
                x = x + STAR_JUMP;
            }
            y = y + STAR_JUMP;
            row++;
        }
    }

    public void move(double dx, double dy) {
        
        for (int starNum = 0; starNum < stars.size(); starNum++) {
            stars.get(starNum).move(dx, dy);
        }
        for (int stripeNum = 0; stripeNum < stripes.size(); stripeNum++) {
            stripes.get(stripeNum).move(dx, dy);
        }
        background.move(dx, dy);
        starBackground.move(dx, dy);
        starBorder.move(dx, dy);
        flagBorder.move(dx, dy);
     
    }
    
    public boolean contains(Location point) {
        
        return flagBorder.contains(point);
    }
}