-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSituationPanel.java
240 lines (216 loc) · 10.7 KB
/
SituationPanel.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
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Vector;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* Creates a panel for a Situation object to be displayed with. Buttons
* selected in this panel have the capability to lead to other panels.
* Buttons pressed in this panel also affect a Person object whose sleep,
* smart, and social scores are displayed.
*
* @author Zahra Thabet
* @author Nolen Belle Bryant
* @author Giulia Bronzi
* @version 12.17.18
*/
public class SituationPanel extends JPanel{
private Situation sit; //the situation to be displayed
private String question; //the question to be displayed
private Option option1, option2; //options of the corresponding Situation
private Person player; //the Person object to be affected by the Situation
//text areas to be displayed in the Panel
private JTextArea questionText, playerStatus;
//buttons corresponding to the options of the Situation
private JButton option1Button, option2Button;
//the TrailsBinaryTree that the Situation displayed in this Panel is in
private TrailsBinaryTree tree;
private JLayeredPane content;//holds the background and foreground
private JPanel opPanel, topRow, midRow; //holds the buttons
/**
* Constructor for objects of class SituationPanel. Creates a Situation
* panel that displays a Situation.
*
* @param p Person to be affected by decisions
* @param t TrailsBinaryTree containing the tree of relevant Situations
*/
public SituationPanel(Person p,TrailsBinaryTree t)
{
player = p;
tree=t;
topRow = new JPanel ();
topRow.setBackground(new Color(0,39,118));
topRow.setOpaque(false);
midRow = new JPanel();
midRow.setBackground(new Color(0,39,118));
midRow.setOpaque(false);
opPanel = new JPanel();
opPanel.setBackground(new Color(0,39,118));
opPanel.setLayout(new GridLayout(2,1,30,30));
Font verand = new Font("Verdana", Font.BOLD, 28);
//accesses all the relevant Situation object components
sit = tree.getCurrent();
question = sit.getQuestion();
option1=sit.getOptionLeft();
option2 = sit.getOptionRight();
//displays the Situation's question
questionText = new JTextArea (question);
questionText.setFont(verand);
questionText.setLineWrap(true);
questionText.setBackground(new Color(0,39,118));
questionText.setForeground(new Color(236,222,187));
//displays the players current point totals
playerStatus = new JTextArea(player.toString());
playerStatus.setBackground(new Color(236,222,187));
playerStatus.setFont(verand);
//displays the Situation's two Options' decisions as buttons
option1Button = new JButton(option1.getDecision());
option1Button.setFont(verand);
option2Button = new JButton(option2.getDecision());
option2Button.setFont(verand);
opPanel.add(option1Button);
opPanel.add(option2Button);
option1Button.addActionListener(new ButtonListener());
option2Button.addActionListener(new ButtonListener());
setBackground(new Color(0,39,118));
setPreferredSize(new Dimension(1200,800));
setLayout(new BorderLayout());
add(questionText, BorderLayout.NORTH);
add(opPanel, BorderLayout.CENTER);
add(playerStatus,BorderLayout.SOUTH);
}
/**
* Represents the listeners for all buttons
*/
private class ButtonListener implements ActionListener
{
/**
* Sets the card to the correct panel based on which button is pressed
* and where the current situation in the TrailsBinaryTree is
*
* @param event action event that triggers the action
*/
public void actionPerformed (ActionEvent event)
{
JPanel sitPanel = (JPanel) opPanel.getParent();
JPanel cardLayoutPanel = (JPanel)sitPanel.getParent();
CardLayout layout = (CardLayout)cardLayoutPanel.getLayout();
try{
//if the source is the option 1 button
if (event.getSource().equals(option1Button)){
if(option1.getDecision().toLowerCase().contains("run")){
RunningPanel run = new RunningPanel(player, tree, true);
cardLayoutPanel.add(run,"runningGame");
layout.show(cardLayoutPanel, "runningGame");
}else if(option1.getDecision().toLowerCase(
).contains("freeze")){
//launches the squirrel memory game
MemoryPanel memS = new MemoryPanel(player, tree, true,
"Squirrel");
cardLayoutPanel.add(memS,"squirrelGame");
layout.show(cardLayoutPanel, "squirrelGame");
}else if(option1.getDecision().toLowerCase(
).contains("directory")){
//launches the fym memory game
MemoryPanel memF = new MemoryPanel(player, tree, true,
"FYM");
cardLayoutPanel.add(memF,"fymGame");
layout.show(cardLayoutPanel, "fymGame");
} else if(option1.getDecision().toLowerCase(
).contains("flashlight")){
//launches the tunnel memory game
MemoryPanel memT = new MemoryPanel(player, tree, true,
"Tunnel");
cardLayoutPanel.add(memT,"tunnelGame");
layout.show(cardLayoutPanel, "tunnelGame");
}else{
//add the associated points to the player's points
player.addAllScores(option1.getPoints());
//if the players points are still above zero,
//continue to the left child SituationPanel
if (player.isAboveZero()){
//incremements the tree so that the current
//Situation is the left child of the current
//Situation
tree.nextLeft();
//shows a SituationPanel of the new current
//Situation
SituationPanel nextPanel = new SituationPanel(
player, tree);
cardLayoutPanel.add(nextPanel,"left");
layout.show(cardLayoutPanel, "left");
//if the player's points are not above zero
//after pressing the button, shows the death panel
}else{
DeathPanel death = new DeathPanel();
cardLayoutPanel.add(death,"loss");
layout.show(cardLayoutPanel, "loss");
}
}
//if the source is the option 2 button
}else{
if(option2.getDecision().toLowerCase().contains("run")){
//launches the running game
RunningPanel run = new RunningPanel(
player, tree, false);
cardLayoutPanel.add(run,"runningGame");
layout.show(cardLayoutPanel, "runningGame");
}else if(option2.getDecision().toLowerCase(
).contains("scamper")){
//launches the squirrel modified memory game
ModMemoryPanel memModS = new ModMemoryPanel(player, tree,
false, "Squirrel");
cardLayoutPanel.add(memModS,"squirrelModGame");
layout.show(cardLayoutPanel, "squirrelModGame");
}else if(option2.getDecision().toLowerCase(
).contains("name")){
//launches the fym modified memory game
ModMemoryPanel memModF = new ModMemoryPanel(player, tree,
false, "FYM");
cardLayoutPanel.add(memModF,"fymModGame");
layout.show(cardLayoutPanel, "fymModGame");
} else if(option2.getDecision().toLowerCase(
).contains("At last")){
//launches the tunnel modified memory game
ModMemoryPanel memModT = new ModMemoryPanel(player, tree,
false, "Tunnel");
cardLayoutPanel.add(memModT,"tunnelModGame");
layout.show(cardLayoutPanel, "tunnelModGame");
}else{
//add the associated points to the player's points
player.addAllScores(option2.getPoints());
//if the players points are still above zero,
//continue to the right child SituationPanel
if (player.isAboveZero()){
//incremements the tree so that the current
//Situation is the right child of the
//current Situation
tree.nextRight();
//shows a SituationPanel of the new current
//Situation
SituationPanel nextPanel = new SituationPanel(
player, tree);
cardLayoutPanel.add(nextPanel,"right");
layout.show(cardLayoutPanel, "right");
//if the player's points are not above zero
//after pressing the button, shows the death panel
}else{
DeathPanel death = new DeathPanel();
cardLayoutPanel.add(death,"loss");
layout.show(cardLayoutPanel, "loss");
}
}
}
//if there is no child Situation, proceeds to the
//graduation panel
}catch(ArrayIndexOutOfBoundsException e){
GraduationPanel win = new GraduationPanel();
cardLayoutPanel.add(win,"winPanel");
layout.show(cardLayoutPanel, "winPanel");
}
}
}
}