Part 1

The simulator should begin with three wash baskets on the screen (for
our purposes they can just be rectangles or squares). One is labeled
"whites", one "darks", and the last "colors". An image showing
the kind of display we have in mind appears below.

When the simulation
begins, a color swatch will appear on the screen. The user ("laundry
trainee") should then click in the corresponding basket. If the user
is correct, the program should randomly select a new color for the
next item and display it on the screen. For this part of the assignment
you may just select among the three colors
Color.white, Color.red, and Color.black
when creating items of clothing.
If the user clicks on an incorrect basket, the
original item remains in position for another try.

The image above will be a working version of the simple
laundry sorter if your web browser supports Java 1.1.
You should experiment with
this demonstration version after you finish reading Part 1 to make
sure you understand exactly what we have in mind.

One odd feature of the simple interface that may bother you a bit is
a result of the fact that the program selects laundry items randomly.
Because the selection is truly random it sometimes picks the same
color twice in a row. When this happens and you click on the correct
basket for the first item you will get the feeling that the program
ignored you. Even though it has actually displayed a new item, the
new item looks just like the old one, so you may think nothing
changed. Don't let this trick you into thinking that your version of
the program (or ours) isn't working correctly. The more advanced
interface in Part 2 includes counters in the display that make it
clearer whether the user succeeded.

You now have an empty project to which you should add a Java file for your class:

Click on "Laundry" in the Package Explorer panel on
the left side of the Eclipse window.

Open the File Menu. Select New and then Class.

Enter "Laundry" as the Name of the class.

Enter "WindowController" as the Superclass.

Click Finish.

You should now have a Laundry.java file inside the
Laundry project. Be sure to modify the
file so that it starts with import objectdraw.*;
Later, when you start working the colors, you will also need
to import java.awt.*.

You will need to design an extension of the WindowController
class which will display the wash baskets and the item to be sorted.
Begin by laying out where all the items go on some graph paper. (We
recommend using a window that has height and width of 400 pixels.) The
picture should look more or less like the one above.

When the program begins, place all the wash baskets (with labels) on
the screen. Then, add the item of clothing that is to be sorted. For
simplicity you might as well always make the first item have color
white. The item should actually consist of two rectangles, a FilledRect which is the appropriate color and a FramedRect
which is the same size, but lays on top of the filled rectangle to
form a border (otherwise it will be awfully difficult to see a white
item!)

When you lay out the wash baskets and item, define constants
(private static final ...) for all the relevant
information. This makes it easier to change the layout and
also makes your program much, much easier to read (presuming you give
constants good names). Constant names are by convention written with
all capital letters and underscores, e.g. THIS_IS_A_CONSTANT. Your
constants may be (and often should be) more complex objects like
Locations. You can initialize constants with the results of a
constructor:

private static final Location SOME_LOCN = new Location(100,200);

Remember that you may NOT have constants whose definition uses
canvas (e.g., no FramedRect constants).

The widths and heights of wash baskets and the item to be sorted,
coordinates of the upper left corner of each of these, etc., are all
good candidates for constants.

After writing the code to draw the initial display, it would be good
to run it to see if it does what you expect. As with last week, you
must create a configuration to run your program. These instructions
are basically the same as last week, except that you will use a
different name and different dimensions.

Open the Run menu.

Select "Run..."

Select "Java Applet" from the Configurations list.

Click the New button at the bottom left.

Make sure that the Name field at the top, the Project, and the
Applet class all say Laundry.

Once you have done the layout and figured out how to generate new
items, all you have to do is to write the code for the method
onMouseClick. Because you may be generating the item in one
method (begin) and checking to see if the user clicked in the
appropriate basket in a different method (the onMouseClick
method), you will need to associate some information with an instance
variable that will enable onMouseClick to determine which is the
correct basket. An appropriate way to do this is to use an instance
variable of type FramedRect.

When you generate a new item (in either begin or
onMouseClick), you will associate the new variable with the
rectangle/basket in which an item of its color should be placed. That
way when the user clicks on a basket, onMouseClick can simply
check to see if the rectangle currently associated with the instance
variable contains the point where the mouse was clicked. Then,
onMouseClick will either select a new color for the item (if the
user was correct) or wait until the user clicks again (if incorrect).

Because your program only uses one laundry
item at a time, you might as well just recycle it - reusing the same
rectangle for each laundry item. Simply change its color rather than
creating a new rectangle. In general it is a good strategy to reuse
objects rather than creating new ones when possible, as this generally
uses less time and does not clutter memory.

To pick a random color,
use the RandomIntGenerator class as described in the textbook
(Section 2.9). Since there are 3 colors to select from, you should
create your random number generator to return random numbers in the
range of 1 to 3 and associate each of those values with one of the
colors the laundry may have (Color.white,
Color.black, and Color.red).