-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.java
More file actions
251 lines (200 loc) · 7.06 KB
/
Window.java
File metadata and controls
251 lines (200 loc) · 7.06 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Dimension;
import java.awt.Image;
/**
*. class that creates and controls the window
*.
*. @author Cyrine Ben Ayed and Jessica Feng
*. @version Spring 2022
*/
public class Window extends JFrame{
/** panels containing different components of the window*/
JPanel controls = new JPanel();
JPanel image = new JPanel();
JPanel choices = new JPanel();
JLabel text = new JLabel("", null, JLabel.CENTER);
/** the buttons that we'll be using*/
JButton yesButton = new JButton("YES");
JButton noButton = new JButton("NO");
JButton exitButton = new JButton("Exit");
JButton helpButton = new JButton("Help");
JButton anyButton = new JButton("Invisible button");
/** checks for the user's response */
String response = "";
/** the contructor */
public Window(String name) {
super(name);
}
/** method to set up our window components */
public void setUp() {
//set the buttons background and font
yesButton.setBackground(Color.GREEN);
yesButton.setOpaque(true);
yesButton.setFont(new Font("SansSerif", Font.BOLD, 12));
noButton.setOpaque(true);
noButton.setBackground(Color.RED);
noButton.setFont(new Font("SansSerif", Font.BOLD, 12));
// set the buttons and image panels
controls.setLayout(new FlowLayout());
image.setLayout(new FlowLayout());
choices.setLayout(new FlowLayout());
// set the text
text.setForeground(Color.black);
text.setFont(new Font("SansSerif", Font.BOLD, 16));
text.setAlignmentX(0.5f);
text.setAlignmentY(0.5f);
text.setVerticalAlignment(text.CENTER);
text.setLayout(new FlowLayout());
//Add exit and help to the experiment layout
controls.add(exitButton);
controls.add(helpButton);
//Add yes and no controls to set up the component orientation in the experiment layout
choices.add(yesButton);
choices.add(noButton);
// add the bakcground image
image.add(text);
//adds all panels to the frame
this.getContentPane().add(controls, BorderLayout.NORTH);
this.getContentPane().add(image, BorderLayout.CENTER);
this.getContentPane().add(choices, BorderLayout.SOUTH);
//Process the Yes button press
yesButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//for debugging
// String command = "Click yes!!";
// System.out.println(command);
//click on anyButton
anyButton.setActionCommand("yes clicked");
anyButton.doClick();
}
});
//Process the No button press
noButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//for debugging
// String command = "Click no!!";
// System.out.println(command);
// click on anyButton
setResponse("no");
anyButton.setActionCommand("no clicked");
anyButton.doClick();
}
});
anyButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//for debugging
// System.out.println(anyButton.getActionCommand());
//process yes vs no click
if (anyButton.getActionCommand().equals("yes clicked")){
setResponse("yes");
// System.out.println(getResponse());
} else {
setResponse("no");
// System.out.println(getResponse());
}
synchronized (anyButton) {
anyButton.notifyAll();
}
}
});
//Process the Exit button press
exitButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//for debugging
// String command = "Click Exit!!";
// System.out.println(command);
// exit the game
System.exit(0);
}
});
//Process the restart button press
helpButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//for debugigng
// String command = "help!!";
// System.out.println(command);
// show them their scores
String helpMsg = "Read the prompt and press the yes or no button depending on your decision. There are 8 days in the game by the end of which you'll get your player report";
JOptionPane.showMessageDialog(Game.win, "<html><p style=\"width:400px\">"+helpMsg+"</p></html>");
}
});
}
/**
*. changes the background image and text
*. @param imgFile the path of the image we want to work with
*. @param message the text
*/
public void updateComponents(String imgFile, String message, boolean show) {
// change the background image
ImageIcon temp = new ImageIcon(imgFile);
Image temp2 = temp.getImage().getScaledInstance((int)600, (int)400, Image.SCALE_SMOOTH);
ImageIcon img = new ImageIcon(temp2);
//change the text
text.setText("<html><p style=\"width:400px\">"+message+"</p></html>");
//update
text.setIcon(img);
text.setVerticalTextPosition(JLabel.BOTTOM);
text.setHorizontalTextPosition(JLabel.CENTER);
choices.setVisible(show);
}
/** @return the response of the user */
public String getResponse() {
return this.response;
}
/** sets the response field */
public String setResponse(String str) {
this.response = str;
return response;
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*. @param imgFile the path to the image
*/
private void createAndShowGUI(String imgFile, String message) {
Dimension winsize = new Dimension(600,700);
//setting for the window
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content panel
this.setUp();
this.updateComponents(imgFile, message, false);
this.setPreferredSize(winsize);
this.setMinimumSize(winsize);
this.getContentPane().setPreferredSize(new Dimension(600, 400));
//Display the window.
this.pack();
this.setVisible(true);
}
/*
*. creates and shows the window
*. @param the path to the image file
*/
public void showWindow(String imgFile, String message){
/* Use an appropriate Look and Feel */
try {
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);
//Schedule a job for the event dispatchi thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI(imgFile, message);
}
});
}
}