import objectdraw.*;
import java.awt.*;
// Andrea Danyluk, February 8, 2000
// Implements a laundry sorting simulator.
// When the applet begins, three squares are displayed, representing laundry
// baskets for white, dark, and colored laundry. A square is also displayed,
// representing a swatch of fabric. The initial swatch is white.
// The user should drag the swatch, using the mouse, to the laundry basket in which
// it belongs. If the user chooses correctly, a new swatch is generated with random color
// If the user chooses incorrectly (or drops the swatch somewhere other than a basket),
// the original swatch remains in place.
public class LaundrySorterApplet extends WindowController
{
private static final int BASKET_X_POSITION = 20; // x-anchor for laundry baskets
private static final int BASKET_Y_POSITION = 100; // y-anchor for laundry baskets
private static final int BASKET_SPACING = 20; // spacing between baskets
private static final int BASKET_SIZE = 60; // width and height of each basket
private static final Location SWATCH_POSITION = new Location(60, 20); // swatch position
private static final int SWATCH_SIZE = 40; // width and height of swatch
// position of messages to indicate number of swatches correct and incorrect
private static final RIGHT_TEXT_POSITION = new Location(20, 200);
private static final Location WRONG_TEXT_POSITION = new Location(100, 200);
// color ranges
static final int DARK_COLORS = 330; // any RGB sum less than 230 is dark
private static final int WHITE_COLORS = 600; // any RGB sum greater than 600 is white
private FilledRect swatch; // the swatch
private FramedRect swatchFrame; // the swatch frame
private FramedRect targetBasket; // basket into which swatch should be placed
private FramedRect whites, darks, colors; // the three baskets
private int rightCounter = 0; // number sorted correctly
private int wrongCounter = 0; // number sorted incorrectly
private Text rightCountText, wrongCountText;
RandomIntGenerator rGen = new RandomIntGenerator(0,255);
private boolean draggingIt = false; // whether the swatch has been picked up
private initialPoint;
public void begin()
{
// construct the laundry baskets
whites = new FramedRect(BASKET_X_POSITION, BASKET_Y_POSITION,
BASKET_SIZE, BASKET_SIZE, canvas);
darks = new FramedRect(BASKET_X_POSITION+BASKET_SIZE+BASKET_SPACING,
BASKET_Y_POSITION, BASKET_SIZE, BASKET_SIZE, canvas);
colors = new FramedRect(BASKET_X_POSITION+2*BASKET_SIZE+2*BASKET_SPACING,
BASKET_Y_POSITION, BASKET_SIZE, BASKET_SIZE, canvas);
// generate labels for the baskets
new Text("whites", BASKET_X_POSITION+15, BASKET_Y_POSITION+20, canvas);
new Text("darks ", BASKET_X_POSITION+BASKET_SIZE+BASKET_SPACING+15,
BASKET_Y_POSITION+20, canvas);
new Text("colors", BASKET_X_POSITION+2*BASKET_SIZE+2*BASKET_SPACING+15,
BASKET_Y_POSITION+20, canvas);
// set up the swatch and its target basket
swatch = new FilledRect(SWATCH_POSITION, SWATCH_SIZE, SWATCH_SIZE, canvas);
swatch.setColor(Color.white);
swatchFrame = new FramedRect(SWATCH_POSITION, SWATCH_SIZE, SWATCH_SIZE, canvas);
targetBasket = whites;
// display initial counters of number correctly and incorrectly sorted
rightCountText = new Text(" correct = 0", RIGHT_TEXT_POSITION, canvas);
wrongCountText = new Text("incorrect = 0", WRONG_TEXT_POSITION, canvas);
}
public void onMousePress(Location point)
// picks up the swatch if mouse depressed on it
{
if (swatch.contains(point))
{
draggingIt = true;
initialPoint = point;
}
}
public void onMouseDrag(Location point)
// drags the swatch (if it's been picked up)
{
if (draggingIt)
{
swatch.move(point.getX() - initialPoint.getX(),
point.getY() - initialPoint.getY());
swatchFrame.move(point.getX() - initialPoint.getX(),
point.getY() - initialPoint.getY());
initialPoint = point;
}
}
public void onMouseRelease(Location point)
// determines whether swatch is dropped in its corresponding basket
// if so, generates a new swatch
{
if (draggingIt)
{
// drop it and be ready to pick up again
draggingIt = false;
swatchFrame.moveTo(SWATCH_POSITION);
swatch.moveTo(SWATCH_POSITION);
// determine if swatch was placed correctly
if (targetBasket.contains(point))
{
rightCounter = rightCounter + 1;
rightCountText.setText("correct = " + rightCounter);
// generate a new color swatch
int redness = rGen.nextValue();
int greenness = rGen.nextValue();
int blueness = rGen.nextValue();
swatch.setColor(new Color(redness, greenness, blueness));
int colorSum = redness + greenness + blueness;
if (colorSum < DARK_COLORS)
{
targetBasket = darks;
}
else if (colorSum <= WHITE_COLORS)
{
targetBasket = colors;
}
else
{
targetBasket = whites;
}
}
else
{
wrongCounter = wrongCounter + 1;
wrongCountText.setText("incorrect = " + wrongCounter);
}
}
}
}