質問編集履歴

1

修正依頼があったため修正しました

2021/02/13 11:31

投稿

etcetera
etcetera

スコア24

test CHANGED
File without changes
test CHANGED
@@ -2,59 +2,349 @@
2
2
 
3
3
  ```java
4
4
 
5
+ //GameFrame ファイル
6
+
5
7
  import javax.swing.JFrame;
6
8
 
9
+
10
+
11
+ public class GameFrame extends JFrame {
12
+
13
+
14
+
15
+ public GameFrame() {
16
+
17
+
18
+
19
+ setLayout(null);
20
+
21
+ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
22
+
23
+ setSize(500,500);
24
+
25
+ setLocationRelativeTo(null);
26
+
27
+ MainPanel pn =new MainPanel();
28
+
29
+ add(pn);
30
+
31
+ setVisible(true);
32
+
33
+
34
+
35
+ }
36
+
37
+
38
+
39
+ public static void main(String[] args) {
40
+
41
+ GameFrame GF=new GameFrame();
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+ }
50
+
51
+
52
+
53
+ }
54
+
55
+
56
+
57
+ ```
58
+
59
+
60
+
61
+ ```java
62
+
63
+ //StartPanelファイル
64
+
65
+ import java.awt.Color;
66
+
67
+ import java.awt.Font;
68
+
69
+ import java.awt.FontMetrics;
70
+
71
+ import java.awt.Graphics;
72
+
73
+ import java.awt.event.ActionEvent;
74
+
75
+ import java.awt.event.ActionListener;
76
+
77
+ import java.awt.event.KeyAdapter;
78
+
79
+ import java.awt.event.KeyEvent;
80
+
81
+
82
+
83
+ import javax.swing.JButton;
84
+
85
+ import javax.swing.JPanel;
86
+
87
+
88
+
89
+ public class StartPanel extends JPanel implements ActionListener{
90
+
91
+ boolean in_game=true;
92
+
93
+ MainPanel mp;
94
+
95
+ JButton bt;
96
+
97
+
98
+
99
+ public StartPanel(MainPanel mp1) {
100
+
101
+
102
+
103
+ mp=mp1;
104
+
105
+ setLayout(null);
106
+
107
+ setBackground(Color.white);
108
+
109
+ Font f=new Font("SansSerif",Font.BOLD,20);
110
+
111
+ FontMetrics fm=getFontMetrics(f);
112
+
113
+ String str="遊び方";
114
+
115
+ int w=fm.stringWidth(str)+50;
116
+
117
+ int h=fm.getHeight()+20;
118
+
119
+ bt=new JButton(str);
120
+
121
+ bt.setFont(f);
122
+
123
+ bt.addActionListener(this);
124
+
125
+ bt.setSize(w,h);
126
+
127
+ bt.setLocation(250-w/2,400);
128
+
129
+ add(bt);
130
+
131
+ addKeyListener(new Key());
132
+
133
+ setFocusable(true);
134
+
135
+
136
+
137
+
138
+
139
+
140
+
141
+ }
142
+
143
+ @Override
144
+
145
+ public void paint(Graphics g) {
146
+
147
+ super.paint(g);
148
+
149
+ FontMetrics fm;
150
+
151
+ Font f;
152
+
153
+ String str1,str="Game Title";
154
+
155
+ int w,h;
156
+
157
+
158
+
159
+ f=new Font("Serif",Font.BOLD,25);
160
+
161
+ fm=g.getFontMetrics(f);
162
+
163
+ str1=str+"(Serif)";
164
+
165
+ w=fm.stringWidth(str1);
166
+
167
+ h=fm.getHeight();
168
+
169
+ g.setFont(f);
170
+
171
+ g.drawString(str1,250-w/2,h);
172
+
173
+
174
+
175
+ f=new Font("SansSerif",Font.BOLD,25);
176
+
177
+ fm=g.getFontMetrics(f);
178
+
179
+ str1=str+"(SansSerif)";
180
+
181
+ w=fm.stringWidth(str1);
182
+
183
+ h=h+fm.getHeight()+10;
184
+
185
+ g.setFont(f);
186
+
187
+ g.drawString(str1,250-w/2,h);
188
+
189
+
190
+
191
+
192
+
193
+ f=new Font("Dialog",Font.BOLD,25);
194
+
195
+ fm=g.getFontMetrics(f);
196
+
197
+ str1=str+"(Dialog)";
198
+
199
+ w=fm.stringWidth(str1);
200
+
201
+ h=h+fm.getHeight()+10;
202
+
203
+ g.setFont(f);
204
+
205
+ g.drawString(str1,250-w/2,h);
206
+
207
+
208
+
209
+ f=new Font("DialogInput",Font.BOLD,25);
210
+
211
+ fm=g.getFontMetrics(f);
212
+
213
+ str1=str+"(DialogInput)";
214
+
215
+ w=fm.stringWidth(str1);
216
+
217
+ h=h+fm.getHeight()+10;
218
+
219
+ g.setFont(f);
220
+
221
+ g.drawString(str1,250-w/2,h);
222
+
223
+
224
+
225
+ f=new Font("Serif",Font.PLAIN,20);
226
+
227
+ fm=g.getFontMetrics(f);
228
+
229
+ str1="ゲーム開始: 「s」キー";
230
+
231
+ w=fm.stringWidth(str1);
232
+
233
+ h=500-fm.getHeight()-10;
234
+
235
+ g.setFont(f);
236
+
237
+ g.drawString(str1,250-w/2,h-200);
238
+
239
+ //requestFocusInWindow();
240
+
241
+
242
+
243
+
244
+
245
+
246
+
247
+
248
+
249
+
250
+
251
+ }
252
+
253
+
254
+
255
+ public void actionPerformed(ActionEvent e) {
256
+
257
+ if(e.getSource()==bt) {
258
+
259
+ System.out.println("遊び方");
260
+
261
+ }
262
+
263
+ }
264
+
265
+
266
+
267
+ class Key extends KeyAdapter{
268
+
269
+
270
+
271
+ public void keyPressed(KeyEvent ke) {
272
+
273
+ if(ke.getKeyCode()==KeyEvent.VK_S) {
274
+
275
+ mp.state=1;
276
+
277
+
278
+
279
+ }
280
+
281
+ }
282
+
283
+ }
284
+
285
+
286
+
287
+ }
288
+
289
+
290
+
291
+ ```
292
+
293
+
294
+
295
+ ```java
296
+
297
+ //MainPanel ファイル
298
+
7
299
  import java.awt.BorderLayout;
8
300
 
9
301
 
10
302
 
11
303
  import javax.swing.JPanel;
12
304
 
13
- import java.awt.Color;
305
+
14
-
15
- import java.awt.Font;
306
+
16
-
17
- import java.awt.FontMetrics;
18
-
19
- import java.awt.Graphics;
20
-
21
- import java.awt.event.ActionEvent;
22
-
23
- import java.awt.event.ActionListener;
24
-
25
- import java.awt.event.KeyAdapter;
26
-
27
- import java.awt.event.KeyEvent;
28
-
29
-
30
-
31
- import javax.swing.JButton;
32
-
33
- import javax.swing.JPanel;
34
-
35
-
36
-
37
- public class GameFrame extends JFrame {
307
+ public class MainPanel extends JPanel implements Runnable {
308
+
38
-
309
+ StartPanel sp;
310
+
39
-
311
+ GamePanel gp;
312
+
40
-
313
+ GameClearPanel gcp;
314
+
315
+ GameOverPanel gop;
316
+
317
+ boolean in_game=true;//ゲーム実行中はtrue
318
+
319
+ public int state=0;//ゲーム状態
320
+
321
+ public int level=1;//ゲームレベル
322
+
323
+ int old_state=0;//直前のゲーム状態
324
+
325
+ Thread td;
326
+
327
+ int width=500;
328
+
329
+ int hight=500;
330
+
331
+
332
+
41
- public GameFrame() {
333
+ public MainPanel() {
42
-
43
-
44
-
45
- setLayout(null);
334
+
46
-
47
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
335
+
48
-
336
+
49
- setSize(500,500);
337
+ setSize(width,hight);
50
-
338
+
51
- setLocationRelativeTo(null);
339
+ setLayout(new BorderLayout());
52
-
340
+
53
- MainPanel pn =new MainPanel();
341
+ sp=new StartPanel(this);
54
-
342
+
55
- add(pn);
343
+ add(sp,BorderLayout.CENTER);
56
-
344
+
57
- setVisible(true);
345
+ td=new Thread(this);
346
+
347
+ td.start();
58
348
 
59
349
 
60
350
 
@@ -62,350 +352,90 @@
62
352
 
63
353
 
64
354
 
65
- public static void main(String[] args) {
355
+ public void run() {
356
+
66
-
357
+ while(in_game) {
358
+
359
+ try {
360
+
361
+ td.sleep(100);
362
+
363
+ }catch(InterruptedException e) {}
364
+
365
+ if(state!=old_state) {
366
+
367
+ if(old_state==0) {
368
+
369
+ remove(sp);
370
+
371
+ }
372
+
373
+ else if(old_state==1) {
374
+
375
+ remove(gp);
376
+
377
+ }
378
+
379
+ else if(old_state==2) {
380
+
381
+ remove(gcp);
382
+
383
+ }
384
+
385
+ else {
386
+
387
+ remove(gop);
388
+
389
+ }
390
+
391
+
392
+
393
+ //新しいパネルの追加
394
+
395
+ if (state==4) {
396
+
397
+ in_game=false;
398
+
399
+ }else if(state==0) {
400
+
401
+ sp=new StartPanel(this);
402
+
403
+ add(sp);
404
+
405
+ }else if(state==1) {
406
+
67
- GameFrame GF=new GameFrame();
407
+ gp=new GamePanel(this);
408
+
68
-
409
+ add(gp);
410
+
69
-
411
+ }
412
+
70
-
413
+ else if(state==2) {
414
+
71
-
415
+ gcp=new GameClearPanel(this);
416
+
72
-
417
+ add(gcp);
418
+
73
-
419
+ }
420
+
421
+ else if(state==3){
422
+
423
+ gop=new GameOverPanel(this);
424
+
425
+ add(gop);
426
+
427
+ }
428
+
429
+ validate();
430
+
431
+ old_state=state;
74
432
 
75
433
  }
76
434
 
77
-
435
+ }
78
436
 
79
437
  }
80
438
 
81
-
82
-
83
- public class MainPanel extends JPanel implements Runnable {
84
-
85
- StartPanel sp;
86
-
87
- GamePanel gp;
88
-
89
- GameClearPanel gcp;
90
-
91
- GameOverPanel gop;
92
-
93
- boolean in_game=true;//ゲーム実行中はtrue
94
-
95
- public int state=0;//ゲーム状態
96
-
97
- public int level=1;//ゲームレベル
98
-
99
- int old_state=0;//直前のゲーム状態
100
-
101
- Thread td;
102
-
103
- int width=500;
104
-
105
- int hight=500;
106
-
107
-
108
-
109
- public MainPanel() {
110
-
111
-
112
-
113
- setSize(width,hight);
114
-
115
- setLayout(new BorderLayout());
116
-
117
- sp=new StartPanel(this);
118
-
119
- add(sp,BorderLayout.CENTER);
120
-
121
- td=new Thread(this);
122
-
123
- td.start();
124
-
125
-
126
-
127
- }
128
-
129
-
130
-
131
- public void run() {
132
-
133
- while(in_game) {
134
-
135
- try {
136
-
137
- td.sleep(100);
138
-
139
- }catch(InterruptedException e) {}
140
-
141
- if(state!=old_state) {
142
-
143
- if(old_state==0) {
144
-
145
- remove(sp);
146
-
147
- }
148
-
149
- else if(old_state==1) {
150
-
151
- remove(gp);
152
-
153
- }
154
-
155
- else if(old_state==2) {
156
-
157
- remove(gcp);
158
-
159
- }
160
-
161
- else {
162
-
163
- remove(gop);
164
-
165
- }
166
-
167
- }
168
-
169
- //新しいパネルの追加
170
-
171
- if (state==4) {
172
-
173
- in_game=false;
174
-
175
- }
176
-
177
- else if(state==1) {
178
-
179
- gp=new GamePanel(this);
180
-
181
- add(gp);
182
-
183
- }
184
-
185
- else if(state==2) {
186
-
187
- gcp=new GameClearPanel(this);
188
-
189
- add(gcp);
190
-
191
- }
192
-
193
- else if(state==3){
194
-
195
- gop=new GameOverPanel(this);
196
-
197
- add(gop);
198
-
199
- }
200
-
201
- validate();
202
-
203
- old_state=state;
204
-
205
- }
206
-
207
439
  }
208
440
 
209
- }
210
-
211
-
212
-
213
- public class StartPanel extends JPanel implements ActionListener{
214
-
215
- boolean in_game=true;
216
-
217
- MainPanel mp;
218
-
219
- JButton bt;
220
-
221
-
222
-
223
- public StartPanel(MainPanel mp1) {
224
-
225
-
226
-
227
- mp=mp1;
228
-
229
- setLayout(null);
230
-
231
- setBackground(Color.white);
232
-
233
- Font f=new Font("SansSerif",Font.BOLD,20);
234
-
235
- FontMetrics fm=getFontMetrics(f);
236
-
237
- String str="遊び方";
238
-
239
- int w=fm.stringWidth(str)+50;
240
-
241
- int h=fm.getHeight()+20;
242
-
243
- bt=new JButton(str);
244
-
245
- bt.setFont(f);
246
-
247
- bt.addActionListener(this);
248
-
249
- bt.setSize(w,h);
250
-
251
- bt.setLocation(250-w/2,400);
252
-
253
- add(bt);
254
-
255
- addKeyListener(new Key());
256
-
257
- setFocusable(true);
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
- }
266
-
267
-
268
-
269
- public void paint(Graphics g) {
270
-
271
- super.paint(g);
272
-
273
- FontMetrics fm;
274
-
275
- Font f;
276
-
277
- String str1,str="Game Title";
278
-
279
- int w,h;
280
-
281
-
282
-
283
- f=new Font("Serif",Font.BOLD,25);
284
-
285
- fm=g.getFontMetrics(f);
286
-
287
- str1=str+"(Serif)";
288
-
289
- w=fm.stringWidth(str1);
290
-
291
- h=fm.getHeight();
292
-
293
- g.setFont(f);
294
-
295
- g.drawString(str1,250-w/2,h);
296
-
297
-
298
-
299
- f=new Font("SansSerif",Font.BOLD,25);
300
-
301
- fm=g.getFontMetrics(f);
302
-
303
- str1=str+"(SansSerif)";
304
-
305
- w=fm.stringWidth(str1);
306
-
307
- h=h+fm.getHeight()+10;
308
-
309
- g.setFont(f);
310
-
311
- g.drawString(str1,250-w/2,h);
312
-
313
-
314
-
315
-
316
-
317
- f=new Font("Dialog",Font.BOLD,25);
318
-
319
- fm=g.getFontMetrics(f);
320
-
321
- str1=str+"(Dialog)";
322
-
323
- w=fm.stringWidth(str1);
324
-
325
- h=h+fm.getHeight()+10;
326
-
327
- g.setFont(f);
328
-
329
- g.drawString(str1,250-w/2,h);
330
-
331
-
332
-
333
- f=new Font("DialogInput",Font.BOLD,25);
334
-
335
- fm=g.getFontMetrics(f);
336
-
337
- str1=str+"(DialogInput)";
338
-
339
- w=fm.stringWidth(str1);
340
-
341
- h=h+fm.getHeight()+10;
342
-
343
- g.setFont(f);
344
-
345
- g.drawString(str1,250-w/2,h);
346
-
347
-
348
-
349
- f=new Font("Serif",Font.PLAIN,20);
350
-
351
- fm=g.getFontMetrics(f);
352
-
353
- str1="ゲーム開始: 「s」キー";
354
-
355
- w=fm.stringWidth(str1);
356
-
357
- h=500-fm.getHeight()-10;
358
-
359
- g.setFont(f);
360
-
361
- g.drawString(str1,250-w/2,h-200);
362
-
363
- requestFocusInWindow();
364
-
365
-
366
-
367
-
368
-
369
- S
370
-
371
-
372
-
373
-
374
-
375
- }
376
-
377
-
378
-
379
- public void actionPerformed(ActionEvent e) {
380
-
381
- if(e.getSource()==bt) {
382
-
383
- System.out.println("遊び方");
384
-
385
- }
386
-
387
- }
388
-
389
-
390
-
391
- class Key extends KeyAdapter{
392
-
393
- public void KeyPressed(KeyEvent ke) {
394
-
395
- if(ke.getKeyCode()==KeyEvent.VK_S) {
396
-
397
- mp.state=1;
398
-
399
- System.out.println("キーを取得");
400
-
401
- }
402
-
403
- }
404
-
405
- }
406
-
407
-
408
-
409
- }
410
-
411
441
  ```