-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGUI.java
More file actions
256 lines (169 loc) · 7.42 KB
/
GUI.java
File metadata and controls
256 lines (169 loc) · 7.42 KB
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
//this file contains the code for the buttons, JFrame, as well as basically the main game
// it uses the buttons to allow navigation between the branches
//fair warning rn the dimensions of the frames, labels, etc arent exactly top quality, but should work
// feel free to let us know if u have any suggestions for graphics and such, we can try to make that too
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI implements ActionListener {
//instance variables for GUI class
int count = 0;
//label contains the words for the next question/direction for each step
JLabel label;
//the frame contains the panel & labels, similar to a window
//panel is used add the buttons and labels on to the frame
JFrame frame;
JPanel panel;
//these are the two buttons, each of which have a label. button represents option1 and button2 represents option2
JButton button;
JButton button2;
//lab2 will contain the imported image on it
JLabel lab2;
// the class also has a player which is the main player going through the game
Player p;
// there is also a Dimension object that stores the size of the user's screen
Dimension size1;
//accessor methods for GUI class
public Dimension getDim(){
return size1;
}
public JLabel getlab1(){
return label;
}
public JFrame getframe(){
return frame;
}
public JPanel getPanel(){
return panel;
}
public JButton getBut1(){
return button;
}
public JButton getBut2(){
return button2;
}
public JLabel getLab2(){
return lab2;
}
public Player getPlayer(){
return p;
}
//this is the constructor for the GUI class, it takes a Player parameter which is the player in the main game
public GUI(Player p) {
this.p = p;
//this sets the default start and frame w/ dimensions etc this part could still change, but isnt terrible rn
//creating buttons using JButton
frame = new JFrame();
button = new JButton("Start Game");
button2 = new JButton("Start Game but 2~~");
lab2 = new JLabel();
//size1 stores the dimensions of the user's screen
size1 = Toolkit.getDefaultToolkit().getScreenSize();
//setting the button sizes using size1
button.setPreferredSize(new Dimension(size1.width,size1.height/7));
button2.setPreferredSize(new Dimension(size1.width,size1.height/7));
//.addActionListen will register when the button is clicked to perform an action
button.addActionListener(this);
button2.addActionListener(this);
label = new JLabel("Welcome!");
panel = new JPanel();
label.setSize(size1.width,size1.height/7);
panel.setPreferredSize(new Dimension(size1.width, size1.height)); //setting the panel to the size of the user's screen so that the frame is sized to be full screen
lab2.setBounds(0,100,100,100);
panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
panel.setLayout(new FlowLayout());
//adding our objects onto the panel to be displayed
panel.add(lab2);
panel.add(label);
panel.add(button);
panel.add(button2);
frame.add(panel, BorderLayout.CENTER);
//creating a border for our window
Border border = new LineBorder(Color.PINK, 4, true);
panel.setBorder(border);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Choose Your Own Adventure!");
frame.pack();
//this is what sets up the starting screen with the call to this method (below)
this.startView();
frame.setVisible(true);
}
// this method is used to start playing the game, simply call GUI.startGame(playerName) and it will start the window, etc
public static void startGame(Player p){
new GUI(p);
}
//this sets the players current option to 1
public void op1(){
p.setOp(1);
search();
}
//this sets the players current option to 2 and calls the search method
public void op2(){
p.setOp(2);
search();
}
//this part is for tracking the action/clicking of the buttons
public void actionPerformed(ActionEvent e) {
//if the top button is clicked, it calls op1 (explained above)
if(e.getSource()==button) {
op1();
}
//if the lower button is clicked, it calls op2 (explained above)
else{
op2();
}
}
//added method to set up the starting screen (labels and such subject to change, this is just a placeholder) When this method is called it will change
//the screen to show the starting screen
public void startView(){
Branch start = new Branch(0, 0, "Crazy Cool Adventure", "Barbie Charm School Adventure of your Dreams", "morgannn.PNG", "Welcome to choose your own adventure!! Which adventure would you like to begin? ");
this.convert(start);
}
//the search method will direct the player through the main branches of the story
// for example a main branch might be the "cave" branch, within the cave branch there would be multiple branch objects of story branches within cave
public void search(){
if(p.getCode1()==0){
if(p.getOp()==1){
Water.water0(this);
}
else{
BarbieSchoolInfo.intro0(this);
frame.setTitle("Barbie Princess Dream School Adventure");
}
}
else if((p.getCode1() == 1)||(p.getCode1()==5)||(p.getCode1()==6)){
BarbieSchoolInfo.findNextbarb(this);
}
else if(p.getCode1()==2){
Water.findNextWater(this);
}
else if(p.getCode1()==3){
School.findNextSchool(this);
}
else if(p.getCode1()==4){
Dorm.findNextDorm(this);
}
}
// this method converts the parameters of the GUI class, updating the labels, image, title, etc
// it takes a branch object which will contain all of this imformation
public void convert(Branch b){
p.setLoc(b.getName());
p.setCode1(b.getCode1());
p.setCode2(b.getCode2());
if(p.getCode1()==2){
frame.setTitle(p.getLoc());
}
label.setText(b.getLabel());
button.setLabel(b.getOp1());
button2.setLabel(b.getOp2());
PutImage.displayImage(frame,b.getImage(), lab2, size1);
}
}