Computer Science 120
Introduction to Programming
Spring 2012, Siena College
Drag10ShirtsNicer Demo
A working demo of Drag10ShirtsNicer will appear below. Click inside the applet to interact with it.
Drag10ShirtsNicer BlueJ Project
Click here to download a BlueJ project for Drag10ShirtsNicer.
Drag10ShirtsNicer Source Code
The Java source code for Drag10ShirtsNicer is below. Click on a file name to download it.
import objectdraw.*; import java.awt.*; /* * Example Drag10ShirtsNicer * Drag one of 10 shirts around window, now with fixed colors and * a 2D layout. * * Jim Teresco, Siena College, CSIS 120, Spring 2011 * * $Id: Drag10ShirtsNicer.java 1577 2011-03-28 02:10:20Z terescoj $ */ public class Drag10ShirtsNicer 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 X_OFFSET = 90; private static final int Y_OFFSET = 150; // how many shirts to draw private static final int NUM_ROWS = 2; private static final int NUM_COLS = 5; private static final int NUM_SHIRTS = NUM_ROWS * NUM_COLS; // T-shirts on the screen -- an array! private TShirt[] shirts; // array of colors with initializers private Color[] shirtColors = { Color.blue, Color.red, Color.green, Color.yellow, Color.cyan, Color.black, Color.white, Color.gray, Color.magenta, Color.orange }; // 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 proper number of columns int nextShirtY = ITEM_TOP; for (int rowNum = 0; rowNum<NUM_ROWS; rowNum++) { int nextShirtX = ITEM_LEFT; for (int colNum = 0; colNum<NUM_COLS; colNum++) { int shirtNum = rowNum*NUM_COLS + colNum; shirts[shirtNum] = new TShirt(nextShirtX, nextShirtY, canvas); shirts[shirtNum].setColor(shirtColors[shirtNum]); nextShirtX = nextShirtX + X_OFFSET; } nextShirtY = nextShirtY + Y_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; } }