import objectdraw.*; import java.awt.*; public class WhatADrag extends WindowController{ // Starting position for t-shirt sleeves private static final int SLEEVES_LEFT = 50, SLEEVES_TOP = 50; // width and height of the t-shirt sleeves private static final int SLEEVES_WIDTH = 100; private static final int SLEEVES_HEIGHT = 25; // starting position for the t-shirt body private static final int BODY_LEFT = SLEEVES_LEFT + SLEEVES_WIDTH/4; private static final int BODY_TOP = SLEEVES_TOP; // width and height of the t-shirt body private static final int BODY_WIDTH = SLEEVES_WIDTH/2; private static final int BODY_HEIGHT = SLEEVES_HEIGHT * 3; // t-shirt to be dragged private FilledRect sleeves, body; // point where mouse was last seen private Location lastPoint; // whether the t-shirt has been grabbed by the mouse private boolean shirtGrabbed = false; // make the box public void begin() { sleeves = new FilledRect(SLEEVES_LEFT, SLEEVES_TOP, SLEEVES_WIDTH, SLEEVES_HEIGHT, canvas); body = new FilledRect(BODY_LEFT, BODY_TOP, BODY_WIDTH, BODY_HEIGHT, canvas); } // Save starting point and whether point was in shirt public void onMousePress(Location point) { lastPoint = point; shirtGrabbed = sleeves.contains(point) || body.contains(point); } // if mouse is in shirt, then drag the shirt public void onMouseDrag(Location point) { if ( shirtGrabbed ) { sleeves.move( point.getX() - lastPoint.getX(), point.getY() - lastPoint.getY() ); body.move( point.getX() - lastPoint.getX(), point.getY() - lastPoint.getY() ); lastPoint = point; } } }