CPSC 124 (Fall 1998): Lab 10
Using Files

WELCOME TO THE FINAL LAB for Computer Science 124. The final lab of the course is taking place on the last day of classes for the term. It will be a fairly short lab, and there will be no lab report to turn in. There is only one exercise, which you should finish during the lab. You get some points for showing up and some points for finishing the exercise. The point of the exercise is to give you some experience working with files and file dialog boxes.

Before you begin, copy the folder /home/cs124/lab10 into your home directory and do a "cd lab10".

Reading from a File

The file SimpleDrawProgram.java defines a standalone drawing application, which is based on the line-drawing applet that you worked with in Lab 8. (This program depends on other classes that are defined in the files MessageDialog.java and TextReader.java.) I have rewritten the applet as an application, improved it in some ways, and made some changes to make it easier to use files for saving and reloading drawings. (Two changes that you need to know about: In this program, a color is represented as an integer, which gives an index into an array of colors; this makes it easy to represent the color in a file. Second, I changed the name of the class that represents a colored line segment from Line in Lab 8 to ColoredLine in this lab.) We need an application for this lab, rather than an applet, because applets, for security reasons, are not ordinarily allowed to access files. You can run the program with the command "java SimpleDrawProgram".

The application opens a frame, which is an independent window. The frame has its own menu bar. The Line Color menu sets the color that will be used for drawing lines. (When you draw lines, you'll notice that the program uses a "rubber band" line to follow the mouse as you move it. When you release the mouse, the line becomes a permanent part of the drawing.) The Background Color menu allows you to fill in the background of the drawing with a solid color. The File menu contains several commands, including a Save command that can be used to save a drawing to a file and a Load command that is meant for reloading a saved drawing from a file. The Load command is not yet implemented. Your exercise for the lab is to implement the Load command.

After you have tried the program, open the file SimpleDrawProgram.java with nedit. Find the doLoadFromFile() command. (Be smart: Find it with the editor's Find command!) Right now, this method says simply:

    new MessageDialog(parentFrame, 
                 "Sorry, loading is not yet implemented.  (That's your job.)");

This command displays the message to the user in a dialog box, as you can see if you try the Load command in SimpleDrawProgram. Get rid of this line and replace it with the commands necessary to read a drawing from a file. It must be able to read data in the format written by the doSaveToFile() method. Therefore, the doLoadFromFile() method can be modeled to some extent on the doSaveToFile() method. Your method should do the following:

Any other information that you need can be found in Section 8.2 and Section 8.3 of the text.

After you have completed the doLoadFromFile() method successfully, you should be able to reload drawings that have been saved in files. Try it on the sample data file, sampleDrawing. If you try to load a file that does not contain valid data for a drawing, your program should show an error message. The drawing that is currently on the screen should not be changed.

To complete the lab, you should show me that you can successfully read the data in the file sampleDrawing, and that when you try to read the data in the file bogusDrawing, your program gives an error message and makes no changes to the current drawing.

After you have finished the exercise, you can leave or you can stay to work on your final project.

[ Lab Index | Online Notes ]