The goal of this lab is to practice file I/O and to review designing windows.
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:
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.
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)
.
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.
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.
Email the files you wrote or modified to
Thomas.VanDrunen@wheaton.edu
.