Computer Science 120
Introduction to Programming

Spring 2012, Siena College

Drag4Shirts Demo

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



Drag4Shirts BlueJ Project

Click here to download a BlueJ project for Drag4Shirts.


Drag4Shirts Source Code

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


Drag4Shirts.java

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

/*
 * Example Drag4Shirts
 *
 * Drag a shirt around window, given choice of red shirt, blue shirt,
 * green shirt, and yellow shirt.
 *
 * Jim Teresco, Siena College, CSIS 120, Spring 2011
 * Based on example from Williams College CSCI 134
 *
 * $Id: Drag4Shirts.java 1576 2011-03-24 04:32:40Z terescoj $
 */

public class Drag4Shirts extends WindowController {

    // starting location of t-shirt
    private static final int ITEM_LEFT = 75;
    private static final int ITEM_TOP = 50;
    private static final int SND_OFFSET = 150;

    // T-shirts on the screen
    private TShirt redShirt, blueShirt, greenShirt, yellowShirt;

    // 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() {
        redShirt = new TShirt(ITEM_LEFT, ITEM_TOP, canvas);
        redShirt.setColor(Color.red);

        blueShirt = new TShirt(ITEM_LEFT + SND_OFFSET, ITEM_TOP, canvas);
        blueShirt.setColor(Color.blue);
        
        greenShirt = new TShirt(ITEM_LEFT, ITEM_TOP + SND_OFFSET, canvas);
        greenShirt.setColor(Color.green);

        yellowShirt = new TShirt(ITEM_LEFT + SND_OFFSET, ITEM_TOP + SND_OFFSET, canvas);
        yellowShirt.setColor(Color.yellow);
        
        selectedShirt = blueShirt; // blue 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;

        // test the selected shirt (which is on top) first.
        if (selectedShirt.contains(point)) {
            dragging = true;
        } else if (blueShirt.contains(point)) {
            selectedShirt = blueShirt;
            dragging = true;
            selectedShirt.sendToFront();
        } else if (redShirt.contains(point)) {
            selectedShirt = redShirt;
            dragging = true;
            selectedShirt.sendToFront();
        } else if (greenShirt.contains(point)) {
            selectedShirt = greenShirt;
            dragging = true;
            selectedShirt.sendToFront();
        } else if (yellowShirt.contains(point)) {
            selectedShirt = yellowShirt;
            dragging = true;
            selectedShirt.sendToFront();
        } else {
            dragging = false;
        }
    }

    // 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 all 4 shirts back to starting positions when mouse leaves window
    public void onMouseExit(Location point) {
        redShirt.reset();
        blueShirt.reset();
        greenShirt.reset();
        yellowShirt.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 1576 2011-03-24 04:32:40Z terescoj $
 */

public class TShirt {

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

    /* 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);
    }
}