Computer Science 120
Introduction to Programming

Spring 2011, Siena College

Drag20Shirts Demo

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



Drag20Shirts BlueJ Project

Click here to download a BlueJ project for Drag20Shirts.


Drag20Shirts Source Code

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


Drag20Shirts.java

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

/*
 * Example Drag20Shirts
 
 * Drag one of 20 shirts around window, now with 10 fixed colors and 
 * a 2D layout.
 *
 * Jim Teresco, Siena College, CSIS 120, Spring 2011
 *
 * $Id: Drag20Shirts.java 1582 2011-03-30 04:29:27Z terescoj $
 */

public class Drag20Shirts extends WindowController {

    // starting location of t-shirts
    private static final int ITEM_LEFT = 30;
    private static final int ITEM_TOP = 50;
    private static final int X_OFFSET = 90;
    private static final int Y_OFFSET = 150;
    
    // how many shirts to draw
    private static final int NUM_ROWS = 4;
    private static final int NUM_COLS = 5;
    private static final int NUM_SHIRTS = NUM_ROWS * NUM_COLS;
    
    // T-shirts on the screen -- an array!
    private TShirt[] shirts;
    
    // array of colors with initializers
    private Color[] shirtColors = { Color.blue, Color.red, Color.green,
                                    Color.yellow, Color.cyan, Color.black,
                                    Color.white, Color.gray, Color.magenta,
                                    Color.orange };

    // The piece of laundry to be moved.
    private TShirt selectedShirt;

    // Previously noted position of mouse cursor
    private Location lastPoint;

    // Whether user is actually dragging a shirt
    private boolean dragging;

    // display the shirts
    public void begin() {

        // construct the array that can hold our 10 shirts
        shirts = new TShirt[NUM_SHIRTS];

        // now create the shirts, with proper number of columns
        int nextShirtY = ITEM_TOP;
        for (int rowNum = 0; rowNum<NUM_ROWS; rowNum++) {
            int nextShirtX = ITEM_LEFT;

            for (int colNum = 0; colNum<NUM_COLS; colNum++) {

                int shirtNum = rowNum*NUM_COLS + colNum;
            
                shirts[shirtNum] = new TShirt(nextShirtX, nextShirtY, canvas);
                // since we don't have as many colors as shirts, we will
                // "wrap around" and reuse colors using modulo arithmetic
                shirts[shirtNum].setColor(shirtColors[shirtNum % shirtColors.length]);

                nextShirtX = nextShirtX + X_OFFSET;
            }
            nextShirtY = nextShirtY + Y_OFFSET;
        }

        selectedShirt = shirts[0]; // first shirt on top at start
    }

    // Whenever mouse is depressed, note its location
    // and which (if any) shirt the mouse is on.
    public void onMousePress(Location point) {
        lastPoint = point;

        dragging = false;
        // test the selected shirt (which is on top) first.
        if (selectedShirt.contains(point)) {
            dragging = true;
        } else {
            for (int shirtNum = 0; shirtNum<NUM_SHIRTS; shirtNum++) {
                if (shirts[shirtNum].contains(point)) {
                    selectedShirt = shirts[shirtNum];
                    dragging = true;
                    selectedShirt.sendToFront();
                }
            }
        }
    }

    // If mouse is dragged, move the selected shirt with it
    // If didn't press mouse on a shirt, do nothing
    public void onMouseDrag(Location point) {
        if (dragging) {
            selectedShirt.move(point.getX() - lastPoint.getX(),
                point.getY() - lastPoint.getY());
            lastPoint = point;
        }
    }

    // move both shirts back to starting positions when mouse leaves window
    public void onMouseExit(Location point) {

        for (int shirtNum = 0; shirtNum<NUM_SHIRTS; shirtNum++) {
            shirts[shirtNum].reset();
        }
        dragging = false;
    }
}

TShirt.java


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

/*
 * Example Drag2Shirts
 *
 * A class that defines a graphical type that looks a bit
 * like a t-shirt.
 *
 * Jim Teresco, Siena College, CSIS 120, Spring 2011
 * Based on example from Williams College CSCI 134
 *
 * $Id: TShirt.java 1582 2011-03-30 04:29:27Z terescoj $
 */

