回答編集履歴

7

追記

2018/08/20 20:41

投稿

umyu
umyu

スコア5846

test CHANGED
File without changes

6

追記

2018/08/20 20:41

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -475,3 +475,103 @@
475
475
  あとクラス間でイベントの通知をしたい時は[PropertyChangeListener](https://docs.oracle.com/javase/jp/10/docs/api/java/beans/PropertyChangeListener.html)が使えます。
476
476
 
477
477
  参考:[他のクラスからJFrameのラベルのテキストを変更](https://teratail.com/questions/107501)
478
+
479
+
480
+
481
+ ---
482
+
483
+
484
+
485
+ > カウントを初期化
486
+
487
+
488
+
489
+ ```Java
490
+
491
+ public class ScreenToucher extends JFrame implements ActionListener {
492
+
493
+ public void addCount() {
494
+
495
+ p1.addCount();
496
+
497
+ }
498
+
499
+ }
500
+
501
+
502
+
503
+ class ScorePanel extends JPanel {
504
+
505
+ // privateに変数名をiからscoreに変更
506
+
507
+ private int score = 0;
508
+
509
+ // finalで代入を抑止
510
+
511
+ private final JLabel l1;
512
+
513
+
514
+
515
+ ScorePanel() {
516
+
517
+ JPanel p1 = new JPanel();
518
+
519
+ l1 = new JLabel();
520
+
521
+ p1.setBackground(Color.green);
522
+
523
+ // 1行に
524
+
525
+ l1.setText(Integer.toString(score));
526
+
527
+ p1.add(l1);
528
+
529
+ add(p1);
530
+
531
+ }
532
+
533
+ // addCountを追加
534
+
535
+ public void addCount() {
536
+
537
+ score += 100;
538
+
539
+ l1.setText(Integer.toString(score));
540
+
541
+ }
542
+
543
+ // setCountで内部の変数を同期
544
+
545
+ public void setCount(int count) {
546
+
547
+ score = count;
548
+
549
+ l1.setText(Integer.toString(score));
550
+
551
+ }
552
+
553
+
554
+
555
+ }
556
+
557
+
558
+
559
+ class MyPanel3 extends JPanel {
560
+
561
+ void MouseListener() {
562
+
563
+ addMouseListener(new MouseAdapter() {
564
+
565
+ public void mousePressed(MouseEvent e) {
566
+
567
+ // 前略
568
+
569
+ repaint();
570
+
571
+ // Count()メソッドと変数:iは削除。
572
+
573
+ toucher2.addCount();
574
+
575
+ }
576
+
577
+ ```

5

追記

2018/08/20 20:40

投稿

umyu
umyu

スコア5846

test CHANGED
File without changes

4

追記

2018/08/20 10:05

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -56,7 +56,7 @@
56
56
 
57
57
 
58
58
 
59
- Swingでゲーム作成は
59
+ `Swing`でゲーム作成は
60
60
 
61
61
 
62
62
 
@@ -64,7 +64,7 @@
64
64
 
65
65
  1-a,表示したい画面毎に`JPanel`または`JLayeredPane`を継承したクラスを作成する
66
66
 
67
-  この時`paintComponent`はできるだけオーバーライドしない。
67
+  この時`paintComponent`をオーバーライドすると、画面項目の座標計算を自力で行う必要が発生するので、パフォーマンスがシビアに求められている部分以外はできるだけオーバーライドしない。
68
68
 
69
69
   文字列を表示したい時は`JLabel`を使う。
70
70
 
@@ -76,11 +76,11 @@
76
76
 
77
77
 
78
78
 
79
- 2,定期的に処理を行いたい時
80
-
81
- `EDT(Event Dispatch Thread)`の関係上、[Thread](https://docs.oracle.com/javase/jp/10/docs/api/java/lang/Thread.html)クラスを使わず、[javax.swing.Timer](https://docs.oracle.com/javase/jp/10/docs/api/javax/swing/Timer.html)と[SwingWorker](https://docs.oracle.com/javase/jp/10/docs/api/javax/swing/SwingWorker.html)を使う。
82
-
83
- 3,画面作成は`WindowBuilder`を作る
79
+ 2,ゲームの制限時間や定期的に処理を行いたい時
80
+
81
+ `Swing`の`EDT(Event Dispatch Thread)`の関係上、[Thread](https://docs.oracle.com/javase/jp/10/docs/api/java/lang/Thread.html)クラスを使わず、[javax.swing.Timer](https://docs.oracle.com/javase/jp/10/docs/api/javax/swing/Timer.html)と[SwingWorker](https://docs.oracle.com/javase/jp/10/docs/api/javax/swing/SwingWorker.html)を使う。
82
+
83
+ 3,画面作成は`WindowBuilder`を使う。
84
84
 
85
85
 
86
86
 

3

追記

2018/08/20 10:05

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -250,14 +250,20 @@
250
250
 
251
251
  case MENU:
252
252
 
253
+ // ゲーム開始ボタンをクリック時に画面遷移
254
+
253
255
  showPanel(GameMode.PLAY);
254
256
 
257
+ // 制限時間タイマーを起動
258
+
255
259
  timer.start();
256
260
 
257
261
  break;
258
262
 
259
263
  default:
260
264
 
265
+ // ゲームオーバー時に何らかの処理をしたいなら、caseの条件を追加して同じような形で。
266
+
261
267
  break;
262
268
 
263
269
  }

2

サンプルコードを追加。

2018/08/20 09:54

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -48,8 +48,6 @@
48
48
 
49
49
  }
50
50
 
51
-
52
-
53
51
  ```
54
52
 
55
53
 
@@ -87,3 +85,387 @@
87
85
 
88
86
 
89
87
  この3ルールを意識すると開発しやすいと思います。
88
+
89
+
90
+
91
+ ---
92
+
93
+
94
+
95
+ ゆるふわで質問文のコードを書き換えました。サンプルコードです、ご参考まで。
96
+
97
+
98
+
99
+ ```Java
100
+
101
+ import javax.swing.JFrame;
102
+
103
+
104
+
105
+ import javax.swing.*;
106
+
107
+ import java.awt.*;
108
+
109
+ import java.awt.event.*;
110
+
111
+
112
+
113
+ // ゲームモード
114
+
115
+ enum GameMode {
116
+
117
+ MENU, PLAY, OVER,
118
+
119
+ }
120
+
121
+
122
+
123
+ public class ScreenToucher extends JFrame implements ActionListener {
124
+
125
+
126
+
127
+ int i = 0;
128
+
129
+ // private finalにして代入を抑止する
130
+
131
+ private final static int width = 500;
132
+
133
+ private final static int height = 500;
134
+
135
+ private ScorePanel p1;
136
+
137
+ private GameMode mode = GameMode.MENU;
138
+
139
+ // CardLayoutを使って切り替え
140
+
141
+ private final JPanel gamePanel = new JPanel(new CardLayout());
142
+
143
+ private Timer timer; // タイマー
144
+
145
+
146
+
147
+ public static void main(String args[]) {
148
+
149
+ // mainスレッドなので、SwingUtilities#invokeLaterを使う。
150
+
151
+ SwingUtilities.invokeLater(() -> {
152
+
153
+ ScreenToucher frame = new ScreenToucher("Screen Toucher");
154
+
155
+ frame.setVisible(true);
156
+
157
+ });
158
+
159
+ }
160
+
161
+
162
+
163
+ ScreenToucher(String title) {
164
+
165
+ setTitle(title);
166
+
167
+ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
168
+
169
+ setBounds(100, 100, width, height);
170
+
171
+
172
+
173
+ p1 = new ScorePanel();
174
+
175
+
176
+
177
+ JPanel p2 = new JPanel();
178
+
179
+ p2.setBackground(Color.gray);
180
+
181
+
182
+
183
+ JButton btn = new JButton("ゲーム開始");
184
+
185
+ btn.addActionListener(this);
186
+
187
+ p2.add(btn);
188
+
189
+ // カードレイアウトに登録
190
+
191
+ gamePanel.add(p2, GameMode.MENU.name());
192
+
193
+ gamePanel.add(new MyPanel3(this), GameMode.PLAY.name());
194
+
195
+ gamePanel.add(new GameOverPanel(), GameMode.OVER.name());
196
+
197
+
198
+
199
+ add(p1, BorderLayout.NORTH);
200
+
201
+ add(gamePanel, BorderLayout.CENTER);
202
+
203
+ // Subthreadクラスを削除して、javax.swing.Timerを使用
204
+
205
+ // 10000ms=10秒後
206
+
207
+ timer = new Timer(10000, (e) -> {
208
+
209
+ showPanel(GameMode.OVER);
210
+
211
+ // ゲームオーバー後になにかしたい時はtimerも意識する必要があります。
212
+
213
+ });
214
+
215
+ // ここではtimer#startをしない。
216
+
217
+ // 最初はメニューを表示
218
+
219
+ showPanel(GameMode.MENU);
220
+
221
+ }
222
+
223
+
224
+
225
+ public void setCount(int count) {
226
+
227
+ p1.setCount(count);
228
+
229
+ }
230
+
231
+
232
+
233
+ public void showPanel(GameMode m) {
234
+
235
+ final CardLayout card = (CardLayout) gamePanel.getLayout();
236
+
237
+ card.show(gamePanel, m.name());
238
+
239
+ this.mode = m; // パネル表示時にゲームモードを変更
240
+
241
+ }
242
+
243
+
244
+
245
+ @Override
246
+
247
+ public void actionPerformed(ActionEvent arg0) {
248
+
249
+ switch (mode) {
250
+
251
+ case MENU:
252
+
253
+ showPanel(GameMode.PLAY);
254
+
255
+ timer.start();
256
+
257
+ break;
258
+
259
+ default:
260
+
261
+ break;
262
+
263
+ }
264
+
265
+ }
266
+
267
+ }
268
+
269
+
270
+
271
+ // MyPanel1 → ScorePanelにクラス名を変更
272
+
273
+ class ScorePanel extends JPanel {
274
+
275
+
276
+
277
+ int i;
278
+
279
+
280
+
281
+ private JLabel l1;
282
+
283
+
284
+
285
+ ScorePanel() {
286
+
287
+ JPanel p1 = new JPanel();
288
+
289
+
290
+
291
+ l1 = new JLabel();
292
+
293
+ p1.setBackground(Color.green);
294
+
295
+ Integer j = new Integer(i);
296
+
297
+ String text = j.toString();
298
+
299
+ l1.setText(text);
300
+
301
+ p1.add(l1);
302
+
303
+
304
+
305
+ add(p1);
306
+
307
+ }
308
+
309
+
310
+
311
+ public void setCount(int count) {
312
+
313
+ l1.setText(Integer.toString(count));
314
+
315
+ }
316
+
317
+
318
+
319
+ }
320
+
321
+
322
+
323
+ // クラス名はクリッカーの方が良いかもです。
324
+
325
+ class MyPanel3 extends JPanel {
326
+
327
+
328
+
329
+ static int width = 500;
330
+
331
+ static int height = 500;
332
+
333
+ static int i = 0;
334
+
335
+ static int r = 60;
336
+
337
+ static int x;
338
+
339
+ static int y;
340
+
341
+
342
+
343
+ final static Color bc = Color.black;
344
+
345
+ final static Color dc = Color.green;
346
+
347
+
348
+
349
+ private ScreenToucher toucher2;
350
+
351
+
352
+
353
+ public MyPanel3(ScreenToucher toucher2) {
354
+
355
+ setBackground(Color.black);
356
+
357
+ this.toucher2 = toucher2;
358
+
359
+ MouseListener();
360
+
361
+ repaint();
362
+
363
+ }
364
+
365
+
366
+
367
+ void MouseListener() {
368
+
369
+ addMouseListener(new MouseAdapter() {
370
+
371
+ public void mousePressed(MouseEvent e) {
372
+
373
+ double mouseX = e.getX();
374
+
375
+ double mouseY = e.getY();
376
+
377
+ if (mouseX > x && mouseX < x + 2 * r) {
378
+
379
+ if (mouseY > y && mouseY < y + 2 * r) {
380
+
381
+ repaint();
382
+
383
+ toucher2.setCount(Count());
384
+
385
+ }
386
+
387
+ }
388
+
389
+ }
390
+
391
+ });
392
+
393
+ }
394
+
395
+
396
+
397
+ protected void paintComponent(Graphics g) {
398
+
399
+ super.paintComponent(g);
400
+
401
+ x = (int) (Math.random() * width);
402
+
403
+ y = (int) (Math.random() * height) + 30;
404
+
405
+ if ((x < width - 2 * r) && (y < height - 2 * r)) {
406
+
407
+ g.setColor(dc);
408
+
409
+ g.fillOval(x, y, r, r);
410
+
411
+ } else {
412
+
413
+ repaint();
414
+
415
+ }
416
+
417
+ }
418
+
419
+
420
+
421
+ int Count() {
422
+
423
+ i += 100;
424
+
425
+ return i;
426
+
427
+ }
428
+
429
+
430
+
431
+ }
432
+
433
+
434
+
435
+ // MyPanel4 →GameOverPanelに名前を変更
436
+
437
+ class GameOverPanel extends JPanel {
438
+
439
+
440
+
441
+ public GameOverPanel() {
442
+
443
+ setBackground(new Color(0, 0, 0, 100));
444
+
445
+ }
446
+
447
+
448
+
449
+ public void paintComponent(Graphics g) {
450
+
451
+ super.paintComponent(g);
452
+
453
+ g.setColor(Color.white);
454
+
455
+ g.drawString("GAME OVER", 100, 200);
456
+
457
+ }
458
+
459
+ }
460
+
461
+
462
+
463
+
464
+
465
+ ```
466
+
467
+
468
+
469
+ あとクラス間でイベントの通知をしたい時は[PropertyChangeListener](https://docs.oracle.com/javase/jp/10/docs/api/java/beans/PropertyChangeListener.html)が使えます。
470
+
471
+ 参考:[他のクラスからJFrameのラベルのテキストを変更](https://teratail.com/questions/107501)

1

追記

2018/08/20 09:40

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -30,7 +30,7 @@
30
30
 
31
31
  }
32
32
 
33
- // 内部クラスに変更して
33
+ // ClickActionを内部クラスに変更して
34
34
 
35
35
  class ClickAction implements ActionListener {
36
36
 
@@ -64,7 +64,7 @@
64
64
 
65
65
  1,画面の遷移
66
66
 
67
- 1-a,`JPanel`または`JLayeredPane`を継承したクラスを画面毎に作成する
67
+ 1-a,表示したい画面毎に`JPanel`または`JLayeredPane`を継承したクラスを作成する
68
68
 
69
69
   この時`paintComponent`はできるだけオーバーライドしない。
70
70
 
@@ -80,7 +80,7 @@
80
80
 
81
81
  2,定期的に処理を行いたい時
82
82
 
83
- `EDT(Event Dispatch Thread)`の関係上、`Thread`クラスを使わず、`javax.swing.Timer`を使う。
83
+ `EDT(Event Dispatch Thread)`の関係上、[Thread](https://docs.oracle.com/javase/jp/10/docs/api/java/lang/Thread.html)クラスを使わず、[javax.swing.Timer](https://docs.oracle.com/javase/jp/10/docs/api/javax/swing/Timer.html)と[SwingWorker](https://docs.oracle.com/javase/jp/10/docs/api/javax/swing/SwingWorker.html)を使う。
84
84
 
85
85
  3,画面作成は`WindowBuilder`を作る
86
86