CPSC 124, Fall 2005
Lab 12: Partially-full Arrays

In this lab, you are given a program that can display a single image, and you have to modify it so that it can display a list of images. The images are little "icons" that can be placed in a display area by the user. Since the number of images varies as the program runs, you need to use the technique of "partially full arrays."

Since last week's lab turned out to be too long, only Part 1 of that lab is due today; Parts 2 and 3 are due next Tuesday, along with the lab for this week. You should be able to complete the partially-full array exercise before the end of the lab period today. You can use any remaining time to work on Parts 2 and 3 of Labs 11 (or, if you have completed Lab 11 already, you could work on your final project).

Silly Stamper

As usual, you can begin the lab by creating a new Java project in Eclipse. Into that project, you should import the entire contents of the directory /classes/f05/cs124/lab12. This will include three files, FileIO.java, IconStampPad.java, SillyStamperApplication.java, and a directory named icons that holds all the icon images that will be used in the program. Be sure that you select the icon directory, and not just the individual files in the icon directory; after you finish the import, the icon images should still be in a directory named icons inside Eclipse. (In case you want to download the files for this lab off the Web, the icons directory is also available in a zip file, icons.zip, to make them easier to download.)

After doing the import, you can run SillyStamperApplication as an application. You should see a large display area with a list of icons to the right and a few control buttons at the bottom of the window. You can select an icon in the list by clicking on it. If you click the display area, a copy of the currently selected icon will be shown in the display area at the point where you click. As the program is currently written, only one icon at a time can be displayed. Your job is to make it possible to display a list of icons. You will be working only in the file IconStampPad.java. The six parts of the program that you have to modify are each marked with a "TODO". The six modifications are:

  1. The starting version of IconStampPad uses three variables to keep track of the icon in the picture: iconData is the icon number that tells which icon to display, and iconX and iconY are the x- and y-coordinates where the icon is drawn. To make the program work with multiple icons, you will have to replace these three variables with three arrays. Don't forget that in addition to declaring an array variable, you must also create the actual array using, for example, new int[10000]. You should make the arrays large enough to hold information for any number of icons. Since the number of icons will vary as the program runs, the arrays will be "partially full arrays." You will also need a separate int variable to keep track of the number of icons in the picture (which is also the number of meaningful items stored in each array).
  2. In the paintComponent method, instead of drawing a single icon, you will have to draw a sequence of icons using the data from the three arrays.
  3. In the mousePressed method, instead of just changing the information about a single icon, you will have to add information about the new icon to the three arrays.
  4. In the doUndo method, you should remove the last icon from the list of icons stored in the arrays (if there are any icons in the list).
  5. In the writeDataToFile method, you should write information about all the icons in the list using FileIO. (As the comments in the program explain, FileIO is a simple class for reading from and writing to files. It works just like TextIO, except that data can be read from a file or written to a file. The work of letting the user select a file and opening the file is done elsewhere in the program, in parts of the program that you don't have to modify.) Follow the format that is used for the single icon in the original program: Each line should contain the icon number, the x-coordinate, and the y-coordinate of one icon, separated by spaces.
  6. In the readDataFromFile method, you should read all the icon data from the file. Start with an empty list of icons. For each line in the file, add one icon to the list. In order to do this, you will need some way to tell when you are at the end of the file. The FileIO class has a static boolean-valued method FileIO.eof() that returns true if you are at the end of the file. This means that you can process the file with a while loop of the form "while (FileIO.eof() == false)..."

Exercise: Modify the file IconStampPad.java, as described above, to use arrays to store data about the icons in the picture. Use your modified program to load the sample data file /classes/f05/cs124/SillyStamperExample.data, and show me the resulting picture on your computer screen. (Also, as usual, turn in a printout of the modified file next week, along with a printout of your VideoPokerPanel.java file from Lab 11.)

If you would like some extra credit on this lab, add a "Redo" button to the applet. This button should "undo the most recent undo", if any. To make this work, you need to keep track of how many redo's are available. This number goes up when an Undo is performed. It goes down when a Redo is performed, and it is reset to zero whenever a new icon image is added to the picture. Ideally, you should enable/disable the Undo and Redo buttons, so that they are only enabled when there is something for them to do. You might also want to add a "Clear" button.

David J. Eck, November 2005