|
@@ -16,8 +16,15 @@ import java.awt.BorderLayout;
|
16
|
16
|
import java.awt.Image;
|
17
|
17
|
import java.awt.event.ActionEvent;
|
18
|
18
|
import java.awt.event.ActionListener;
|
|
19
|
+import java.io.File;
|
|
20
|
+import java.io.FileInputStream;
|
|
21
|
+import java.io.FileOutputStream;
|
19
|
22
|
import java.io.IOException;
|
20
|
23
|
|
|
24
|
+import java.io.ObjectInputStream;
|
|
25
|
+import java.io.ObjectOutputStream;
|
|
26
|
+import java.io.Serializable;
|
|
27
|
+import java.util.List;
|
21
|
28
|
import javax.imageio.ImageIO;
|
22
|
29
|
import javax.swing.BorderFactory;
|
23
|
30
|
import javax.swing.ImageIcon;
|
|
@@ -28,45 +35,66 @@ import javax.swing.JTabbedPane;
|
28
|
35
|
import javax.swing.WindowConstants;
|
29
|
36
|
|
30
|
37
|
/**
|
|
38
|
+ * Allows the user to customise options before starting a game.
|
31
|
39
|
*
|
32
|
40
|
* @author Chris
|
33
|
41
|
*/
|
34
|
42
|
public class ConfigScreen extends JDialog {
|
35
|
|
-
|
|
43
|
+
|
|
44
|
+ /**
|
|
45
|
+ * A version number for this class. It should be changed whenever the class
|
|
46
|
+ * structure is changed (or anything else that would prevent serialized
|
|
47
|
+ * objects being unserialized with the new class).
|
|
48
|
+ */
|
36
|
49
|
private static final long serialVersionUID = 1;
|
37
|
|
-
|
38
|
|
- private final JTabbedPane tabbedPane = new JTabbedPane();
|
|
50
|
+
|
|
51
|
+ /** The URL to prefix icons with. */
|
|
52
|
+ private static final String ICON_URL = "/com/md87/cardgame/res/icons/";
|
|
53
|
+
|
|
54
|
+ /** The configuration directory. */
|
|
55
|
+ private static String configdir;
|
|
56
|
+
|
|
57
|
+ /** The panel that allows the user to customise the players. */
|
39
|
58
|
private final PlayerPanel playerPanel = new PlayerPanel();
|
|
59
|
+ /** The panel that allows the user to select the game type. */
|
40
|
60
|
private final GamePanel gamePanel = new GamePanel(this);
|
|
61
|
+ /** The panel that allows the user to alter betting options. */
|
41
|
62
|
private final BettingPanel bettingPanel = new BettingPanel();
|
|
63
|
+ /** The panel that allows the user to alter apperance options. */
|
42
|
64
|
private final AppearancePanel stylePanel = new AppearancePanel();
|
|
65
|
+ /** The panel that allows the user to save/load settings. */
|
43
|
66
|
private final JPanel savePanel = new JPanel();
|
44
|
|
-
|
|
67
|
+
|
|
68
|
+ /** The type of game that is currently selected. */
|
45
|
69
|
private GameInfo game;
|
46
|
70
|
|
|
71
|
+ /**
|
|
72
|
+ * Creates and displays a new config screen.
|
|
73
|
+ */
|
47
|
74
|
public ConfigScreen() {
|
48
|
75
|
super();
|
49
|
|
-
|
|
76
|
+
|
50
|
77
|
Image gameIcon = null;
|
51
|
78
|
Image playerIcon = null;
|
52
|
79
|
Image moneyIcon = null;
|
53
|
80
|
Image saveIcon = null;
|
54
|
81
|
Image styleIcon = null;
|
55
|
|
-
|
|
82
|
+
|
56
|
83
|
try {
|
57
|
|
- gameIcon = ImageIO.read(getClass().getResource("/com/md87/cardgame/res/icons/icon.png"));
|
58
|
|
- playerIcon = ImageIO.read(getClass().getResource("/com/md87/cardgame/res/icons/user.png"));
|
59
|
|
- moneyIcon = ImageIO.read(getClass().getResource("/com/md87/cardgame/res/icons/money.png"));
|
60
|
|
- saveIcon = ImageIO.read(getClass().getResource("/com/md87/cardgame/res/icons/save.png"));
|
61
|
|
- styleIcon = ImageIO.read(getClass().getResource("/com/md87/cardgame/res/icons/style.png"));
|
|
84
|
+ gameIcon = ImageIO.read(getClass().getResource(ICON_URL + "icon.png"));
|
|
85
|
+ playerIcon = ImageIO.read(getClass().getResource(ICON_URL + "user.png"));
|
|
86
|
+ moneyIcon = ImageIO.read(getClass().getResource(ICON_URL + "money.png"));
|
|
87
|
+ saveIcon = ImageIO.read(getClass().getResource(ICON_URL + "save.png"));
|
|
88
|
+ styleIcon = ImageIO.read(getClass().getResource(ICON_URL + "style.png"));
|
62
|
89
|
} catch (IOException ex) {
|
63
|
90
|
System.err.println("Unable to load icons");
|
64
|
|
- }
|
65
|
|
-
|
|
91
|
+ }
|
|
92
|
+
|
66
|
93
|
setIconImage(gameIcon);
|
67
|
|
-
|
|
94
|
+
|
68
|
95
|
setTitle("JaPoker: Game configuration");
|
69
|
|
-
|
|
96
|
+
|
|
97
|
+ final JTabbedPane tabbedPane = new JTabbedPane();
|
70
|
98
|
tabbedPane.addTab("Game", new ImageIcon(gameIcon), gamePanel);
|
71
|
99
|
tabbedPane.addTab("Players", new ImageIcon(playerIcon), playerPanel);
|
72
|
100
|
tabbedPane.addTab("Betting", new ImageIcon(moneyIcon), bettingPanel);
|
|
@@ -74,61 +102,179 @@ public class ConfigScreen extends JDialog {
|
74
|
102
|
tabbedPane.addTab("Save/Load", new ImageIcon(saveIcon), savePanel);
|
75
|
103
|
tabbedPane.setEnabledAt(4, false);
|
76
|
104
|
tabbedPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
|
77
|
|
-
|
|
105
|
+
|
78
|
106
|
setLayout(new BorderLayout());
|
79
|
107
|
add(tabbedPane, BorderLayout.NORTH);
|
80
|
|
-
|
|
108
|
+
|
81
|
109
|
final JButton netButton = new JButton("Join Network Game");
|
82
|
110
|
netButton.setEnabled(false);
|
83
|
111
|
JPanel buttonPanel = new JPanel();
|
84
|
112
|
buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 20, 20, 20));
|
85
|
113
|
buttonPanel.add(netButton);
|
86
|
114
|
add(buttonPanel, BorderLayout.WEST);
|
87
|
|
-
|
|
115
|
+
|
88
|
116
|
final JButton startButton = new JButton("Start Game");
|
89
|
117
|
buttonPanel = new JPanel();
|
90
|
118
|
buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 20, 20, 20));
|
91
|
|
- buttonPanel.add(startButton);
|
|
119
|
+ buttonPanel.add(startButton);
|
92
|
120
|
add(buttonPanel, BorderLayout.EAST);
|
93
|
|
-
|
|
121
|
+
|
94
|
122
|
startButton.addActionListener(new ActionListener() {
|
95
|
123
|
@Override
|
96
|
124
|
public void actionPerformed(final ActionEvent e) {
|
97
|
125
|
startGame();
|
98
|
126
|
}
|
99
|
127
|
});
|
100
|
|
-
|
|
128
|
+
|
|
129
|
+ final File configDir = new File(getConfigDir());
|
|
130
|
+ if (!configDir.isDirectory()) {
|
|
131
|
+ configDir.mkdirs();
|
|
132
|
+ } else if (new File(configDir, "last_used.config").exists()) {
|
|
133
|
+ loadSettings("last_used");
|
|
134
|
+ }
|
|
135
|
+
|
101
|
136
|
pack();
|
102
|
137
|
setVisible(true);
|
103
|
138
|
setResizable(false);
|
104
|
|
-
|
|
139
|
+
|
105
|
140
|
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
|
106
|
141
|
}
|
107
|
|
-
|
|
142
|
+
|
|
143
|
+ /**
|
|
144
|
+ * Sets the game type that is currently selected.
|
|
145
|
+ *
|
|
146
|
+ * @param newGame The type of game that is now selected
|
|
147
|
+ */
|
108
|
148
|
public void setGame(final GameInfo newGame) {
|
109
|
149
|
game = newGame;
|
110
|
|
-
|
|
150
|
+
|
111
|
151
|
playerPanel.setMaxPlayers(game.getNumPlayers());
|
112
|
152
|
}
|
113
|
|
-
|
|
153
|
+
|
|
154
|
+ /**
|
|
155
|
+ * Retrieves the current game type.
|
|
156
|
+ *
|
|
157
|
+ * @return The type of game that is currently selected
|
|
158
|
+ */
|
114
|
159
|
public GameInfo getGame() {
|
115
|
160
|
return game;
|
116
|
161
|
}
|
117
|
|
-
|
|
162
|
+
|
|
163
|
+ /**
|
|
164
|
+ * Starts a game with the given settings.
|
|
165
|
+ */
|
118
|
166
|
private void startGame() {
|
119
|
|
- final Game myGame = game.getGame(playerPanel.getNumPlayers(),
|
120
|
|
- bettingPanel.getBigBlind(), bettingPanel.getAnte(),
|
|
167
|
+ final Game myGame = game.getGame(playerPanel.getNumPlayers(),
|
|
168
|
+ bettingPanel.getBigBlind(), bettingPanel.getAnte(),
|
121
|
169
|
bettingPanel.getRaises());
|
122
|
170
|
final GameWindow window = new GameWindow(myGame,
|
123
|
171
|
stylePanel.getFrontName(), stylePanel.getBackName(),
|
124
|
172
|
stylePanel.getColour());
|
125
|
|
-
|
|
173
|
+
|
126
|
174
|
for (Player player : playerPanel.getPlayers(myGame, window)) {
|
127
|
175
|
myGame.addPlayer(player);
|
128
|
176
|
}
|
129
|
|
-
|
|
177
|
+
|
130
|
178
|
myGame.startTournament();
|
|
179
|
+
|
|
180
|
+ saveSettings("last_used");
|
131
|
181
|
dispose();
|
132
|
182
|
}
|
133
|
183
|
|
|
184
|
+ /**
|
|
185
|
+ * Loads and applies the settings from the specified file.
|
|
186
|
+ *
|
|
187
|
+ * @param name The name of the file
|
|
188
|
+ */
|
|
189
|
+ @SuppressWarnings("unchecked")
|
|
190
|
+ public void loadSettings(final String name) {
|
|
191
|
+ try {
|
|
192
|
+ final ObjectInputStream ois
|
|
193
|
+ = new ObjectInputStream(new FileInputStream(
|
|
194
|
+ new File(getConfigDir(), name + ".config")));
|
|
195
|
+
|
|
196
|
+ ois.readObject();
|
|
197
|
+
|
|
198
|
+ final GameInfo gameInfo = (GameInfo) ois.readObject();
|
|
199
|
+ final List<Object[]> playerData = (List<Object[]>) ois.readObject();
|
|
200
|
+ final Object[] bettingData = (Object[]) ois.readObject();
|
|
201
|
+ final Object[] styleData = (Object[]) ois.readObject();
|
|
202
|
+
|
|
203
|
+ gamePanel.setGame(gameInfo);
|
|
204
|
+ playerPanel.setData(playerData);
|
|
205
|
+ bettingPanel.setData(bettingData);
|
|
206
|
+ stylePanel.setData(styleData);
|
|
207
|
+ } catch (IOException ex) {
|
|
208
|
+ System.err.println("Unable to load settings from " + name);
|
|
209
|
+ ex.printStackTrace();
|
|
210
|
+ } catch (ClassNotFoundException ex) {
|
|
211
|
+ System.err.println("Unable to load settings from " + name);
|
|
212
|
+ ex.printStackTrace();
|
|
213
|
+ } catch (ClassCastException ex) {
|
|
214
|
+ System.err.println("Unable to load settings from " + name);
|
|
215
|
+ ex.printStackTrace();
|
|
216
|
+ }
|
|
217
|
+ }
|
|
218
|
+
|
|
219
|
+ /**
|
|
220
|
+ * Saves the current settings to the specified file.
|
|
221
|
+ *
|
|
222
|
+ * @param name The name of the file
|
|
223
|
+ */
|
|
224
|
+ public void saveSettings(final String name) {
|
|
225
|
+ try {
|
|
226
|
+ final ObjectOutputStream oos
|
|
227
|
+ = new ObjectOutputStream(new FileOutputStream(
|
|
228
|
+ new File(getConfigDir(), name + ".config")));
|
|
229
|
+ oos.writeObject(new ConfigVersioner());
|
|
230
|
+ oos.writeObject(game);
|
|
231
|
+ oos.writeObject(playerPanel.getData());
|
|
232
|
+ oos.writeObject(bettingPanel.getData());
|
|
233
|
+ oos.writeObject(stylePanel.getData());
|
|
234
|
+ } catch (IOException ex) {
|
|
235
|
+ System.err.println("Unable to save settings to " + name);
|
|
236
|
+ ex.printStackTrace();
|
|
237
|
+ }
|
|
238
|
+ }
|
|
239
|
+
|
|
240
|
+ /**
|
|
241
|
+ * Returns the application's config directory.
|
|
242
|
+ *
|
|
243
|
+ * @return configuration directory
|
|
244
|
+ */
|
|
245
|
+ public static String getConfigDir() {
|
|
246
|
+ if (configdir == null) {
|
|
247
|
+ final String fs = System.getProperty("file.separator");
|
|
248
|
+ final String osName = System.getProperty("os.name");
|
|
249
|
+ if (osName.startsWith("Mac OS")) {
|
|
250
|
+ configdir = System.getProperty("user.home") + fs + "Library"
|
|
251
|
+ + fs + "Preferences" + fs + "japoker" + fs;
|
|
252
|
+ } else if (osName.startsWith("Windows")) {
|
|
253
|
+ if (System.getenv("APPDATA") == null) {
|
|
254
|
+ configdir = System.getProperty("user.home") + fs + "japoker" + fs;
|
|
255
|
+ } else {
|
|
256
|
+ configdir = System.getenv("APPDATA") + fs + "japoker" + fs;
|
|
257
|
+ }
|
|
258
|
+ } else {
|
|
259
|
+ configdir = System.getProperty("user.home") + fs + ".japoker" + fs;
|
|
260
|
+ }
|
|
261
|
+ }
|
|
262
|
+
|
|
263
|
+ return configdir;
|
|
264
|
+ }
|
|
265
|
+
|
|
266
|
+}
|
|
267
|
+
|
|
268
|
+/**
|
|
269
|
+ * A class used for versioning config files.
|
|
270
|
+ */
|
|
271
|
+class ConfigVersioner implements Serializable {
|
|
272
|
+
|
|
273
|
+ /**
|
|
274
|
+ * A version number for this class. It should be changed whenever the class
|
|
275
|
+ * structure is changed (or anything else that would prevent serialized
|
|
276
|
+ * objects being unserialized with the new class).
|
|
277
|
+ */
|
|
278
|
+ private static final long serialVersionUID = 2;
|
|
279
|
+
|
134
|
280
|
}
|