public class TShirt {

    /* constant for overall size of the T-shirt */
    private static final double SIZE = 60;

    /* other constants defined relative to SIZE */
    private static final double SLEEVE_WIDTH = SIZE;
    private static final double SLEEVE_HEIGHT = 0.2 * SIZE;

    private static final double BODY_WIDTH = 0.6 * SIZE;
    private static final double BODY_HEIGHT = (0.65) * SIZE;

    private static final double BODY_INSET = 0.2 * SIZE;

    private static final double NECK_WIDTH = 0.3 * SIZE;
    private static final double NECK_HEIGHT = 0.06 * SIZE;

    private static final double NECK_INSET = 0.35 * SIZE;

    private double startX, startY; // the initial location of the shirt
    
    // Rectangles that form a border around the shirt
    private FramedRect sleeveTrim, bodyTrim; 
    private FramedOval neckTrim; 
    
    // Rectangles that form the interior color of the shirt
    private FilledRect body, sleeves; 
    private FilledOval neck; 

    /*
     * Create a new T-shirt with its upper left corner at (x,y) and with
     * a width of size.
     */
    public TShirt(double x, double y, DrawingCanvas canvas) {
        
        // create boundary rectangles
        sleeveTrim = new FramedRect(x, 
                                    y + NECK_HEIGHT / 2,
                                    SLEEVE_WIDTH, 
                                    SLEEVE_HEIGHT, 
                                    canvas);
        bodyTrim = new FramedRect(x + BODY_INSET,
                                  y + NECK_HEIGHT / 2, 
                                  BODY_WIDTH,
                                    BODY_HEIGHT, 
                                    canvas);

        // create interior rectangles
        sleeves = new FilledRect(x + 1,
                                 y + NECK_HEIGHT / 2 + 1,
                                 SLEEVE_WIDTH - 1, 
                                 SLEEVE_HEIGHT - 1, 
                                 canvas);
        body = new FilledRect(x + BODY_INSET + 1,
                              y + NECK_HEIGHT / 2 + 1, 
                              BODY_WIDTH - 1,
                              BODY_HEIGHT - 1, 
                              canvas);

        // give it a neck hole
        neck = 
            new FilledOval(x + NECK_INSET, y, NECK_WIDTH, NECK_HEIGHT, canvas);
        neckTrim = 
            new FramedOval(x + NECK_INSET, y, NECK_WIDTH, NECK_HEIGHT, canvas);

        // set the interior to white
        body.setColor(Color.white);
        neck.setColor(Color.white);
        sleeves.setColor(Color.white);

        // remember the starting location for re-set
        startX = x;
        startY = y;
    }
    
    /*
     * Move the t-shirt by specified offsets.
     */
    public void move(double xOffset, double yOffset) {
        body.move(xOffset, yOffset);
        neck.move(xOffset, yOffset);
        sleeves.move(xOffset, yOffset);
        bodyTrim.move(xOffset, yOffset);
        sleeveTrim.move(xOffset, yOffset);
        neckTrim.move(xOffset, yOffset);
    }

    /*
     * Returns true if the t-shirt contains the point;
     * false otherwise
     */
    public boolean contains(Location pt) {
        return bodyTrim.contains(pt) || sleeveTrim.contains(pt) || neck.contains(pt);
    }

    /*
     * Change color of t-shirt to newColor.
     */
    public void setColor(Color newColor) {
        body.setColor(newColor);
        neck.setColor(newColor);
        sleeves.setColor(newColor);
    }

    /*
     * Put t-shirt in front of other objects on canvas.
     */
    public void sendToFront() {
        bodyTrim.sendToFront();
        sleeveTrim.sendToFront();
        body.sendToFront();
        neck.sendToFront();
        sleeves.sendToFront();
        neckTrim.sendToFront();
    }

    /*
     * Move the t-shirt to a new upper-left coordinate position. 
     */
    public void moveTo(double x, double y) {
        double dx = x - sleeves.getX();
        double dy = y - neck.getY();
        
        this.move(dx, dy);
 
    }    
    
    /*
     * Move t-shirt back to starting position.
     */
    public void reset() {
        this.moveTo(startX, startY);
    }
}