Computer Science 252
Problem Solving with Java
Spring 2014, The College of Saint Rose
Drag10ShirtsArray Demo
A working demo of Drag10ShirtsArray will appear below. Click inside the applet to interact with it.
Drag10ShirtsArray BlueJ Project
Click here to download a BlueJ project for Drag10ShirtsArray.
Drag10ShirtsArray Source Code
The Java source code for Drag10ShirtsArray is below. Click on a file name to download it.
import objectdraw.*; import java.awt.*; import java.util.Random; /* * Example Drag10Shirts * * Drag one of 10 shirts around window. * * Jim Teresco, Siena College, CSIS 120, Spring 2011 * Updated for The College of Saint Rose, CSC 252, Spring 2014 * * $Id: Drag10ShirtsArray.java 2218 2013-10-18 14:06:39Z terescoj $ */ public class Drag10ShirtsArray 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 array! private 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 TShirt[NUM_SHIRTS]; // now create the shirts -- with random colors for now Random colorGen = new Random(); int nextShirtPos = ITEM_LEFT; for (int shirtNum = 0; shirtNum<NUM_SHIRTS; shirtNum++) { shirts[shirtNum] = new TShirt(nextShirtPos, ITEM_TOP, canvas); shirts[shirtNum].setColor(new Color(colorGen.nextInt(256), colorGen.nextInt(256), colorGen.nextInt(256))); nextShirtPos = nextShirtPos + SND_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; } }
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 2218 2013-10-18 14:06:39Z 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); } }