Laundry.java
import objectdraw.*;
// A piece of laundry of an arbitrary shape.
public interface Laundry {
// Move the laundry relative to its current position
// Parameter:
// xOffset - distance to move horizontally
// yOffset - distance to move vertically
public void move( double xOffset, double yOffset );
// Move the laundry to a specific position:
// x - x coordinate
// y - y coordinate
public void moveTo( double x, double y );
// Remove the laundry from the display
public void hide ();
// Return true if the point is in the laundry
// Parameters:
// pt - the point to test for containment
public boolean contains( Location pt);
// Return the sum of the redness, blueness, and greenness
// of the laundry item.
public int colorValue( );
}
LaundryBasket.java
import objectdraw.*;
import java.awt.*;
// A class of objects that act a bit like laundry
// baskets
public class LaundryBasket {
// Constant controlling basket sizes
private static final int BASKETSIZE = 50;
// Constants controlling text positioning
private static final int TEXTDOWNSET = 30;
private static final int TEXTINSET = 5;
// boundary of basket
private FramedRect box;
// Create a new laundry basket
//
// x,y -- location of basket's upper left corner
// label -- text to display within basket
// canvas - the drawing canvase
public LaundryBasket( double x, double y, String label,
DrawingCanvas canvas) {
box = new FramedRect( x, y, BASKETSIZE, BASKETSIZE, canvas);
new Text(label, x + TEXTINSET, y + TEXTDOWNSET,
canvas).setFontSize(13);
}
// Test to see if the basket contains a point
public boolean contains( Location point ) {
return box.contains(point);
}
}
LaundrySorter.java
import objectdraw.*;
import java.awt.*;
/**
* A laundry sorting game. The user drags a shirt to the proper
* "washing machine" according to the shirt's color.
*/
public class LaundrySorter extends WindowController {
// Constants controlling basket locations
private static final int BASKETTOPS = 120;
private static final int BASKETLEFT = 20;
private static final int BASKETOFFSET = 100;
// Constants controlling shirt's position and size
private static final int ITEMLEFT = 115;
private static final int ITEMTOP = 10;
// Location where score should be displayed
private static final int SCORELINE = 200;
private static final int SCORELEFT = 40;
//The piece of laundry to be sorted.
private Laundry item;
// baskets for white, dark, and color washing machines.
private LaundryBasket white;
private LaundryBasket dark;
private LaundryBasket colors;
// The basket corresponding to shirt's color
private LaundryBasket correct;
// Determines if next laundry should be a tshirt or a pair of pants
private RandomIntGenerator laundryGenerator = new RandomIntGenerator (1,2);
// Previously noted position of mouse cursor
private Location lastPoint;
// counters to measure trainees accuracy
private int numsorted, mistakes;
// Display of current result
private Text scoreDisplay;
// Initializes the applet by constructing all the objects.
public void begin(){
white =new LaundryBasket(BASKETLEFT + 0*BASKETOFFSET,
BASKETTOPS,"whites",canvas);
dark =new LaundryBasket(BASKETLEFT + 1*BASKETOFFSET,
BASKETTOPS,"darks",canvas);
colors=new LaundryBasket(BASKETLEFT + 2*BASKETOFFSET,
BASKETTOPS,"colors",canvas);
item= new Tshirt(ITEMLEFT,ITEMTOP,canvas);
numsorted = 0;
mistakes = 0;
scoreDisplay = new Text("Correct: " + numsorted +
" Incorrect: " + mistakes,
SCORELEFT,SCORELINE,canvas);
scoreDisplay.setFontSize(13);
}
// Whenever mouse is depressed, note its location
public void onMousePress(Location point) {
lastPoint = point;
}
// If mouse is dragged from a position within the item,
// move the item with it
public void onMouseDrag(Location point) {
if ( item.contains(lastPoint) ) {
item.move( point.getX() - lastPoint.getX(),
point.getY() - lastPoint.getY());
lastPoint = point;
}
}
// Checks if the item has been place in the correct basket
public void onMouseRelease(Location point){
if (item.contains(lastPoint)) {
// Determine correct basket
if ( item.colorValue() > 600 ) {
correct=white;
}
else if ( item.colorValue() > 250 ) {
correct=colors;
}
else {
correct=dark;
}
}
// if the object was dragged to the correct basket
if ( correct.contains(point)){
numsorted = numsorted+1;
item.hide();
// Make a new item
if (laundryGenerator.nextValue () == 1) {
item = new Tshirt(ITEMLEFT,ITEMTOP,canvas);
}
else {
item = new Pants(ITEMLEFT,ITEMTOP,canvas);
}
}
else {
mistakes = mistakes + 1;
item.moveTo(ITEMLEFT, ITEMTOP );
}
scoreDisplay.setText("Correct: " + numsorted +
" Incorrect: " + mistakes);
}
}
Pants.java
import objectdraw.*;
import java.awt.*;
// A class that defines a graphical type that looks a bit
// like pants
public class Pants implements Laundry {
private static final double SIZE = 90;
private static final double HIP_WIDTH = SIZE / 3;
private static final double HIP_HEIGHT = SIZE / 3;
private static final double LEG_WIDTH = HIP_WIDTH / 2 - 3;
private static final double LEG_HEIGHT = SIZE - HIP_HEIGHT;
// rectangles that form a border around
private FramedRect leftLegTrim, rightLegTrim, hipTrim;
// rectangles that form the interior color
private FilledRect leftLeg, rightLeg, hips;
// the initial location of the pants
private double startX, startY;
// Current color of the pants
private Color hue;
// Random number generator used to select colors
private RandomIntGenerator generator = new RandomIntGenerator(0,255);
// create a new pair of pants with its upper left corner at (x,y) and with
// a length of size.
public Pants( double x, double y, DrawingCanvas canvas ) {
// create boundary rectangles
leftLegTrim = new FramedRect( x, y + HIP_HEIGHT, LEG_WIDTH,
LEG_HEIGHT, canvas);
rightLegTrim = new FramedRect( x + HIP_WIDTH - LEG_WIDTH,
y + HIP_HEIGHT, LEG_WIDTH,
LEG_HEIGHT, canvas);
hipTrim = new FramedRect( x, y, HIP_WIDTH, HIP_HEIGHT, canvas);
// create interior rectangles
leftLeg = new FilledRect( x+1, y+HIP_HEIGHT-1, LEG_WIDTH-1,
LEG_HEIGHT+1, canvas);
rightLeg = new FilledRect( x+HIP_WIDTH-LEG_WIDTH+1, y+HIP_HEIGHT-1,
LEG_WIDTH-1, LEG_HEIGHT+1, canvas);
hips = new FilledRect( x+1, y+1, HIP_WIDTH-1, HIP_HEIGHT-1, canvas);
// remember the starting location for re-set
startX = x;
startY = y;
setColor();
}
// move the pants by specified offsets.
public void move( double xOffset, double yOffset ) {
hips.move(xOffset,yOffset);
leftLeg.move(xOffset,yOffset);
rightLeg.move(xOffset,yOffset);
hipTrim.move(xOffset,yOffset);
leftLegTrim.move(xOffset,yOffset);
rightLegTrim.move(xOffset,yOffset);
}
// move the pants to a specific position.
public void moveTo( double x, double y) {
move( x - hips.getX(), y - hips.getY() );
}
public void hide() {
hips.hide();
leftLeg.hide();
rightLeg.hide();
hipTrim.hide();
leftLegTrim.hide();
rightLegTrim.hide();
}
// returns true if the pants contains the point; false otherwise
public boolean contains( Location pt) {
return hips.contains(pt) || leftLeg.contains(pt) ||
rightLeg.contains(pt);
}
// pick a new random color for the pants
private void setColor( ) {
hue = new Color( generator.nextValue(),
generator.nextValue(),
generator.nextValue());
hips.setColor(hue);
leftLeg.setColor(hue);
rightLeg.setColor(hue);
}
// get the sum of the components of the pant's color
public int colorValue( ) {
return hue.getRed() + hue.getBlue() + hue.getGreen();
}
}
Tshirt.java
import objectdraw.*;
import java.awt.*;
// A class that defines a graphical type that looks a bit
// like a t-shirt
public class Tshirt implements Laundry {
// constants to define the size of the shirt
private static final double SIZE = 120;
private static final double SLEEVE_WIDTH = SIZE;
private static final double SLEEVE_HEIGHT = 0.2*SIZE;
private static final double BODY_WIDTH = 0.6*SIZE;
private static final double BODY_HEIGHT = (0.65)*SIZE;
private static final double BODY_INSET = 0.2*SIZE;
private static final double NECK_WIDTH = 0.3*SIZE;
private static final double NECK_HEIGHT = 0.06*SIZE;
private static final double NECK_INSET = 0.35*SIZE;
// rectangles that form a border around the shirt
private FramedRect sleeveTrim, bodyTrim;
private FramedOval neckTrim;
// rectangles that form the interior color of the shirt
private FilledRect body, sleeves;
private FilledOval neck;
// Current color of the shirt
private Color hue;
// Random number generator used to select next object
private RandomIntGenerator generator = new RandomIntGenerator(0,255);
// create a new T-shirt with its upper left corner at (x,y) and with
// a width of size.
public Tshirt( double x, double y, DrawingCanvas canvas ) {
// create boundary rectangles
sleeveTrim = new FramedRect( x, y + NECK_HEIGHT/2, SLEEVE_WIDTH,
SLEEVE_HEIGHT, canvas);
bodyTrim = new FramedRect( x + BODY_INSET, y + NECK_HEIGHT/2,
BODY_WIDTH, BODY_HEIGHT, canvas);
// create interior rectangles
sleeves = new FilledRect( x+1, y+NECK_HEIGHT/2+1, SLEEVE_WIDTH-1,
SLEEVE_HEIGHT-1, canvas);
body = new FilledRect( x+BODY_INSET+1, y+NECK_HEIGHT/2+1,
BODY_WIDTH-1, BODY_HEIGHT-1, canvas);
// give it a neck hole
neck = new FilledOval( x + NECK_INSET, y, NECK_WIDTH,
NECK_HEIGHT, canvas);
neckTrim = new FramedOval( x + NECK_INSET, y, NECK_WIDTH,
NECK_HEIGHT, canvas);
// set the interior color
setColor();
}
// move the t-shirt by specified offsets.
public void move( double xOffset, double yOffset ) {
body.move(xOffset,yOffset);
neck.move(xOffset,yOffset);
sleeves.move(xOffset,yOffset);
bodyTrim.move(xOffset,yOffset);
sleeveTrim.move(xOffset,yOffset);
neckTrim.move(xOffset,yOffset);
}
// hide the t-shirt from sight.
public void hide( ) {
body.hide();
neck.hide();
sleeves.hide();
bodyTrim.hide();
sleeveTrim.hide();
neckTrim.hide();
}
// move the t-shirt to a new upper-left coordinate position
public void moveTo( double x, double y) {
move( x - sleeves.getX(), y - neck.getY());
}
// returns true if the t-shirt contains the point; false otherwise
public boolean contains( Location pt) {
return body.contains(pt) || sleeves.contains(pt) || neck.contains(pt);
}
// pick a new random color for the shirt
private void setColor( ) {
hue = new Color( generator.nextValue(),
generator.nextValue(),
generator.nextValue());
body.setColor(hue);
sleeves.setColor(hue);
neck.setColor(hue);
}
// get the sum of the components of the shirt's color
public int colorValue( ) {
return hue.getRed() + hue.getBlue() + hue.getGreen();
}
}