CS 124, Fall 2011
Lab 14: Partially Full Arrays

This lab is due at the end of the lab period. You should turn in your work before you leave the lab. If you have extra time during the lab period, you can work on your final project.

To begin, create a project named lab14, and copy the file Rectangles.java from /classes/cs124 into your lab14 src folder.

For this final lab of the semester, you will work with a "partially full array," as covered in Section 7.3. You will add a partially full array to a program, Rectangles.java, that is mostly complete. In fact, it would be easier to use an ArrayList in this program than a partially full array. Essentially, you will have to implement some of the functionality that is built into an ArrayList. That is, you want to implement the ability to store a varying number of items in a list and to expand the list to any required size.

The program when finished will allow the user to add any number of rectangles, of various sizes and colors, to a drawing area. The user can also drag the rectangles around in a drawing area. Here is an applet version of the completed program:

Right-click or shift-click on the drawing area to add a new rectangle. Drag rectangles using the left mouse button. Use the "Set Color" button and the slider to control the color and size of the rectangles.

Your starting point for the lab is a program that does everything that the complete version does, except that it only allows one rectangle. Instead of adding a new rectangle, right-clicking will move the one rectangle to the point where the mouse is pressed. The file Rectangles.java contains a main() routine, so you can run it as a program. You should run the program and try it out before starting to work on the lab. Try the slider and the "Set Color" button.

There are six places in Rectangles.java that are marked with the notation TODO. These are the six points where you will have to make changes. (You can find the TODO's by clicking small rectangles in the right-hand margin of the editor pane.)

Take a minute to look through Rectangles.java. Note that there is a nested class, Rect, that represents one rectangle. The display area and event listeners are defined using anonymous inner classes. All the changes that you need to make are in the event-handling methods and in the paintComponent method for the display class.

The first change you should make is on line number 34: Delete the definition of the instance variable private Rect rect, and replace it with an array of Rect and a counter to keep track of the number of items in the array. Initially, the length of the array should be 1:

private Rect[] rects = new Rect[1];
private int rectCount = 0;

As soon as you do this, there will be several errors in the program, since rect is no longer defined. These errors will go away as you replace references to rect with code that manipulates the partially full array. The changes that you have to make include:

That's the required part of the lab. My version of the program makes a few more changes. If you have the time and inclination, you can try them as well for maybe just a little bit of extra credit: When the user starts to drag a rectangle:

  1. Move that rectangle to the end of the list so that it will appear on top of all the other rectangles.
  2. Set currentColor and the color of the color patch to match the color of the selected rectangle.
  3. Set the value of the slider to match the width of the selected rectangle.

Remember to turn in your work
by copying lab14 into your homework folder
before the end of the lab period.