The goal of this lab is to practice file I/O, handling exceptions, and writing action listeners.
In this lab we will revisit the Sprite program from lab 14. 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:
For your convenience, the GUI and File I/O handouts from class are available online.
I have provided solutions for the lab 14 functionality, with a little bit of rearrangement.
Follow these set-up instructions carefully.
Make a directory for this lab and cd
into it.
First, copy files from the course directory for this lab:
hg clone /cslab/class/csci235/labs/lab16 cd lab16
Take a look at how this differs from what you saw in lab 14. I have provided the action listeners and dialogs, but you will need to fill in some methods.
Remember that you will need to include -cp
simulation.jar:.
with both the javac
and
java
commands.
First, discuss with your partner how you can save/store
a collection of Sprites.
You'll need to decide on a file format, which will need to include a
way to format each of the Sprites. At the end of this week's
Sprite.java
, you'll find a place to describe your format
in a comment.
The trickiest part of this will be figuring out how to write the
Color
value out in a way that it can be re-constructed.
One path to that is to look at the constructors for Color; you want to find a constructor for which there
is a corresponding method returning the value. (Try to keep it
simple. And you can rule out anything having to do with "system
properties".)
You can uncomment or replace the final section in
Sprite.java
. You'll need something like what is there to
encapsulate the conversion.
SaveListener
Next, finish the saveSprites()
method in the
SaveListener
class. Most of this will involve calling
the writeSprites()
method in class Sprites
.
Note that you need to catch any exceptions; you can display a message
back to the user using the provided label.
A lot of the work is going to be in class Sprites
, where
you need to iterate over all of the sprites to write them to a file.
Finally, in the file Sprites.java
, find the
portion of code that will load a configuration from a file
and fill-in what's missing.
Turn in your source files as lab16
with the command
/cslab/class/csci235/bin/handin lab16 *.java