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.
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. 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 can make the first item always have color white (or you can be more ambitious and select the color randomly). 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, make up constants (private static final ...) for all the relevant information. This will make it easier to change things around and also make 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 Location. 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 width 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.
(Pay attention to the following as it will make your code much, much easier!)
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 - let's call it correctBasket - of type FramedRect.
When you generate a new item (in either begin or onMouseClick), you will associate correctBasket 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 correctBasket 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).
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 eliminate this problem.
We have provided a class in the objectdraw package which helps to generate random numbers. This will be used to determine the next color for the item.
Suppose you wish to generate random integers in the range from m to n (where m <= n). Let generator be an instance variable declared to be of type RandomIntGenerator. Create a new random number generator from class RandomIntGenerator, and assign it to generator:
generator = new RandomIntGenerator(m,n);
Now every time you want a new random integer in that range, simply invoke the method
which will return a randomly chosen integer between m and n (inclusive). Thus to generate integers between 1 and 3 (say, standing for white, dark, and colored), use
If n is the value of generator.nextValue(), make the color of the item be Color.white if n is 1, Color.black if n is 2, and Color.red if n is 3.
Bring to lab a list of all of the constants that you will be using in your program, giving each an intuitive name (remember that constants in Java should be written with all capital letters). Be sure to include a sketch showing where all of the boxes will be drawn as a consistency check for your constants. For each of the methods of the class for part 1, please write a description in English of what should happen in that method. It should include sufficient detail that you can easily translate it into Java. E.g., draw filled and framed rectangles at START_LAUNDRY with width ...and height .... When describing what happens in methods like onMouseClick, you know they will only be called when the mouse is clicked, so describe what happens in response to that click.
Try to convince yourself that your design is correct before you come to lab, as that will make your lab work much simpler. In particular, the trickiest part of the lab is using our hint above to make it easy to determine whether the user clicked in the correct basket, so look at this very carefully.
Part 1 |