-
Notifications
You must be signed in to change notification settings - Fork 0
/
Paints.java
executable file
·373 lines (338 loc) · 11.9 KB
/
Paints.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.KeyEvent;
import javax.swing.ActionMap;
import javax.swing.ImageIcon;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.KeyStroke;
//Class Paint extends Methods class which extends MyPanel class
public class Paints extends Methods {
Values val; //object of Values class
int k = 0;
Boolean collision;
int generateTime = 500;
// Non static block
{
//anger level labels
angerLevelLeft = new JLabel();
angerLevelRight = new JLabel();
angerLevelTop = new JLabel();
angerLevelBottom = new JLabel();
//image icons
try {
anger1 = new ImageIcon("resources\\angerlevel1.png");
anger2 = new ImageIcon("resources\\angerlevel2.png");
anger3 = new ImageIcon("resources\\angerlevel3.png");
anger4 = new ImageIcon("resources\\angerlevel4.png");
anger5 = new ImageIcon("resources\\angerlevel5.png");
anger1side = new ImageIcon("resources\\angerlevel11.png");
anger2side = new ImageIcon("resources\\angerlevel22.png");
anger3side = new ImageIcon("resources\\angerlevel33.png");
anger4side = new ImageIcon("resources\\angerlevel44.png");
anger5side = new ImageIcon("resources\\angerlevel55.png");
} catch (Exception e) {
e.printStackTrace();
}
//setting icons and position of Jlabels
angerLevelLeft.setIcon(anger1side);
angerLevelRight.setIcon(anger1side);
angerLevelTop.setIcon(anger1);
angerLevelBottom.setIcon(anger1);
angerLevelBottom.setBounds(600, 560, 150, 15);
add(angerLevelBottom);
angerLevelTop.setBounds(600, 220, 150, 15);
add(angerLevelTop);
angerLevelLeft.setBounds(490, 320, 15, 150);
add(angerLevelLeft);
angerLevelRight.setBounds(860, 320, 15, 150);
add(angerLevelRight);
//Font for showing score
Font myFont = new Font("Serif", Font.BOLD, 48);
// Setting layout to null
setLayout(null);
// Adding mouse listener to panel
addMouseListener(this);
// JLabels for traffic signals declaration of which is given in MyPanel
// class
// Setting Icons for JLAbel
Left.setIcon(Red2);
Right.setIcon(Red3);
Top.setIcon(Red1);
Bottom.setIcon(Red1);
// Adding JLabels i.e traffic signals and Exit button
add(Left);
add(Right);
add(Top);
add(Bottom);
add(leftButton);
add(rightButton);
add(topButton);
add(bottomButton);
//Key Bindings
InputMap mp = this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
mp.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false),
"keyEscape");
mp.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false), "keyLeft");
mp.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), "keyUp");
mp.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false), "keyDown");
mp.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), "keyRight");
mp.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false), "spaceStop");
ActionMap act = this.getActionMap();
act.put("keyEscape", escape);
act.put("keyLeft", left);
act.put("keyUp", up);
act.put("keyDown", down);
act.put("keyRight", right);
act.put("spaceStop", Stop);
// setting bounds of Buttons and Labels and adding Mouse Listener
/*-----------------------------------------*/
Score.setText("" + score);
Score.setBounds(1250, 0, 300, 100);
Score.setFont(myFont);
Score.setForeground(Color.white);
add(Score);
/*-----------------------------------------*/
Left.setBounds(495, 267, 80, 25);
leftButton.setBounds(0, 290, 570, 220);
leftButton.setOpaque(false);
leftButton.setBorderPainted(false);
leftButton.setContentAreaFilled(false);
/*------------------------------------------*/
Right.setBounds(785, 500, 80, 25);
rightButton.setBounds(800, 290, 570, 220);
rightButton.setOpaque(false);
rightButton.setBorderPainted(false);
rightButton.setContentAreaFilled(false);
/*-------------------------------------------*/
Top.setBounds(547, 185, 25, 80);
topButton.setBounds(570, 0, 215, 285);
topButton.setOpaque(false);
topButton.setBorderPainted(false);
topButton.setContentAreaFilled(false);
/*-------------------------------------------*/
Bottom.setBounds(785, 525, 25, 80);
bottomButton.setBounds(570, 510, 235, 800);
bottomButton.setOpaque(false);
bottomButton.setBorderPainted(false);
bottomButton.setContentAreaFilled(false);
/*-------------------------------------------*/
leftButton.addMouseListener(this);
rightButton.addMouseListener(this);
topButton.addMouseListener(this);
bottomButton.addMouseListener(this);
// Initializing positions and number of cars of Left Side
initializationLeft(1);
// Initializing positions and number of cars of Right Side
initializationRight();
// Initializing positions and number of cars of Top Side
initializationTop();
// Initializing positions and number of cars of Bottom Side
initializationBottom(1);
}
// Paint Component method
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
//showing score
Score.setText("" + score * 10);
// Adding background image
g2d.drawImage(img, 0, 0, getWidth(), getHeight(), null);
//rectangles around cars for collision detection
Shape[] recLeft = new Shape[side1.size()];
for (int i = 0; i < recLeft.length; i++) {
recLeft[i] = new Rectangle(3, 3, 65, 25);
}
Shape[] recRight = new Shape[side2.size()];
for (int i = 0; i < recRight.length; i++) {
recRight[i] = new Rectangle(3, 3, 65, 25);
}
Shape[] recTop = new Shape[side3.size()];
for (int i = 0; i < recTop.length; i++) {
recTop[i] = new Rectangle(3, 3, 65, 25);
}
Shape[] recBottom = new Shape[side4.size()];
for (int i = 0; i < recBottom.length; i++) {
recBottom[i] = new Rectangle(3, 3, 65, 25);
}
// Left Side
// Side1.length is the number of cars to be generated
for (int i = 0; i < side1.size(); i++) {
// Provding arguments to method and making it equal to an object of
// Value class because it reurns object of value class
// First argument is used to tell method that it is left side
// 2nd argument is used to tell the method number of this car in the
// lane
// 3rd arument is used to tell methid that car wants to move left
// ,right or straight
// Green is used to tell compiler that which signal is open
// 5th and 6th argumenty is to tell the x and y position of car
// 7th argument is tell the method current angle of rotation
val = trans(1, i, side1.get(i) + 1, green, side1x.get(i),
side1y.get(i), side1Angle.get(i));
// Saving xpos to pass it to method next time
side1x.set(i, val.xpos);
// Saving ypos to pass it to method next time
side1y.set(i, val.ypos);
// Saving angle to pass it to method next time
side1Angle.set(i, val.angle);
if (side1x.get(i) > 425 && side1x.get(i) < 430) {
// score
score++;
passedLeft++;
}
recLeft[i] = val.at1.createTransformedShape(recLeft[i]);
// Drawing image of car
g2d.drawImage(carLeft.get(i).image, val.at1, this);
if (side1y.get(i) <= -100 && side1y.get(i) >= -103
|| side1x.get(i) >= 1400 && side1x.get(i) <= 1403
|| side1y.get(i) >= 800 && side1y.get(i) <= 803) {
//removing car when it has gone
recLeft[i]=null;
side1.remove(i);
side1x.remove(i);
side1y.remove(i);
side1Angle.remove(i);
carLeft.remove(i);
passedLeft--;
}
}
// Right side
// Side1.length is the number of cars to be generated
for (int i = 0; i < side2.size(); i++) {
// Provding arguments to method and making it equal to an object of
// Value class because it reurns object of value class
val = trans(2, i, side2.get(i) + 1, green, side2x.get(i),
side2y.get(i), side2Angle.get(i));
// Saving xpos to pass it to method next time
side2x.set(i, val.xpos);
// Saving Ypos to pass it to method next time
side2y.set(i, val.ypos);
// Saving angle to pass it to method next time
side2Angle.set(i, val.angle);
if (side2x.get(i) < 935 && side2x.get(i) > 930) {
score++;
passedRight++;
}
recRight[i] = val.at1.createTransformedShape(recRight[i]);
// Drawing image of car
g2d.drawImage(carRight.get(i).image, val.at1, this);
if (side2y.get(i) <= -100 && side2y.get(i) >= -104
|| side2x.get(i) <= 0 && side2x.get(i) >= -3
|| side2y.get(i) >= 800 && side2y.get(i) <= 803) {
//removing car when it has gone
recRight[i]=null;
side2.remove(i);
side2x.remove(i);
side2y.remove(i);
side2Angle.remove(i);
carRight.remove(i);
passedRight--;
}
}
// Top side
for (int i = 0; i < side3.size(); i++) {
// Provding arguments to method and making it equal to an object of
// Value class because it reurns object of value class
val = trans(3, i, side3.get(i) + 1, green, side3x.get(i),
side3y.get(i), side3Angle.get(i));
// Saving xpos to pass it to method next time
side3x.set(i, val.xpos);
// Saving Ypos to pass it to method next time
side3y.set(i, val.ypos);
// Saving angle to pass it to method next time
side3Angle.set(i, val.angle);
if (side3y.get(i) > 165 && side3y.get(i) < 170) {
score++;
passedTop++;
}
recTop[i] = val.at1.createTransformedShape(recTop[i]);
// Drawing image of car
g2d.drawImage(carTop.get(i).image, val.at1, this);
if (side3y.get(i) >= 800 && side3y.get(i) <= 803
|| side3x.get(i) <= 0 && side3x.get(i) >= -3
|| side3x.get(i) >= 1400 && side3x.get(i) <= 1403) {
//removing car when it has gone
recTop[i]=null;
side3.remove(i);
side3x.remove(i);
side3y.remove(i);
side3Angle.remove(i);
carTop.remove(i);
passedTop--;
}
}
// Bottom side
for (int i = 0; i < side4.size(); i++) {
// Provding arguments to method and making it equal to an object of
// Value class because it reurns object of value class
val = trans(4, i, side4.get(i) + 1, green, side4x.get(i),
side4y.get(i), side4Angle.get(i));
// Saving xpos to pass it to method next time
side4x.set(i, val.xpos);
// Saving ypos to pass it to method next time
side4y.set(i, val.ypos);
// Saving angle to pass it to method next time
side4Angle.set(i, val.angle);
if (side4y.get(i) < 625 && side4y.get(i) > 621) {
score++;
passedBottom++;
}
recBottom[i] = val.at1.createTransformedShape(recBottom[i]);
// Drawing image of car
g2d.drawImage(carBottom.get(i).image, val.at1, this);
if (side4y.get(i) <= 0 && side4y.get(i) >= -3 || side4x.get(i) <= 0
&& side4x.get(i) >= -3 || side4x.get(i) >= 1400
&& side4x.get(i) <= 1403) {
//removing car when it has gone
recBottom[i]=null;
side4.remove(i);
side4x.remove(i);
side4y.remove(i);
side4Angle.remove(i);
carBottom.remove(i);
passedBottom--;
}
}
angerLevelset();
collisionAll(recLeft, recRight, recTop, recBottom);
if (side1.size() > 18 || side2.size() > 18 || side3.size() > 18
|| side4.size() > 18) {
//Game Over Frame
frame = new JFrame();
GameOver gm = new GameOver(score);
frame.requestFocusInWindow();
frame.add(gm);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.setVisible(true);
//stoping music
GameMode.gamePanel.clip.stop();
GameMode.gamePanel=null;
TrafficMania.frame.setVisible(false);
}
//Regenerating cars
if (k == generateTime) {
initializationLeft(1);
initializationRight();
initializationTop();
initializationBottom(1);
if (generateTime > 20) {
generateTime -= 15;
}
}
angerLevelset(); //Showing anger level
if (k > generateTime) {
k = 0;
}
k++;
// Repainting
repaint();
}
}