CIT 591 ArrayDisplay 2.0
Fall 2003, David Matuszek

This is a revised version of the ArrayDisplay class, with added functionality.

For Assignment 5 (Traffic Jam), you can print your results on System.out if you prefer. However, here is a simple class that, if added to your program, will make your output simpler and more interesting.

To create an object of type ArrayDisplay, you must first define a two-dimensional array of Objects (of any sort). For example, you might have a two-dimensional array of Vehicles. Use this array as a parameter to the ArrayDisplay constructor. For example:

Vehicle[][] vehicles = new Vehicle[10][15];
// ... put some vehicles into the array ...
ArrayDisplay view = new ArrayDisplay(vehicles);

It is better to put something into the array before using it to create an ArrayDisplay. Any null values in the array will be ignored (not displayed) by the ArrayDisplay class; other values will be displayed by calling their public String toString() method. (If you don't supply this method, the toString() method inherited from Object will be used, with unpleasant results.) Do not create more than one ArrayDisplay for your array.

 

New in version 2.0:

There is now a second ArrayDisplay constructor that takes two Object[][] parameters. The first is as described above, while the second is a background array that is drawn first, in a lighter color, before the main array is drawn on top of it.

The purpose of this second array is to give you a way to display destinations.

When you make a change in your array, it is not automatically displayed; you have to call the repaint() method of your ArrayDisplay object to make the changes appear on the screen. For example:

// ... make some changes to the vehicles array ...
view.repaint();

To make sure that your changes don't fly by too fast for the eye to follow, there is a built-in one second (1000 millisecond) delay between each change to the display.

ArrayDisplay has two additional (trivial) features. You can tell your ArrayDisplay object to display a message with displayMessage(String), for example:

view.displayMessage("Going OK so far.");

The message field is initially pretty short. If you want a longer field, just resize the window.

Finally, the ArrayDisplay has a Quit button that will end your program. (The close box in the upper-right corner has not been implemented.)

To use ArrayDisplay, just copy the source code from here and drop it into the same folder as the rest of your program.

Also note: (1) ArrayDisplay has a main program, used for testing purposes, which you may ignore, and (2) you are welcome to modify ArrayDisplay in any way you like.