Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The Player application provides a number of UI widgets that allow a user to change various settings such as the player's name, the number of cards to play, and whether to beep to announce each ball.[PENDING: put picture of player with callouts here]
As you can see from the diagram, the settings for the Player application that the user can change are:
As an added convenience, the Player application remembers these settings between invocations so that each time the user invokes the Player application its UI can be initialized with the values used in the last game.
- the player's name
- the name of the machine on which the Game is running
- the number of cards to play
- whether the application should beep for each ball
- the random number seed
One class is the central repository for all program settings in the Player: the
PlayerParameters
class. For each program setting,PlayerParameters
declares one member variable to contain its current value, and two methods, one to set (or change) the value and one to get it. For example, here is the declaration for the member variable inPlayerParameters
that stores the player's name, and the two methods that set and get it:Note that the. . . private String name = ""; . . . void setName(String name) { this.name = name; saveParameters(); } String getName() { return name; }setName
method calls another method namedsaveParameters
which saves the now changed properties to a file on the file system. All the settings but one, the random number seed, is remembered between invocations of the Player application.
PlayerParameters
manages the values of the settings for the program. The work of saving the settings to the Player's properties file and restoring them from the same file is done by its superclassParameters
.
Parameters
is an abstract class with three abstract methods that must be implemented by its subclasses. These three methods bridge setting values as maintained by thePlayerParameters
class and theirString
representations saved to a file.Parameters
uses the JDK'sProperties
class to store theString
representation of each setting and save and restore them.
When the Player first starts up, it loads the last-saved properties and uses them to initialize its UI. The Player and the Game use the JDK'sProperties
class to save and restore program settings, so they are stored asString
s in as key/value pairs like this:The method used to load the properties is implemented inplayer.shouldbeep=false server.name=taranis num.cards=3 player.name=mary campionePlayerParameters
' superclass,Parameters
, and looks like this:This method does three things:protected void getParameters() { Properties defaults = new Properties(); FileInputStream in = null; setDefaults(defaults); properties = new Properties(defaults); try { String folder = System.getProperty("user.home"); String filesep = System.getProperty("file.separator"); in = new FileInputStream(folder + filesep + propertiesFilename); properties.load(in); } catch (java.io.FileNotFoundException e) { in = null; ErrorMessages.error("Can't find properties file. " + "Using defaults."); } catch (java.io.IOException e) { ErrorMessages.error("Can't read properties file. " + "Using defaults."); } finally { if (in != null) { try { in.close(); } catch (java.io.IOException e) { } in = null; } } updateSettingsFromProperties(); }Note that
- calls
setDefaults
to set up default values which will be used if there are no properties saved or if there are any errors while reading them. (This method call is shown in bold)- loads the properties from the file. (This is the code in the big
try
statement.)- calls
updateSettingsFromProperties
to update the program settings from the properties just read. (This method call is also shown in bold.)setDefaults
andupdateSettingsFromProperties
are abstract methods in theParameters
class. These methods are implemented in its concrete subclasses such asPlayerParameters
. This is appropriate becauseParameters
does the generic work of saving and restoring the properties and its two subclassesPlayerParameters
andGameParameters
manage the specific program settings for each program.Remember that the program settings are stored as human-readable text.
setDefaults
andupdateSettingsFromProperties
convert the values fromString
s to the appropriate type at this time.Whenever a program setting is modified and that change takes affect, the settings are saved to the file. For example, here's
PlayerParameters
'ssetHostname
method that changes the hostname.This method callsvoid setHostname(String hostname) { this.hostname = hostname; saveParameters(); }saveParameters
a method implemented by the parent classParameters
This method converts the settings as stored in the program toprotected void saveParameters() { updatePropertiesFromSettings(); if (DEBUG) { System.out.println("Just set properties: " + propertiesDescription); System.out.println(toString()); } FileOutputStream out = null; try { String folder = System.getProperty("user.home"); String filesep = System.getProperty("file.separator"); out = new FileOutputStream(folder + filesep + propertiesFilename); properties.save(out, propertiesDescription); } catch (java.io.IOException e) { ErrorMessages.error("Can't save properties. " + "Oh well, it's not a big deal."); } finally { if (out != null) { try { out.close(); } catch (java.io.IOException e) { } out = null; } } }String
s by calling theupdatePropertiesFromSettings
method which is declared abstract inParameters
and implemented byPlayerParameters
. Next the method saves the properties to a file.You should note the use of the JDK's
Properties
class to store theString
representations of the program settings, and save and restore them to the file system.
The Game application also provides a UI for modifying settings for the entire game (such as maximum number of players, maximum number of wolf cries and so on):[PENDING: put picture of game with callouts here]
Like the Player application, the Game application uses one class, the
GameParameters
class, to manage its settings.GameParameters
is also a subclass ofParameters
. The code in this class is very similar to that in the Player and we leave it to you to figure out.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.