-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
467 lines (421 loc) · 17.1 KB
/
GUI.java
File metadata and controls
467 lines (421 loc) · 17.1 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Admin
*/
public class GUI extends javax.swing.JFrame {
/**
* Creates new form GUI
*/
private static int WIDTH = 800, HEIGHT = 400;
private int widthRect = 60;
private int space = 150;
int x = WIDTH;
Graphics g = null;
Random r;
Thread t, t2;
ArrayList<JButton> arr;
JButton bird;
int c1 = 0;
int up = 0;
int max = 0;
int v = 1;
int point = 0;
int birdy;
boolean isLose = false;
boolean isRunning = false;
boolean isPause = false;
public GUI() {
initComponents();
v = 3;
r = new Random();
arr = new ArrayList<>();
pnmain.setPreferredSize(new Dimension(WIDTH, HEIGHT));
t = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
for (int i = 0; i < arr.size(); i = i + 2) {
if (arr.get(i).getX() + widthRect < 200 && arr.get(i).getX() + widthRect >= 197) {
point++;
lbpoint.setText(point + "");
}
arr.get(i).setLocation(arr.get(i).getX() - v, arr.get(i).getY());
arr.get(i + 1).setLocation(arr.get(i + 1).getX() - v, arr.get(i + 1).getY());
if (isLose==false&&arr.get(i).getBounds().intersects(bird.getBounds()) || arr.get(i + 1).getBounds().intersects(bird.getBounds())) {
isLose=true;
int a = JOptionPane.showConfirmDialog(rootPane, "game over! continue?");
if (a == JOptionPane.NO_OPTION || a == JOptionPane.CANCEL_OPTION) {
System.exit(0);
} else {
clear();
isLose=false;
}
}
// pnmain.add(arr.get(i));
// pnmain.add(arr.get(i + 1));
}
if (c1 == 150) {
c1 = 0;
AddRect();
}
c1++;
try {
Thread.sleep(10);
//pnmain.removeAll();
// pnmain.repaint();
if (arr.get(0).getX() + widthRect <= 0) {
pnmain.remove(arr.get(1));
pnmain.remove(arr.get(0));
arr.remove(1);
arr.remove(0);
}
} catch (Exception e) {
}
}
}
});
t2 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
if (bird.getY() >= HEIGHT&&isLose==false) {
isLose=true;
int a = JOptionPane.showConfirmDialog(rootPane, "game over! continue?");
if (a == JOptionPane.NO_OPTION || a == JOptionPane.CANCEL_OPTION) {
System.exit(0);
} else {
clear();
isLose=false;
}
}
if (bird.getY() <= max) {
up = 0;
}
if (up == 0) {
bird.setLocation(bird.getX(), bird.getY() + 1);
} else {
bird.setLocation(bird.getX(), bird.getY() - 10);
}
try {
Thread.sleep(6);
} catch (Exception e) {
}
}
}
});
this.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent evt) {
System.out.println("isrunning"+isRunning);
System.out.println("ispause"+isPause);
if (isRunning == false) {
t.start();
t2.start();
isRunning = true;
} else {
if (isPause == true) {
t.resume();
t2.resume();
isPause = false;
} else {
if (evt.getKeyCode() == KeyEvent.VK_SPACE) {
max = bird.getY() - 70;
up = 1;
}
}
}
}
});
}
void Start() {
int option = JOptionPane.showConfirmDialog(rootPane, "do you want to continue?");
if (option == JOptionPane.NO_OPTION || option == JOptionPane.CANCEL_OPTION) {
clear();
} else {
LoadData();
}
}
void AddRect() {
int y1 = r.nextInt(HEIGHT - space - 50);
int y2 = y1 + space;
JButton bt1 = new JButton();
bt1.setBounds(WIDTH - widthRect, -10000, widthRect, y1);
JButton bt2 = new JButton();
bt2.setBounds(WIDTH - widthRect, y2, widthRect, HEIGHT - y2);
arr.add(bt1);
arr.add(bt2);
bt1.setFocusable(false);
bt2.setFocusable(false);
pnmain.add(bt1);
pnmain.add(bt2);
}
void LoadData() {
pnmain.removeAll();
pnmain.repaint();
arr.clear();
try {
File f = new File("data.txt");
if (f.isFile()) {
if (f.length() != 0) {
FileReader fr = new FileReader(f);
BufferedReader bf = new BufferedReader(fr);
String line = "";
while ((line = bf.readLine()) != null) {
// System.out.println(line);
String[] data = line.split("\\|");
//System.out.println(data[0]);
c1 = Integer.parseInt(data[0]);
up = Integer.parseInt(data[1]);
max = Integer.parseInt(data[2]);
v = Integer.parseInt(data[3]);
point = Integer.parseInt(data[4]);
birdy = Integer.parseInt(data[5]);
System.out.println(birdy);
bird = new JButton();
bird.setBounds(200, birdy, 40, 30);
Icon icon = new ImageIcon("bird1.png");
bird.setIcon(icon);
bird.setBackground(pnmain.getBackground());
pnmain.add(bird);
lbpoint.setText(point + "");
}
}
}
File f2 = new File("button.txt");
if (f2.isFile()) {
if (f2.length() != 0) {
FileReader fr = new FileReader(f2);
BufferedReader bf = new BufferedReader(fr);
String line = "";
while ((line = bf.readLine()) != null) {
System.out.println(line);
String[] data = line.split("\\|");
JButton bt = new JButton();
bt.setBounds(Integer.parseInt(data[0]), Integer.parseInt(data[1]),
Integer.parseInt(data[2]), Integer.parseInt(data[3]));
arr.add(bt);
pnmain.add(bt);
}
} else {
clear();
}
}
} catch (Exception e) {
System.out.println(e);
}
}
void clear() {
pnmain.removeAll();
pnmain.repaint();
arr.clear();
AddRect();
// isRunning = false;
c1 = 0;
up = 0;
max = 0;
v = 3;
point = 0;
birdy = HEIGHT / 2;
lbpoint.setText("0");
bird = new JButton();
bird.setBounds(200, birdy, 40, 30);
ImageIcon icon = new ImageIcon("bird1.png");
bird.setBackground(pnmain.getBackground());
bird.setIcon(icon);
pnmain.add(bird);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
pnmain = new javax.swing.JPanel();
jLabel3 = new javax.swing.JLabel();
btpause = new javax.swing.JButton();
btsave = new javax.swing.JButton();
btexit = new javax.swing.JButton();
lbpoint = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
pnmain.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
pnmainMouseClicked(evt);
}
});
javax.swing.GroupLayout pnmainLayout = new javax.swing.GroupLayout(pnmain);
pnmain.setLayout(pnmainLayout);
pnmainLayout.setHorizontalGroup(
pnmainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 487, Short.MAX_VALUE)
);
pnmainLayout.setVerticalGroup(
pnmainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 390, Short.MAX_VALUE)
);
jLabel3.setText("Point:");
btpause.setText("Pause");
btpause.setFocusable(false);
btpause.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btpauseActionPerformed(evt);
}
});
btsave.setText("Save");
btsave.setFocusable(false);
btsave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btsaveActionPerformed(evt);
}
});
btexit.setText("Exit");
btexit.setFocusable(false);
btexit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btexitActionPerformed(evt);
}
});
lbpoint.setText("0");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(btpause)
.addGap(36, 36, 36)
.addComponent(btsave)
.addGap(36, 36, 36)
.addComponent(jLabel3)
.addGap(18, 18, 18)
.addComponent(lbpoint)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 102, Short.MAX_VALUE)
.addComponent(btexit)
.addGap(52, 52, 52))
.addComponent(pnmain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(pnmain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(51, 51, 51)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(btpause)
.addComponent(btsave)
.addComponent(btexit)
.addComponent(lbpoint))
.addGap(0, 31, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void btpauseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btpauseActionPerformed
if (isPause == false) {
t.suspend();
t2.suspend();
btpause.setText("resume");
isPause = true;
} else {
t.resume();
t2.resume();
btpause.setText("pause");
isPause = false;
}
}//GEN-LAST:event_btpauseActionPerformed
private void pnmainMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pnmainMouseClicked
}//GEN-LAST:event_pnmainMouseClicked
private void btsaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btsaveActionPerformed
try {
FileWriter myWriter = new FileWriter("button.txt");
FileWriter myWriter1 = new FileWriter("data.txt");
String content = "";
String content1 = "";
for (int i = 0; i < arr.size(); i++) {
content += arr.get(i).getX() + "|" + arr.get(i).getY() + "|"
+ arr.get(i).getWidth() + "|" + arr.get(i).getHeight() + "\n";
}
content1 += c1 + "|" + up + "|" + max + "|" + v + "|" + point + "|" + bird.getY();
myWriter1.write(content1);
myWriter1.close();
myWriter.write(content);
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.err.println("An error occurred.");
e.printStackTrace();
}
t.suspend();
t2.suspend();
isPause = true;
}//GEN-LAST:event_btsaveActionPerformed
private void btexitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btexitActionPerformed
System.exit(0);
}//GEN-LAST:event_btexitActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
GUI gu = new GUI();
gu.setVisible(true);
gu.setSize(WIDTH, HEIGHT + 200);
gu.setResizable(false);
gu.Start();
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btexit;
private javax.swing.JButton btpause;
private javax.swing.JButton btsave;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel lbpoint;
private javax.swing.JPanel pnmain;
// End of variables declaration//GEN-END:variables
}