The goal of this lab is to practice file I/O and to review designing windows.
In this lab we will revisit the Sprite program 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:
For your convenience, the GUI and File I/O handouts (part 1, part 2) from class are available online.
If you did not complete lab15, but you are close, you might take up to about 30 minutes now to see if you can complete it. But don't take longer than that, because you'll need time for today's lab.
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/lab17 cd lab17
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
.
If you previously modified either Sprite.java
or
Sprites.java
, talk to the instructor about how to make
your version work with this lab.
If you have not completed the Sprite lab, you can copy the files from
/cslab/class/csci235/labs/lab17b/
.
However you got the listener class files, you probably want to add them to the local Mercurial repository and commit them before you go further.
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 SaveListener
class
so that its actionPerformed()
method
puts up a window asking for a filename. (The simplest way to do this
will be to provide a JTextField
in which the user can
type the filename. Keep it simple for now.)
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. Be sure to catch error cases. It is OK if you print any error messages to the terminal. If you can't think of how to iterate over all of the Sprites, look here for a hint.
If you manage to write your method without looking at the hint, take a look at it now, and note especially the last paragraph.
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 lab17
with the command
/cslab/class/csci235/bin/handin lab17 *.java
If you have time, here are instructions for using dialog classes to select a file and to display messages. If you want to try that, be sure that you first commit your changes to your repository before you start. Be sure you turn in what have done before you go on.