Computer Science 120
Introduction to Programming
Spring 2012, Siena College
Drag10Shirts Demo
A working demo of Drag10Shirts will appear below. Click inside the applet to interact with it.
Drag10Shirts BlueJ Project
Click here to download a BlueJ project for Drag10Shirts.
Drag10Shirts Source Code
The Java source code for Drag10Shirts is below. Click on a file name to download it.
import objectdraw.*; import java.awt.*; import java.util.ArrayList; /* * Example Drag10Shirts * * Drag one of 10 shirts (in an ArrayList) around window. * * Jim Teresco, Siena College, CSIS 120, Spring 2011 * Added ArrayLists, Spring 2012 * * $Id: Drag10Shirts.java 1837 2012-03-24 13:08:01Z terescoj $ */ public class Drag10Shirts 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 SND_OFFSET = 90; // how many shirts to draw private static final int NUM_SHIRTS = 10; // T-shirts on the screen -- an ArrayList! private ArrayList<TShirt> shirts; // 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 ArrayList<TShirt>(); // now create the shirts -- with random colors for now RandomIntGenerator colorGen = new RandomIntGenerator(0, 255); int nextShirtPos = ITEM_LEFT; for (int shirtNum = 0; shirtNum<NUM_SHIRTS; shirtNum++) { TShirt shirt = new TShirt(nextShirtPos, ITEM_TOP, canvas); shirt.setColor(new Color(colorGen.nextValue(), colorGen.nextValue(), colorGen.nextValue())); shirts.add(shirt); nextShirtPos = nextShirtPos + SND_OFFSET; } selectedShirt = shirts.get(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<shirts.size(); shirtNum++) { TShirt shirt = shirts.get(shirtNum); if (shirt.contains(point)) { selectedShirt = shirt; 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<shirts.size(); shirtNum++) { shirts.get(shirtNum).reset(); } dragging = false; } }
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 = 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); } }