-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMapGUI.java
496 lines (459 loc) · 17.9 KB
/
MapGUI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.Timer;
import java.awt.Font;
import java.awt.List;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.UIManager;
import chn.util.FileInput;
/**
* MapGUI is the Graphical User Interface of the program "Domination". It uses multiple ActionListeners, JButtons, JOptionPanes,
* and JLabels on a JPanel/Frame to allow for user interaction with the program
*
*
* @author (Aishwarya, Anurag, Caroline, Serena)
* @version (June 5, 2018)
*/
public class MapGUI
{
//Aishwarya
private JFrame frame; //our frame where all graphics except JOptionPanes appear
private final static int NUM_PLAYERS = 4;
//Anurag
private ArrayList<DistrictButton> buttons = new ArrayList<DistrictButton>(); //stores all the districts
private JLabel leaderboard[] = new JLabel[NUM_PLAYERS]; //Leaderboard of the map's users
//Caroline
private static final Color MAP_COLOR = new Color(235, 213, 179); //Color of the map
private Map game; //map
private DistrictButton selected;
private DistrictButton target;
//Serena
private JButton go;
private java.util.List<Player> gamers;
/**
* Launch the application.
*/
//Aishwarya & Caroline
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
//intializing Map and MapGUI
MapGUI window = new MapGUI(new Map());
window.frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Creates the application.
*/
//Anurag
public MapGUI(Map m) {
initialize();//helper method
game = m;
}
/**
* Initializes the contents of the frame.
*/
//Aishwarya, Anurag, and Serena
private void initialize()
{
//new window with fonts specified
frame = new JFrame();
frame.setFont(new Font("Courier New", Font.PLAIN, 30));
frame.setBounds(100, 100, 1920, 1080);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
try
{
frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("TitleScreen.jpg")))));
}
catch (IOException e)
{
e.printStackTrace();
}
frame.pack();
frame.setVisible(true);
//start button
JButton start = new JButton();
start.setFont(new Font("Times New Roman", Font.PLAIN, 50));
start.setForeground(Color.WHITE);
Color redColor = new Color(66, 13, 9);
//setting up the start button to initialize map
start.setBackground(redColor);
start.setText("Start");
frame.add(start);
start.setBounds(1520, 400, 250, 100);
start.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
initializeMap();
}
});
//instructions button
JButton information = new JButton();
information.setFont(new Font("Times New Roman", Font.PLAIN, 45));
information.setForeground(Color.WHITE);
information.setBackground(redColor);
information.setText("Instructions");
frame.add(information);
information.setBounds(1520, 560, 250, 100);
//if clicked then...
information.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//show instructions to the game
UIManager.put("OptionPane.messageFont", new Font("Arial", Font.PLAIN, 26));
UIManager.put("OptionPane.buttonFont", new Font("Arial", Font.PLAIN, 12));
try
{
JOptionPane.showMessageDialog(frame, new ImageIcon(ImageIO.read(new File("Instructions.jpg"))));
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
);
}
/**
*
* <p>
* Changes the start screen to a map with buttons placed on coordinate points from "coordinates.txt".
* This method also creates many auxiliary buttons and labels which are described as such:
* </p>
*
* <b>
* leaderboard (JLabel) - displays the leaderboard of all players indefinitely
* stats (JLabel) - displays the stats of the selectedButton
* move (JButton) - displays the words "Move/Invade" and when clicked prompts the user to select provinces in a
* JOptionPane Message Dialog
* addFort(JButton) - displays the words "Build Fort" and when clicked returns whether or not a fort was added.
* </b>
* @param N/A
* @return N/A
*/
//Caroline
private void initializeMap()
{
gamers = game.getGamers();
//loading background...
try
{
frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("Map.png")))));
frame.pack();
frame.setVisible(true);
}
catch (IOException e)
{
e.printStackTrace();
}
//leaderboard labels - Anurag
String[] lead = game.leaderBoard();
for(int i = 0 ; i < NUM_PLAYERS ; i++)
{
leaderboard[i] = new JLabel();
frame.add(leaderboard[i]);
leaderboard[i].setFont(new Font("Arial", Font.PLAIN, 26));
leaderboard[i].setBounds(384,378 + (i * 100),327,58);
leaderboard[i].setBorder(null);
leaderboard[i].setOpaque(false);
leaderboard[i].setText(lead[i]);
leaderboard[i].setVisible(true);
}
//statistics label - Anurag
JLabel stats = new JLabel();
frame.add(stats);
stats.setFont(new Font("Arial", Font.PLAIN, 26));
stats.setBounds(18,18,1904,61);
stats.setBorder(null);
stats.setOpaque(false);
stats.setVisible(true);
//currentTurn label - Aishwarya
JLabel currentTurn = new JLabel();
frame.add(currentTurn);
currentTurn.setFont(new Font("Arial", Font.PLAIN, 26));
currentTurn.setBounds(1055,13,837,67);
currentTurn.setBorder(null);
currentTurn.setOpaque(false);
currentTurn.setVisible(true);
currentTurn.setText("Player: " + gamers.get(game.currentPlayer()).getName() + ", Country Name: "
+ gamers.get(game.currentPlayer()).getCountryName());
//initializing go button - Aishwarya
go = new JButton();
frame.add(go);
go.setFont(new Font("Arial", Font.PLAIN, 26));
go.setBounds(976,980,288,100);
go.setBorder(null);
go.setOpaque(false);
go.setBackground(MAP_COLOR);
go.setText("Go!");
go.setVisible(false);
//initializing endTurn button - Serena
JButton endTurn = new JButton();
frame.add(endTurn);
endTurn.setFont(new Font("Arial", Font.PLAIN, 26));
endTurn.setBounds(1296,980,288,100);
endTurn.setBorder(null);
endTurn.setOpaque(false);
endTurn.setBackground(MAP_COLOR);
endTurn.setText("End Turn!");
endTurn.setVisible(false);
//if clicked then...
endTurn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//end the turn and update the currentTurn label on the HDD
game.nextTurn();
currentTurn.setText("Player: " + gamers.get(game.currentPlayer()).getName() + ", Country Name: "
+ gamers.get(game.currentPlayer()).getCountryName());
}
});
//move button - Caroline
JButton move = new JButton();
frame.add(move);
move.setFont(new Font("Arial", Font.PLAIN, 26));
move.setBounds(16,980,288,100);
move.setBorder(null);
move.setOpaque(false);
move.setBackground(MAP_COLOR);
move.setText("Move/Invade");
move.setVisible(false);
//if clicked then...
move.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//prompt user for the district to move to
JOptionPane.showMessageDialog(frame, "Pick a neighboring district to invade");
// int wC = selected.CountryId();
// int wD = selected.DistrictId();
go.setVisible(true);
go.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int wC = selected.CountryId();
int iC = target.CountryId();
int wD = selected.DistrictId();
int iD = target.DistrictId();
//error testing - > System.out.print(wC + " " + wD + " " + iC + " " + iD);
//CHECK IF THE SELECTED PROVINCE BELONGS TO THE CURRENT PLAYER
if(selected.isBordering(target))
{
if(game.isMine(gamers.get(game.currentPlayer()), target))
{
target.getLinked().setTroopCount(selected.getLinked().getdTroopCount());
selected.getLinked().setTroopCount(0);
JOptionPane.showMessageDialog(frame, "Troop Transfer success!");
go.setVisible(false);
}
else
{
game.invade(wC, wD , iC , iD);
selected.setOwner(game.getEurope().get(game.findId(selected.getLinked().getIdentifier())));
selected.updateInternals();
target.setOwner(game.getEurope().get(game.findId(selected.getLinked().getIdentifier())));
target.updateInternals();
go.setVisible(false);
selected = null;
gamers.get(game.currentPlayer()).makeMove();
String[] lead = game.leaderBoard();
//update leaderboard
for(int i = 0 ; i < NUM_PLAYERS ; i++)
{
leaderboard[i].setText(lead[i]);
}
}
}
else
{
JOptionPane.showMessageDialog(frame, "Pick a NEIGHBORING district to invade. Retry.");
go.setVisible(false);
}
}
});
}
});
//addFort button - Anurag
JButton addFort = new JButton();
frame.add(addFort);
addFort.setFont(new Font("Arial", Font.PLAIN, 26));
addFort.setBounds(336,980,288,100);
addFort.setBorder(null);
addFort.setOpaque(false);
addFort.setBackground(MAP_COLOR);
addFort.setText("Build a Fort");
addFort.setVisible(false);
//if clicked then...
addFort.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//add a Fort
if(selected.getLinked().addFort())
JOptionPane.showMessageDialog(frame, "Added!");
else
JOptionPane.showMessageDialog(frame, "We're sorry, you're short on money!");
}
});
//Recruit button - Caroline
JButton recruit = new JButton();
frame.add(recruit);
recruit.setFont(new Font("Arial", Font.PLAIN, 26));
recruit.setBounds(656,980,288,100);
recruit.setBorder(null);
recruit.setOpaque(false);
recruit.setBackground(MAP_COLOR);
recruit.setText("Recruit");
recruit.setVisible(false);
//if clicked then...
recruit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
selected.getLinked().recruit(Integer.parseInt(JOptionPane.showInputDialog("How many sets of 100 would you like to recruit?")));
}
});
//file reading - Aishwarya
FileInput inFile3;
String fileName3 = "coordinates.txt";
inFile3 = new FileInput(fileName3);
for (int x = 0; x < game.getEurope().size(); x++)
{
//initialize country, color, and districtNum - Anurag
Country c = game.getEurope().get(x);
int dNum = c.getDistrictNum();
String col = c.getColor();
Color cColor = Color.decode("#" + col.toUpperCase());
for(int y = 0 ; y < dNum ; y++)
{
//parsing data from file - Caroline
String input;
int count;
input = inFile3.readLine();
String strarray[] = input.split(" ");
int coord[] = new int[strarray.length];
for (count = 0; count < coord.length ; count++)
{
coord[count] = Integer.parseInt(strarray[count]);
}
//DistrictButton set up for all buttons on the array
DistrictButton a;
a = new DistrictButton(game.getEurope().get(x).getProvince(y),c);
frame.add(a);
buttons.add(a);
a.setForeground(Color.WHITE);
a.setBackground(cColor);
a.setBounds(coord[0], coord[1], coord[2], coord[3]);
a.setVisible(true);
a.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String s = e.toString();
s = s.substring(s.indexOf("Country"));
stats.setText(s);
currentTurn.setVisible(true);
endTurn.setVisible(true);
if(go.isVisible())
{
target = a;
move.setVisible(false);
addFort.setVisible(false);
recruit.setVisible(false);
}
else
{
selected = a;
selected.updateInternals();
if(game.isMine(gamers.get(game.currentPlayer()), selected))
{
move.setVisible(true);
addFort.setVisible(true);
recruit.setVisible(true);
}
else
{
move.setVisible(false);
addFort.setVisible(false);
recruit.setVisible(false);
}
}
currentTurn.setText("Player: " + gamers.get(game.currentPlayer()).getName() + ", Country Name: "
+ gamers.get(game.currentPlayer()).getCountryName());
}
}
);
}
}
}
/**
* displaySelector(String[], String[], int) returns a multi-option JOptionPane with choices from the arrays given in the parameters
* @param sarray - choice names
* @param names - player names
* @param num - number of repetitions
* @return rtn - String array that gives the caller a JOPtionPane that presents them with options from a primitive array that they created
*/
//Anurag
public static String[] displaySelector(String[] sarray, String[] names, int num)
{
//stores the responses
String[] rtn = new String[num];
//repeat until all 4 have chosen
for(int i = 0 ; i < num ; i++)
{
//prompting
UIManager.put("OptionPane.messageFont", new Font("Arial", Font.PLAIN, 26));
UIManager.put("OptionPane.buttonFont", new Font("Arial", Font.PLAIN, 12));
int response = JOptionPane.showOptionDialog(null, names[i] + ", where do you wish to rule?", "Atlas",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
null, sarray, sarray[0]);
String[] temp = new String[sarray.length -1];
int x = 0;
int y = 0;
//removing an answer choice after it has already been selected
while(x < sarray.length)
{
if(x == response)
{
x++;
rtn[i] = sarray[response];
}
else
{
temp[y] = sarray[x];
x++;
y++;
}
}
sarray = temp;
}
//done
return rtn;
}
}