Computer Science 252
Problem Solving with Java
Spring 2014, The College of Saint Rose
Drag4ShirtsArrayList Demo
A working demo of Drag4ShirtsArrayList will appear below. Click inside the applet to interact with it.
Drag4ShirtsArrayList BlueJ Project
Click here to download a BlueJ project for Drag4ShirtsArrayList.
Drag4ShirtsArrayList Source Code
The Java source code for Drag4ShirtsArrayList is below. Click on a file name to download it.
import objectdraw.*; import java.awt.*; import java.util.ArrayList; /* * Example Drag4ShirtsArrayList * * Drag a shirt around window, given choice of red shirt, blue shirt, * green shirt, and yellow shirt, but now stored in an ArrayList. * * Jim Teresco, Siena College, CSIS 120, Spring 2012 * Updated for CSC 252, The College of Saint Rose, Spring 2014 * * $Id: Drag4ShirtsArrayList.java 2343 2014-04-06 02:17:18Z terescoj $ */ public class Drag4ShirtsArrayList 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; // our list of T-shirts on the screen // note also that we have a new "import" at the top for the 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, store them in an ArrayList public void begin() { // first, construct our ArrayList that can hold TShirt objects shirts = new ArrayList<TShirt>(); // now, create each shirt and add it to the ArrayList TShirt shirt = new TShirt(ITEM_LEFT, ITEM_TOP, canvas); shirt.setColor(Color.red); shirts.add(shirt); shirt = new TShirt(ITEM_LEFT + SND_OFFSET, ITEM_TOP, canvas); shirt.setColor(Color.blue); shirts.add(shirt); shirt = new TShirt(ITEM_LEFT, ITEM_TOP + SND_OFFSET, canvas); shirt.setColor(Color.green); shirts.add(shirt); shirt = new TShirt(ITEM_LEFT + SND_OFFSET, ITEM_TOP + SND_OFFSET, canvas); shirt.setColor(Color.yellow); shirts.add(shirt); selectedShirt = shirt; // last created shirt "on top" at start (it has to // be one of them, doesn't really matter which) } // Whenever the mouse is pressed, note its location // and which (if any) shirt the mouse is on. public void onMousePress(Location point) { lastPoint = point; // start out assuming that we are not on top of any shirt, which // means we will not be dragging. dragging = false; // test the selected shirt (which is on top) first. if (selectedShirt.contains(point)) { dragging = true; } else { // otherwise, look at each shirt in the ArrayList in turn, see if // it contains the point. If we find one, we set it as the // selected shirt so it will be dragged around and set dragging to true 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 all 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 2218 2013-10-18 14:06:39Z 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); } }