-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatriots-4.java
More file actions
359 lines (312 loc) · 15.4 KB
/
Patriots-4.java
File metadata and controls
359 lines (312 loc) · 15.4 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
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
/*Sudipti Dantuluri
* 4.12.2022
* Patriots.java
* Final game project.*/
import java.awt.BorderLayout;
import java.awt.CardLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.*; import java.awt.event.*;
import javax.swing.*; import javax.swing.event.*;
public class Patriots extends JFrame
{
//Main method
Practice pc = new Practice();
public static void main(String[] args)
{
Patriots p = new Patriots();
}
public Patriots()
{
super("Patriots");
setSize( 900, 900);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocation(400,50);
setResizable(true);
JMenuBar menubar = new JMenuBar();
setJMenuBar(menubar);
JMenu file = new JMenu("File");
menubar.add(file);
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
//JMenu "exit" button
class exitAction implements ActionListener {
public void actionPerformed (ActionEvent e) {
System.exit(0);
}
}
exit.addActionListener(new exitAction());
setContentPane( pc ); // OR frame.getContentPane().add(p_in);
setVisible(true);
}
//Practice class
class Practice extends JPanel
{
JTextArea jtext;
JLabel jlabel1;
JLabel jlabel2;
JLabel jlabel3, jlabel4, jlabel5, jlabel6, jlabel7, jlabel8;
JLabel jlabelImg;
JButton btn3,btn4;
JPanel tPanel;
int team1Num = 0;
int team2Num = 0;
private JButton startButton, buttonPlay, introButton;
private JTextArea t1, howToPlayDescription;
String sent = "\t\t Press Start";
Font headerFont = new Font("Serif", Font.BOLD, 35);
Font descriptionFont = new Font("Serif", Font.PLAIN, 20);
Font introDescriptionFont = new Font("Arial", Font.PLAIN, 5);
private JCheckBox checkBox1, checkBox2, checkBox3;
private JLabel introDescription;
CardLayout cardLayout1 = new CardLayout();
public Practice()
{
mainMenu();
}
public void mainMenu()
{
//cardLayout1 = new CardLayout();
//Image startPageBackground = new ImageIcon("frontPageBackground.jpg").getImage();
ImageIcon startPageBackground = new ImageIcon("frontPageBackground.jpg");
mainPanel mp = new mainPanel();
GamePanel gp = new GamePanel();
Settings setting = new Settings();
Instructions instruction = new Instructions();
setLayout(cardLayout1);
add(mp , "Start Screen");
add(gp, "Game Panel");
add(setting, "Settings Panel");
add(instruction, "Instructions Panel");
//frontPage();
}
class mainPanel extends JPanel
{
mainPanel()
{
setLayout(null);
JButton startButton = new JButton("Play");
add(startButton);
startButton.setBounds(0, 0, 150, 50);
startButton.addActionListener(new sbuttonListener());
JButton settingsButton = new JButton("Settings");
add(settingsButton);
settingsButton.setBounds(150, 0, 150, 50);
settingsButton.addActionListener(new sbuttonListener());
JButton instructionsButton = new JButton("Instructions");
add(instructionsButton);
instructionsButton.setBounds(300, 0, 150, 50);
instructionsButton.addActionListener(new sbuttonListener());
setBackground(Color.PINK);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//Image stickman = new ImageIcon("stickman.jpg").getImage();
Image startPageBackground = new ImageIcon("frontPageBackground.jpg").getImage();
g.drawImage(startPageBackground, 0, 0, 900,900, null);
}
}
class GamePanel extends JPanel {
GamePanel()
{
setLayout(null);
JButton startButton = new JButton("ready to play!");
add(startButton);
startButton.setBounds(50, 50, 150, 50);
startButton.addActionListener(new sbuttonListener());
JLabel introHeader = new JLabel("<html>" + "Introduction" + "<br>" + "</html>");
introHeader.setHorizontalAlignment(JLabel.CENTER);
introDescription = new JLabel();
introDescription.setText("<html>"+"<body>"+ "<h1>" + "Introduction" + "</h1>" + "<p>" + "The US has been invaded by bots and needs your help. You have been recruited by the military." + "<br>" + "Each level, your goal is to destroy the bots and then you will be promoted a new role." + "<br>" + "For the first level, you will have to defeat the bot with your given weapon and watch out for" + "<br>" + "your health. When your health bar reaches zero, you lose; so shoot the bot until it’s health bar zero." + "<br>" + "Once you win the first level, in the second level you will be able to access more weapons" + "<br>" + "and equipment and have more bots to shoot. In the last level, you will be promoted to a navy SEAL officer, so" + "<br>" + "you will have to be aware of health and oxygen levels. Swim up to the surface to regenerate your oxygen levels." + "</p>" + "</body>" + "</html>");
introDescription.setHorizontalAlignment(JLabel.CENTER);
introDescription.setPreferredSize(new Dimension (580, 450));
add(introDescription);
setBackground(Color.PINK);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//Image stickman = new ImageIcon("stickman.jpg").getImage();
//Image startPageBackground = new ImageIcon("frontPageBackground.jpg").getImage();
//g.drawImage(startPageBackground, 0, 0, 900,900, null);
}
}
class Settings extends JPanel {
Settings()
{
setLayout(null);
introDescription = new JLabel();
introDescription.setText("<html>"+"<body>"+ "<h1>" + "Settings" + "</h1>" + "<p>" + "Make sure all your setting are correct before starting the game." + "<br>" + "Have good time!" + "</p>" + "</body>" + "</html>");
introDescription.setHorizontalAlignment(JLabel.CENTER);
introDescription.setPreferredSize(new Dimension (580, 450));
introDescription.setBounds(50, 50, 250, 150);
add(introDescription);
setBackground(Color.BLUE);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//Image stickman = new ImageIcon("stickman.jpg").getImage();
//Image startPageBackground = new ImageIcon("frontPageBackground.jpg").getImage();
//g.drawImage(startPageBackground, 0, 0, 900,900, null);
}
}
class Instructions extends JPanel {
Instructions()
{
setLayout(null);
introDescription = new JLabel();
introDescription.setText("<html>"+"<body>"+ "<h1>" + "Introduction" + "</h1>" + "<p>" + "The US has been invaded by bots and needs your help. You have been recruited by the military." + "<br>" + "Each level, your goal is to destroy the bots and then you will be promoted a new role." + "<br>" + "For the first level, you will have to defeat the bot with your given weapon and watch out for" + "<br>" + "your health. When your health bar reaches zero, you lose; so shoot the bot until it’s health bar zero." + "<br>" + "Once you win the first level, in the second level you will be able to access more weapons" + "<br>" + "and equipment and have more bots to shoot. In the last level, you will be promoted to a navy SEAL officer, so" + "<br>" + "you will have to be aware of health and oxygen levels. Swim up to the surface to regenerate your oxygen levels." + "</p>" + "</body>" + "</html>");
introDescription.setHorizontalAlignment(JLabel.CENTER);
introDescription.setPreferredSize(new Dimension (580, 450));
introDescription.setBounds(50, 50, 550, 450);
add(introDescription);
setBackground(Color.GREEN);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//Image stickman = new ImageIcon("stickman.jpg").getImage();
//Image startPageBackground = new ImageIcon("frontPageBackground.jpg").getImage();
//g.drawImage(startPageBackground, 0, 0, 900,900, null);
}
}
class sbuttonListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
String sbutton = e.getActionCommand();
if(sbutton.equalsIgnoreCase("Play")) {
pc.cardLayout1.show(pc, "Game Panel");
System.out.println("play button");
} else if(sbutton.equalsIgnoreCase("Settings")) {
pc.cardLayout1.show(pc, "Settings Panel");
System.out.println("Settings button");
} else if(sbutton.equalsIgnoreCase("Instructions")) {
pc.cardLayout1.show(pc, "Instructions Panel");
System.out.println("Instructions button");
}
}
}
//Method for front page
public void frontPage()
{
JPanel panel = new JPanel();
JPanel panel1 = new JPanel(new BorderLayout());
JPanel panel2 = new JPanel(new BorderLayout());
JPanel panel3 = new JPanel();
JPanel btnPanel = new JPanel();
tPanel = new JPanel();
JPanel check = new JPanel(new GridLayout(3,1,0,0));
JPanel tPanel2 = new JPanel();
JPanel bruh = new JPanel();
//JButton btn3 = new JButton("Play");
//JButton btn4 = new JButton("How To Play");
//JButton btn5 = new JButton("Introduction");
CardLayout cardLayout = new CardLayout();
panel.setLayout(cardLayout);
//Front page panel
t1 = new JTextArea();
//t1.setFont(headerFont);
t1.setText("\n\n\n " + sent);
t1.setOpaque(true);
//t1.setBackground(Color.MAGENTA);
t1.setPreferredSize(new Dimension (600, 600));
tPanel.add( t1 );
//tPanel.add( jlabelImg );
howToPlayDescription= new JTextArea();
howToPlayDescription.setFont(headerFont);
JLabel howToPlayHeader = new JLabel("<html>" + "How To Play" + "<br>" + "</html>");
howToPlayHeader.setHorizontalAlignment(JLabel.CENTER);
//Description for what keys to use
howToPlayDescription.setFont(headerFont);
howToPlayDescription.setText("First level:\nSpace bar = jump up\n“Left” arrow = left\n“Right” arrow = right\n“w” key = shoot\n\nSecond level:\nSpace bar = rocket\n“w” key = shoot\n\nThird level:\n“Up” arrow = up\n“Down” arrow = down\n“Left” arrow = left\n“Right” arrow = right\n“w” key = shoot ");
howToPlayDescription.setPreferredSize(new Dimension (400, 450));
tPanel2.add( howToPlayDescription );
tPanel2.add( howToPlayHeader );
tPanel2.add(howToPlayHeader, BorderLayout.NORTH);
introDescription = new JLabel();
//introHeader.setFont(headerFont);
JLabel introHeader = new JLabel("<html>" + "Introduction" + "<br>" + "</html>");
introHeader.setHorizontalAlignment(JLabel.CENTER);
//Introduction
introDescription.setFont(introDescriptionFont);
introDescription.setText("<html>"+"<body>"+ "<h1>" + "Introduction" + "</h1>" + "<p>" + "The US has been invaded by bots and needs your help. You have been recruited by the military." + "<br>" + "Each level, your goal is to destroy the bots and then you will be promoted a new role." + "<br>" + "For the first level, you will have to defeat the bot with your given weapon and watch out for" + "<br>" + "your health. When your health bar reaches zero, you lose; so shoot the bot until it’s health bar zero." + "<br>" + "Once you win the first level, in the second level you will be able to access more weapons" + "<br>" + "and equipment and have more bots to shoot. In the last level, you will be promoted to a navy SEAL officer, so" + "<br>" + "you will have to be aware of health and oxygen levels. Swim up to the surface to regenerate your oxygen levels." + "</p>" + "</body>" + "</html>");
introDescription.setHorizontalAlignment(JLabel.CENTER);
introDescription.setPreferredSize(new Dimension (580, 450));
bruh.add( introDescription );
bruh.add( introHeader );
//bruh.add(introHeader, BorderLayout.NORTH);
//Buttons and adding their action listeners
startButton = new JButton("Start");
//startListener b1 = new startListener();
//startButton.addActionListener( b1 );
//startButton.addActionListener(e -> cardLayout.next(panel));
buttonPlay = new JButton("How To Play");
//instructionListener b2 = new instructionListener();
//buttonPlay.addActionListener( b2 );
//buttonPlay.addActionListener(e -> cardLayout.next(panel));
//playListener pl = new playListener();
//buttonPlay.addActionListener( pl );
introButton = new JButton("Introduction");
//introListener b3 = new introListener();
//introButton.addActionListener( b3 );
btnPanel.add(startButton);
btnPanel.add(buttonPlay);
btnPanel.add(introButton);
panel.add(panel1, "link1");
panel.add(panel2, "link2");
panel.add(panel3, "link3");
startButton.addActionListener(e -> cardLayout.show(panel, "link2"));
buttonPlay.addActionListener(e -> cardLayout.show(panel, "link2"));
introButton.addActionListener(e -> cardLayout.show(panel, "link3"));
//add(btn3);
//add(btn4);
//add(btn5);
panel1.add(tPanel, BorderLayout.SOUTH);
add(btnPanel, BorderLayout.NORTH); //panel1.add(btnPanel, BorderLayout.CENTER);
panel2.add(check, BorderLayout.WEST);
panel2.add(tPanel2, BorderLayout.CENTER);
panel3.add(introDescription, BorderLayout.CENTER);
add(panel);
}
//Listener for "start"
class startListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
sent = "Game";
t1.setText("\n\n\n " + sent);
//frontPageBackground(g);
//frame.getContentPane().add(kDimLightLabel);
tPanel.remove(t1);//remove component
tPanel.add( jlabelImg );
repaint();
}
}
//Listener for "how to play"
class playListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//sent = "How to play listner";
//t1.setText("\n\n\n " + sent);
sent = "First level:\nSpace bar = jump up\n“Left” arrow = left\n“Right” arrow = right\n“w” key = shoot\n\nSecond level:\nSpace bar = rocket\n“w” key = shoot\n\nThird level:\n“Up” arrow = up\n“Down” arrow = down\n“Left” arrow = left\n“Right” arrow = right\n“w” key = shoot ";
tPanel.remove(jlabelImg);//remove component
tPanel.add( t1 );
t1.setText(sent);
repaint();
}
}
//Listener for "introduction"
class introListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
sent = "The US has been invaded by bots and needs your help. You have been recruited by the military.\nEach level, your goal is to destroy the bots and then you will be promoted a new role.\nFor the first level, you will have to defeat the bot with your given weapon and watch out for\nyour health. When your health bar reaches zero, you lose; so shoot the bot until it’s health bar\nis zero. Once you win the first level, in the second level you will be able to access more\nweapons and equipment and have more bots to shoot. In the last level, you will be promoted\nto a navy SEAL officer, so you will have to be aware of health and oxygen levels. Swim up to\nthe surface to regenerate your oxygen levels.";
tPanel.remove(jlabelImg);//remove component
tPanel.add( t1 );
t1.setText(sent);
repaint();
}
}
}
}