-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainFrame.java
275 lines (247 loc) · 8.52 KB
/
MainFrame.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
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.awt.event.ActionEvent;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.JTextArea;
import javax.swing.JSlider;
public class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JButton btnPlayButton;
private JLabel lblTitle;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
frame.setSize(2400, 1500);
frame.setMinimumSize(new Dimension(1100, 1000));
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MainFrame() {
setTitle("Snake 2.0");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setLayout(new GridBagLayout());
contentPane.setBackground(new Color(0, 0, 0));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagConstraints gbc = new GridBagConstraints();
btnPlayButton = new JButton("Play!");
btnPlayButton.setFont(new Font("Calibri", Font.PLAIN, 50));
btnPlayButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try
{
// The way I did the file paths here only works for my computer, but it works. Have to find a way to work for all computers later on.
String command[] = {"python", "..\\snake\\snakeGame\\py.py"};
ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
int exitCode = process.waitFor();
System.out.println("Exit Code: " + exitCode);
} catch (Exception ex)
{
ex.printStackTrace();
}
}
});
gbc.gridx = 1;
gbc.gridy = 1;
gbc.insets = new Insets(20,0,0,0);
gbc.ipady = 40;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.CENTER;
contentPane.add(btnPlayButton, gbc);
JButton btnTheme = new JButton("Change Theme");
btnTheme.setFont(new Font("Calibri", Font.PLAIN, 50));
gbc.gridx = 1;
gbc.gridy = 1;
contentPane.add(btnTheme, gbc);
btnTheme.setVisible(false);
lblTitle = new JLabel("Snake 2.0");
lblTitle.setVerticalAlignment(SwingConstants.BOTTOM);
lblTitle.setFont(new Font("Calibri", Font.PLAIN, 200));
lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
lblTitle.setForeground(new Color(255, 255, 255));
gbc.gridx = 1;
gbc.gridy = 0;
contentPane.add(lblTitle, gbc);
JButton btnBack = new JButton("Back <-");
btnBack.setFont(new Font("Calibri", Font.PLAIN, 50));
gbc.gridx = 0;
gbc.gridy = 0;
contentPane.add(btnBack, gbc);
btnBack.setVisible(false);
JButton btnSettings = new JButton("Settings");
btnSettings.setFont(new Font("Calibri", Font.PLAIN, 50));
gbc.gridx = 1;
gbc.gridy = 2;
contentPane.add(btnSettings, gbc);
JTextArea txtrCredits = new JTextArea();
txtrCredits.setEditable(false);
txtrCredits.setFont(new Font("Calibri", Font.PLAIN, 50));
txtrCredits.setBackground(Color.black);
txtrCredits.setForeground(Color.white);
txtrCredits.setText("Made by Alberto Svedvik and Tal Hlebnyak\r\nGUI: Alberto Svedvik\r\nGame Source Code: Tal Hlebnyak");
gbc.gridx = 1;
gbc.gridy = 1;
contentPane.add(txtrCredits, gbc);
txtrCredits.setVisible(false);
JButton btnCredits = new JButton("Credits");
btnCredits.setFont(new Font("Calibri", Font.PLAIN, 50));
gbc.gridx = 1;
gbc.gridy = 3;
contentPane.add(btnCredits, gbc);
JButton btnGuide = new JButton("Guide");
btnGuide.setFont(new Font("Calibri", Font.PLAIN, 50));
gbc.gridx = 1;
gbc.gridy = 4;
contentPane.add(btnGuide, gbc);
JTextArea txtrGuide = new JTextArea();
txtrGuide.setEditable(false);
txtrGuide.setFont(new Font("Calibri", Font.PLAIN, 50));
txtrGuide.setBackground(Color.black);
txtrGuide.setForeground(Color.white);
txtrGuide.setText("Red Fruit: Normal Points, Speed\r\n"
+ "Black Fruit: Game Over\r\n"
+ "Yellow Fruit: Triple Points\r\n"
+ "Cyan Fruit: Speed Boost\r\n"
+ "Purple Fruit: Teleport to a random place on the board\r\n"
+ "Red Square: Moving square that ends the game if touched\r\n");
gbc.gridx = 1;
gbc.gridy = 2;
contentPane.add(txtrGuide, gbc);
txtrGuide.setVisible(false);
JLabel lblVolume = new JLabel("Volume");
gbc.gridx = 1;
gbc.gridy = 2;
lblVolume.setBackground(Color.black);
lblVolume.setForeground(Color.white);
lblVolume.setFont(new Font("Calibri", Font.PLAIN, 50));
contentPane.add(lblVolume, gbc);
JSlider sliderVolume = new JSlider();
gbc.gridx = 1;
gbc.gridy = 3;
contentPane.add(sliderVolume, gbc);
lblVolume.setVisible(false);
sliderVolume.setVisible(false);
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnPlayButton.setVisible(true);
btnSettings.setVisible(true);
btnCredits.setVisible(true);
lblTitle.setVisible(true);
btnBack.setVisible(false);
txtrCredits.setVisible(false);
btnTheme.setVisible(false);
lblVolume.setVisible(false);
sliderVolume.setVisible(false);
btnGuide.setVisible(true);
txtrGuide.setVisible(false);
}
});
btnSettings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnPlayButton.setVisible(false);
btnSettings.setVisible(false);
lblTitle.setVisible(false);
btnBack.setVisible(true);
btnCredits.setVisible(false);
btnTheme.setVisible(true);
lblVolume.setVisible(true);
sliderVolume.setVisible(true);
btnGuide.setVisible(false);
txtrGuide.setVisible(false);
}
});
btnTheme.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(contentPane.getBackground() == Color.black) {
contentPane.setBackground(Color.white);
lblTitle.setForeground(Color.black);
txtrCredits.setBackground(Color.white);
txtrCredits.setForeground(Color.black);
txtrGuide.setBackground(Color.white);
txtrGuide.setForeground(Color.black);
} else {
contentPane.setBackground(Color.black);
lblTitle.setForeground(Color.white);
txtrCredits.setBackground(Color.black);
txtrCredits.setForeground(Color.white);
txtrGuide.setBackground(Color.black);
txtrGuide.setForeground(Color.white);
}
}
});
sliderVolume.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider) e.getSource();
int value = source.getValue();
System.out.println("Slider value changed: " + value);
}
});
btnCredits.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnPlayButton.setVisible(false);
btnSettings.setVisible(false);
lblTitle.setVisible(false);
btnBack.setVisible(true);
txtrCredits.setVisible(true);
btnCredits.setVisible(false);
lblVolume.setVisible(false);
btnGuide.setVisible(false);
txtrGuide.setVisible(false);
}
});
btnGuide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnPlayButton.setVisible(false);
btnSettings.setVisible(false);
lblTitle.setVisible(false);
btnBack.setVisible(true);
txtrCredits.setVisible(false);
btnCredits.setVisible(false);
lblVolume.setVisible(false);
btnGuide.setVisible(false);
txtrGuide.setVisible(true);
}
});
}
}