Lab 18:File I/O

The goal of this lab is to practice file I/O and to review designing windows.

1. Introduction

In this lab we will revisit the Skeet example from lab 15. This time we will give the program the facility to save the current state of the simulation and reload it later.

The program should work as before but with two changes:

2. Set up

Follow these set-up instructions carefully.

Make a directory for this lab. First, copy files from the course directory for this lab:

cp -r /homes/tvandrun/Public/cs235/lab18/* .

Then copy from your lab15 directory AddListener.java and any other class that you wrote (you should have written another action listener, perhaps named something like OkListener.java.

Presumably you did not modify Skeet.java or Skeets.java. If you did, talk to the instructor about how to make your old version conform to this lab.

.

3. Designing a protocol

First, discuss with your partner how you can save/store a collection of Skeets. You'll need to decided on a file format/protocol.

One aspect you'll need to think about is how to save and reload a color. Take a look at the Java API reference for the Color class to see how colors are represented by Java. The easist way is to store a color using an int which describes the red, green, and blue component of that color. See getRGB() and the constructor Color(int).

4. Writing the SaveListener

Next, finish the SaveListener class so that its actionPerformed() method puts up a window asking for a filename. This window should also have a button labelled "Ok" or the equivalent.

Then write another action listener class which will close that window and do the actual saving.

5. Loading a saved configuration

Finally, in the file Skeets.java, find the portion of code that will load a configuration from a file and fill-in what's missing.

One part you will need to think about is how to iterate over all the skeets in the simulation. Here's how: The model has methods width(), height(), and getAgentAt(). You can use these to iterate ove the model as you would iterate over a two-dimensional array.

6. Turn in

Email the files you wrote or modified to Thomas.VanDrunen@wheaton.edu.


Last modified: Thu Dec 10 12:55:39 CST 2009