|
|
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
PlayerParametersclass. For each program setting,
PlayerParametersdeclares 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 inPlayerParametersthat 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; }setNamemethod calls another method namedsaveParameterswhich 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.
PlayerParametersmanages 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.
Parametersis an abstract class with three abstract methods that must be implemented by its subclasses. These three methods bridge setting values as maintained by thePlayerParametersclass and theirStringrepresentations saved to a file.Parametersuses the JDK'sPropertiesclass to store the
Stringrepresentation 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'sPropertiesclass to save and restore program settings, so they are stored asStrings 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
setDefaultsto 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
trystatement.)- calls
updateSettingsFromPropertiesto update the program settings from the properties just read. (This method call is also shown in bold.)setDefaultsandupdateSettingsFromPropertiesare abstract methods in theParametersclass. These methods are implemented in its concrete subclasses such asPlayerParameters. This is appropriate becauseParametersdoes the generic work of saving and restoring the properties and its two subclassesPlayerParametersandGameParametersmanage the specific program settings for each program.Remember that the program settings are stored as human-readable text.
setDefaultsandupdateSettingsFromPropertiesconvert the values fromStrings 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'ssetHostnamemethod that changes the hostname.This method callsvoid setHostname(String hostname) { this.hostname = hostname; saveParameters(); }saveParametersa method implemented by the parent classParametersThis 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; } } }Strings by calling theupdatePropertiesFromSettingsmethod which is declared abstract inParametersand implemented byPlayerParameters. Next the method saves the properties to a file.You should note the use of the JDK's
Propertiesclass to store theStringrepresentations 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
GameParametersclass, to manage its settings.
GameParametersis 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-2004 Sun Microsystems, Inc. All rights reserved.