Computer Science 120
Introduction to Programming

Spring 2012, Siena College

Drag2Shirts Demo

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



Drag2Shirts BlueJ Project

Click here to download a BlueJ project for Drag2Shirts.


Drag2Shirts Source Code

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


Drag2Shirts.java

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

/*
 * Example Drag2Shirts
 *
 * Drag a shirt around window, given choice
 * of red shirt or blue shirt.
 *
 * Jim Teresco, Siena College, CSIS 120, Spring 2011
 * Based on example from Williams College CSCI 134
 *
 * $Id: Drag2Shirts.java 1577 2011-03-28 02:10:20Z terescoj $
 */

public class Drag2Shirts 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;

    // 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);
        
        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 {
            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 both shirts back to starting positions when mouse leaves window
    public void onMouseExit(Location point) {
        redShirt.reset();
        blueShirt.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 1541 2011-02-15 21:09:53Z terescoj $
 */

public class TShirt {

    // constant dimensions
    private static final int WIDTH = 150;
    private static final int HEIGHT = 175;
    private static final int BODY_WIDTH = 100;
    private static final int NECK_HEIGHT = 20;
    private static final int SLEEVE_OFFSET = NECK_HEIGHT/2;
    private static final int NECK_WIDTH = 75;
    private static final int SLEEVE_HEIGHT = 40;
    private static final int BODY_HEIGHT = HEIGHT - SLEEVE_HEIGHT;
    private static final int SLEEVE_LENGTH = (WIDTH-BODY_WIDTH)/2;

    // instance variables for shirt components
    private FilledRect body;
    private FramedRect bodyFrame;
    private FilledRect sleeves;
    private FramedRect sleeveFrame;
    private FilledOval neck;
    private FramedOval neckFrame;

    // remember the initial position of the tshirt
    private double startX, startY;

    /*
     * 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) {

        bodyFrame = new FramedRect(x+SLEEVE_LENGTH,
            y+SLEEVE_HEIGHT, BODY_WIDTH, BODY_HEIGHT, canvas);
        sleeves = new FilledRect(x, y+SLEEVE_OFFSET, 
            WIDTH, SLEEVE_HEIGHT, canvas);
        sleeves.setColor(Color.white);
        sleeveFrame = new FramedRect(x, y+SLEEVE_OFFSET, 
            WIDTH, SLEEVE_HEIGHT, canvas);
        body = new FilledRect(x+SLEEVE_LENGTH+1,
            y+SLEEVE_HEIGHT, BODY_WIDTH-2, BODY_HEIGHT-1, canvas);
        body.setColor(Color.white);
        neck = new FilledOval(x + (WIDTH-NECK_WIDTH)/2,
            y, NECK_WIDTH, NECK_HEIGHT, canvas);
        neck.setColor(Color.white);
        neckFrame = new FramedOval(x + (WIDTH-NECK_WIDTH)/2,
            y, NECK_WIDTH, NECK_HEIGHT, canvas);

        // remember initial location
        startX = x;
        startY = y;

    }

    /*
     * Move the t-shirt by specified offsets.
     */
    public void move(double xOffset, double yOffset) {

        bodyFrame.move(xOffset,yOffset);
        sleeves.move(xOffset,yOffset);
        sleeveFrame.move(xOffset,yOffset);
        body.move(xOffset,yOffset);
        neck.move(xOffset,yOffset);
        neckFrame.move(xOffset,yOffset);

    }

    /*
     * Returns true if the t-shirt contains the point;
     * false otherwise
     */
    public boolean contains(Location pt) {

        return bodyFrame.contains(pt) ||
        sleeveFrame.contains(pt) ||
        neckFrame.contains(pt);
    }

    /*
     * Change color of t-shirt to newColor.
     */
    public void setColor(Color newColor) {

        body.setColor(newColor);
        sleeves.setColor(newColor);
        neck.setColor(newColor);

    }

    /*
     * Put t-shirt in front of other objects on canvas.
     * 
     * Note that this must be done in a particular order to ensure
     * that the t-shirt appears as intended (appropriate parts
     * obscured behind others).  We use the same order in which
     * we originally constructed the t-shirt.
     */
    public void sendToFront() {

        bodyFrame.sendToFront();
        sleeves.sendToFront();
        sleeveFrame.sendToFront();
        body.sendToFront();
        neck.sendToFront();
        neckFrame.sendToFront();

    }

    /*c
     * Move the t-shirt to a new upper-left coordinate position. 
     */
    public void moveTo(double x, double y) {

        double dx, dy;
        dx = x - sleeves.getX();
        dy = y - neck.getY();

        this.move(dx, dy);
    }    

    /*
     * Move t-shirt back to starting position.
     */
    public void reset() {

        this.moveTo(startX, startY);

    }
}