import objectdraw.*; import java.awt.*; public class ColorDragging extends WindowController { private Location lastPoint; private FilledRect rectA, rectB, rectC, rectColorSwatch, rectForColoring, changeColorButton; private boolean draggingSwatch; // Dragging dirty laundry item hint... private int currentSwatchColorIndex; private RandomIntGenerator generator; private static final int RED = 1; private static final int PURPLE = 2; private static final int ORANGE = 3; private static final int BLUE = 4; private static final int CYAN = 5; private static final int YELLOW = 6; private static final int RECT_WIDTH = 100; private static final int RECT_HEIGHT = 50; private static final Location COLOR_SWATCH_LOCATION = new Location(150, 100); public void begin() { generator = new RandomIntGenerator(1, 6); rectA = new FilledRect(25, 200, RECT_WIDTH, RECT_HEIGHT, canvas); rectB = new FilledRect(150, 200, RECT_WIDTH, RECT_HEIGHT, canvas); rectC = new FilledRect(275, 200, RECT_WIDTH, RECT_HEIGHT, canvas); rectColorSwatch = new FilledRect(COLOR_SWATCH_LOCATION, RECT_WIDTH, RECT_HEIGHT, canvas); currentSwatchColorIndex = generator.nextValue(); rectColorSwatch.setColor( whatColor(currentSwatchColorIndex) ); changeColorButton = new FilledRect(100, 20, RECT_WIDTH * 2, 25, canvas); changeColorButton.setColor( Color.lightGray ); Text t = new Text( "Change Swatch Color", 140, 25, canvas ); t.setBold(); t.setColor( Color.blue ); Line hr = new Line(45, 280, 340, 280, canvas); hr.setColor( Color.orange ); hr = new Line(45, 298, 340, 298, canvas); hr.setColor( Color.orange ); new Text("Drag the swatch to any rectangle to change its color!", 50, 280, canvas); t = new Text("Color Swatch", 163, 114, canvas); t.setBold(); } public void onMousePress(Location point) { draggingSwatch = rectColorSwatch.contains(point); if ( rectColorSwatch.contains(point) ) { lastPoint = point; } else if (changeColorButton.contains(point)) { currentSwatchColorIndex = generator.nextValue(); rectColorSwatch.setColor( whatColor( currentSwatchColorIndex ) ); } } public void onMouseDrag(Location point) { if ( draggingSwatch == true ) { rectColorSwatch.move( point.getX() - lastPoint.getX(), point.getY() - lastPoint.getY() ); lastPoint = point; } } public void onMouseRelease(Location point) { FilledRect rectForColoring; if ( draggingSwatch ) { rectColorSwatch.moveTo( COLOR_SWATCH_LOCATION ); if (rectA.contains(point)) { rectForColoring = rectA; } else if (rectB.contains(point)) { rectForColoring = rectB; } else if (rectC.contains(point)) { rectForColoring = rectC; } else { rectForColoring = null; return; } rectForColoring.setColor( whatColor( currentSwatchColorIndex ) ); } } private Color whatColor( int colorIndex ) { if (colorIndex == RED) { return ( Color.red ); } else if (colorIndex == PURPLE) { return ( Color.magenta ); } else if (colorIndex == ORANGE) { return ( Color.orange ); } else if (colorIndex == BLUE) { return ( new Color(0, 0, 255) ); } else if (colorIndex == CYAN) { return ( Color.cyan ); } else if (colorIndex == YELLOW) { return ( new Color(255, 255, 0) ); } else { return Color.black; } } }