Reference: Chapter 6 of the Swing Tutorial book and
Radio Buttons of Chapter 7
The assignment: Write a program that allows the user to select a rectangular area within a blank area using the mouse. The selected area is then drawn as a filled rectangle in one of three colors: red, green, or blue. The color to be used is set by three radio buttons. The interface should look something like (assuming that red is the current drawing color):
+-----------------------------------------------------+ | +--------------------------------------+ | | (*) Red | | | | ( ) Green | < blank area > | | | ( ) Blue | | | | +--------------------------------------+ | +-----------------------------------------------------+Notes: the most useful code we have seen so far is in the SelectionDemo program. Here is the SelectionDemo program file. The RadioButtonDemo program may also be useful. Here is the RadioButtonDemo program file (if you want to actually run it, you need to get the .gif files of the animals - they can be found at: Animal.gif files . Pressing a radio button could set a global state variable (currentColor ?) that specifies which color is currently being used, or you can iterate through the buttons to find out which one is selected (it is probably a good idea to have one of the buttons selected/set initially).
Extra Enhancements:
1. (1 point) Add a "Filled" toggle button (on top of the
Red, Green, Blue buttons, or ..), such that when it is selected the
rectangle is drawn as filled, and when de-selected only the unfilled
outline is drawn.
Here is a sample program that uses a JToggleButton:
ScrollDemo prgram files.
2. (1 point) Keep track of the rectangles that have been
drawn by the user (up to some maximum number, say 100) and redraw
them as needed so that the user can build up a "picture" of
different colored rectangles. One way to do this is to make
an array of "MyRectangle" objects each of which has Rectangle
information and a color (and a filled/unfilled boolean if you
did part 1.).
Notes: Extra credit points are usually
harder to earn than ordinary points.
Also, you should implement the regular assignment features first, before
trying to implement any extra credit features. The instructor and TA
will be quite generous with their time in helping you debug the regular
assignment features, but you should expect to implement the extra credit
mostly "on your own".
What to turn in:
Turn in the following items from the
Computer Science Lab Report Format:
1. (1 point) The Basic Information (your name, class, section,
TA's name, assignment number, and date) can be on a separate cover sheet or as
(highlighted) comments at the top of your main program file.
9. (up to 2 points if done) Document the Extra Credit part
of your program if you did it (by coded/hand-written comments and highlighting
-- as usual).
11. (4 points) Include the program listings.
This listing should show good style,
be appropriately commented, have the important/new parts
highlighted, and include handwritten explanations if it helps the
reader's understanding of the code.
13./14. (5 points)
Do a demo of your modified program for the TA that demonstrates that
the program works correctly.