CPSC 124, Fall 2005
Assignment 2: Mosaic Draw


FOR THIS PROGRAMMING ASSIGNMENT, we move away from the idea of writing a single main program that is executed from beginning to end. Instead, you will be working on an event-driven program, where the user controls what happens next by generating events such as mouse actions and menu commands. In this program, you will not have to design the program as a whole. You just have to write some methods to handle the events.

I have written a version of the program that implements many more features than you will need to do to complete the assignment. You can find that version in the executable jar file MosaicDrawComplete.jar. Here is an applet version of the same program:

The program lets the user use the mouse to draw "mosaics." To make things interesting, as the user drags the mouse, there is some variation in the color. The user can control the amount of variation using the "No Randomness", "Some Randomness", "Moderate Randomness", and "Much Randomness" commands in the "Color" menu. The "Clear" command in the "Control" menu will reset all the squares of the mosaic to black; the "Fill" command will color all the squares; and the "Frame" command fills just the squares that border the edge of the mosaic. You will have to implement these seven commands -- doing so will earn you up to a B on the assignment.

The "Set Color" command in the "Color" menu lets the user select the current drawing color. This command is already implemented for you, so you won't have to worry about doing it. When you run the program as an application instead of as an applet, you will find "Save PNG Image" and "Save JPEG Image" commands that let the user save the image that she has created to a file; these commands are also already implemented for you. A version of the program that implements all the commands that I have mentioned so far can be found in MosaicDrawComplete_B_Level.jar. This is what your program will look like when you have done enough work to get up to a "B" for the assignment.

To get higher than a "B", you must implement one or two additional features. The full version of the program, as demonstrated in the applet above, gives you some ideas for the types of features that you can implement. Before you decide what features you will implement, you are required to consult with me about your plans. There is a possibility of getting extra credit, if you implement more than two extra features.

Remember that this is an individual assignment. You should not give help to or receive help from any other member of the class. You should not discuss your program or show your work to other people in the class. You can get help from me or from the computer science TA's, but not from anyone else. You should not copy code from the Internet or from any other source.


To begin the assignment, you should start a new Eclipse project. Import all the files folder named mosaic_draw in the /classes/f05/cs124/assg2 directory. You will find five files in that direction. All the files are in a package named mosaic_draw. To do the basic assignment, the only file that you will need to modify is Controller.java. To add extra features, you will probably have to modify Menus.java in order to add new menu commands.

Working with events is all about keeping track of "current state". An event can change the current state; the current state can affect what happens when an event occurs. For example, in the MosaicDraw program, the current drawing color is part of the state. The current drawing color can change when the "Set Color" command occurs. When the user draws, the current drawing color tells what color the squares should be set to. Current state is stored in member variables. The current drawing color is stored in three member variables named currentRed, currentGreen, and currentBlue.

When you implement randomness in the color, you will need a member variable to keep track of how much randomness to add to the color. The value of this variable changes when the user selects one of the "Randomness" commands. (The actual code goes in the doMenuCommand method, which is called when the user selects a menu command.) The randomness setting is used in the applyColor method, which is called when the color of a square needs to be set. You can implement randomness by adding a random integer to each of the color components that you use to set the color.

To implement "Fill", "Clear", and "Frame", you will have to modify the doMenuCommand method to handle these commands. All of these commands are relatively easy to implement.

The assignment is due on Wednesday, November 2.