import objectdraw.*; import java.awt.*; /** * Lab 2 - Dirty Laundry (rectangles, colors, and drag/drop) * Design of Part 1 only *

* A game in which the player must decide if an item * is "white", "dark" or "colored", and drag it to the * appropriate basket. The program generates color * swatches at random and determines whether or not a * swatch has been dropped into the correct basket. *

* Suggested window size: 400x200 * * @author CS 51 Instructors */ public class LaundrySorterApplet extends WindowController { // x,y Location of the upper left corner of the basket public static final int BASKET_X_POSITION = 20; public static final int BASKET_Y_POSITION = 100; // width and height of the basket public static final int BASKET_SIZE = 60; // spacing between baskets public static final int BASKET_SPACING = 20; // x,y Location of the upper left corner of the swatch public static final Location SWATCH_POSITION = new Location(60, 20); // width and height of the swatch public static final int SWATCH_SIZE = 40; // the swatch of laundry and its frame private FilledRect swatch; private FramedRect swatchFrame; // the basket in which the swatch should be placed private FramedRect correctBasket; // the three laundry baskets private FramedRect whites, darks, colors; // generator to get codes for the three possible colors private RandomIntGenerator colorGen = new RandomIntGenerator(1,3); // code for current swatch color private int swatchCode = 1; /** * Initialization method, called when applet starts *

* 1. Place all baskets on the screen. * 2. Choose a color for the first item to be sorted. * 3. Place the first item to be sorted on the screen. */ public void begin() { // create whites, darks, and colors laundry baskets using above // constants, and create labels showing which basket is which // Construct the swatch (& its frame) and set the swatch to white // Make the correctBasket be the whites basket } /** * Event handler, called when mouse button is "clicked" (pressed and released) * (note that a click event is accompanied by press and release events) *

* 1. determine whether or not click was in the correct basket. * 2. if so, choose a new color and update the item to be sorted. * * @param point mouse coordinates at time of click */ public void onMouseClick(Location point) { // if correctBasket contains the point then // Use colorGen to give a new value to swatchCode // if swatchCode is 1 then // set color of swatch to white // make correctBasket to whites basket // else if swatch is 2 then // set color of swatch to red // set correctBasket to to colors basket // else // set color of swatch to black // set correctBasket to darks basket } }