CS 5 Fall 2009
Short Assignment #9
Due Wednesday, October 21

Your assignment is to modify the Checkerboard applet from Short Assignment 8 so that whichever square you click on flashes between its normal color and yellow, with the change to or from yellow occurring every half second (i.e., every 500 milliseconds).

You may start from either of my solutions to Short Assignment 8: Checkerboard.java or OscillatingCheckerboard.java. Naturally, you should cite whatever sources you use, including my solutions.

You can play with my implementation if you like:

Here are some design ideas that you might find useful. As in Short Assignment 8, you should define constants (i.e., final variables) as appropriate.

As with the ClickAMac applet from lecture, use an instance variable to remember where the mouse was clicked; set this instance variable in the mouseClicked method.

To implement the flashing behavior, you'll of course need to create a Timer object and have the appropriate actionPerformed method called every 500 milliseconds. Make a boolean instance variable that says, if a square has been clicked, whether that square is being drawn in its normal color or in yellow. Whenever the Timer rings, negate this boolean variable. In your paint method, draw the entire checkerboard. After you've drawn the checkerboard, if a point has been clicked, and if the boolean instance variable indicates that you should draw the square in yellow where you clicked, do so. There will be a brief moment after the checkboard is drawn and before the yellow square is drawn, but it is so brief that nobody will ever notice.

You are free to make inner classes for listeners or to let the applet itself act as a listener.

Given a Point—let's say that it's referenced by clickPoint—how do we determine the row and column numbers of the square containing this point? This is pretty easy. Given that the squares are 30 pixels in each dimension, the row number is clickPoint.y / 30 and the column number is clickPoint.x / 30. There are a couple of things to note:

Turn in a listing of your Java file and a screen shot of your checkerboard with a yellow square under the cursor. You do not have to print in color.

To take a screen picture on Mac OS X, while you have the graphic output on your screen, use the Grab utility if you have it. On my Mac, it's in the Utilities folder within the Applications folder. If you don't have Grab, then press Command-Shift-4. That is, simultaneously hold down the Command key (the one with the apple and/or the butterfly on it), the Shift key, and 4. You'll see a funny icon. Then drag the mouse over the portion of the screen that you want a picture of. The picture will be saved as a PDF file on your desktop.

On the PC, in the Applet Viewer window, click menu "Applet" and then choose "Print... ." A "Print" dialog box will show up. Just press "OK."


Back to Short Assignments
Thomas H. Cormen <thc@cs.dartmouth.edu>
Last modified: Sun Oct 18 17:04:58 2009