回答編集履歴
1
コード追加
test
CHANGED
@@ -5,3 +5,641 @@
|
|
5
5
|
|
6
6
|
|
7
7
|
Fishing を new するのはメインメソッドでの1回のみとなるように, スコープを理解して修正してみてください.
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
---
|
12
|
+
|
13
|
+
```java
|
14
|
+
|
15
|
+
import java.awt.CardLayout;
|
16
|
+
|
17
|
+
import java.awt.event.*;
|
18
|
+
|
19
|
+
import java.util.Random;
|
20
|
+
|
21
|
+
import javax.swing.*;
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
public class JFishing extends JFrame {
|
26
|
+
|
27
|
+
/** 魚 */
|
28
|
+
|
29
|
+
private static class Fish {
|
30
|
+
|
31
|
+
private ImageIcon icon;
|
32
|
+
|
33
|
+
private int score;
|
34
|
+
|
35
|
+
Fish(String iconName, int score) {
|
36
|
+
|
37
|
+
icon = new ImageIcon(iconName);
|
38
|
+
|
39
|
+
this.score = score;
|
40
|
+
|
41
|
+
}
|
42
|
+
|
43
|
+
Icon getIcon() { return icon; }
|
44
|
+
|
45
|
+
int getScore() { return score; }
|
46
|
+
|
47
|
+
}
|
48
|
+
|
49
|
+
/** 釣り上げチャンス */
|
50
|
+
|
51
|
+
private static class CatchChance {
|
52
|
+
|
53
|
+
private static Random random = new Random(System.currentTimeMillis());
|
54
|
+
|
55
|
+
private int ratio;
|
56
|
+
|
57
|
+
private Icon icon;
|
58
|
+
|
59
|
+
private int time;
|
60
|
+
|
61
|
+
private Fish[] fishes;
|
62
|
+
|
63
|
+
/**
|
64
|
+
|
65
|
+
* コンストラクタ
|
66
|
+
|
67
|
+
* @param ratio 確率(全体に占める割合)
|
68
|
+
|
69
|
+
* @param iconName イメージファイル名
|
70
|
+
|
71
|
+
* @param time 継続時間
|
72
|
+
|
73
|
+
* @param fishes 釣れる可能性のある魚の配列
|
74
|
+
|
75
|
+
*/
|
76
|
+
|
77
|
+
CatchChance(int ratio, String iconName, int time, Fish ... fishes) {
|
78
|
+
|
79
|
+
this.ratio = ratio;
|
80
|
+
|
81
|
+
this.icon = new ImageIcon(iconName);
|
82
|
+
|
83
|
+
this.time = time;
|
84
|
+
|
85
|
+
this.fishes = fishes;
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
int getRatio() { return ratio; }
|
90
|
+
|
91
|
+
Icon getIcon() { return icon; }
|
92
|
+
|
93
|
+
int getTime() { return time; }
|
94
|
+
|
95
|
+
Fish getFish() {
|
96
|
+
|
97
|
+
if(fishes.length == 0) return null;
|
98
|
+
|
99
|
+
int j = random.nextInt(fishes.length);
|
100
|
+
|
101
|
+
return fishes[j];
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
};
|
106
|
+
|
107
|
+
/** 魚が掛かっていない状態 */
|
108
|
+
|
109
|
+
private static class NormalState extends CatchChance {
|
110
|
+
|
111
|
+
private Random random = new Random(System.currentTimeMillis());
|
112
|
+
|
113
|
+
NormalState(String iconName) {
|
114
|
+
|
115
|
+
super(0, iconName, 0);
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
/** 魚が掛かっていない時間 */
|
120
|
+
|
121
|
+
int getTime() {
|
122
|
+
|
123
|
+
return 100 * (random.nextInt(50) + 1); // 100 - 5000
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
private static final CatchChance CATCH_CHANCES[] = {
|
130
|
+
|
131
|
+
new NormalState("しーん.jpg"),
|
132
|
+
|
133
|
+
new CatchChance(11,"!.jpg", 600, new Fish("N0.jpg" , 0), new Fish("N1.jpg" , 50), new Fish("N2.jpg" , 50), new Fish("N3.jpg" ,100), new Fish("N4.jpg" , 100)),
|
134
|
+
|
135
|
+
new CatchChance( 7,"!!.jpg", 400, new Fish("R0.jpg" ,150), new Fish("R1.jpg" ,150), new Fish("R2.jpg" , 200), new Fish("R3.jpg" ,200), new Fish("R4.jpg" , 250)),
|
136
|
+
|
137
|
+
new CatchChance( 2,"!!!.jpg",300, new Fish("SR0.jpg",300), new Fish("SR1.jpg",300), new Fish("SR2.jpg", 400), new Fish("SR3.jpg",400), new Fish("SR4.jpg", 500)),
|
138
|
+
|
139
|
+
new CatchChance( 0,"?.jpg", 0, new Fish("SC0.jpg",-50), new Fish("SC1.jpg",-50), new Fish("SC2.jpg",-100), new Fish("SC3.jpg", 0), new Fish("SC4.jpg",1000))
|
140
|
+
|
141
|
+
};
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
public static void main(String[] args) {
|
146
|
+
|
147
|
+
new JFishing().setVisible(true);
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
//private int flag = 0; //使い道不定
|
154
|
+
|
155
|
+
private int gamescore = 0;
|
156
|
+
|
157
|
+
private int alltime = 0; //60sカウントに使う。
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
private CardLayout mgr;
|
162
|
+
|
163
|
+
private CatchPanel catchPanel;
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
JFishing() {
|
168
|
+
|
169
|
+
super();
|
170
|
+
|
171
|
+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
172
|
+
|
173
|
+
setBounds(550, 100, 770, 800);
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
mgr = new CardLayout();
|
178
|
+
|
179
|
+
getContentPane().setLayout(mgr);
|
180
|
+
|
181
|
+
add(new TitlePanel(), "Title");
|
182
|
+
|
183
|
+
add(new ExplainPanel(), "Explain");
|
184
|
+
|
185
|
+
add(new GamePanel(), "Game");
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
catchPanel = new CatchPanel();
|
190
|
+
|
191
|
+
add(catchPanel, "Catch");
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
showTitlePanel();
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
private void showTitlePanel() {
|
200
|
+
|
201
|
+
gamescore = 0;
|
202
|
+
|
203
|
+
alltime = 0;
|
204
|
+
|
205
|
+
mgr.show(getContentPane(), "Title");
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
private void showExplainPanel() {
|
210
|
+
|
211
|
+
mgr.show(getContentPane(), "Explain");
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
private void showGamePanel() {
|
216
|
+
|
217
|
+
mgr.show(getContentPane(), "Game");
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
private void showCatchPanel(Fish fish) {
|
222
|
+
|
223
|
+
catchPanel.setFish(fish);
|
224
|
+
|
225
|
+
mgr.show(getContentPane(), "Catch");
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
/** タイトルパネル */
|
232
|
+
|
233
|
+
private class TitlePanel extends JPanel {
|
234
|
+
|
235
|
+
TitlePanel() {
|
236
|
+
|
237
|
+
super(null);
|
238
|
+
|
239
|
+
setTitle("魚釣りゲーム~タイトル~");
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
JLabel titleLabel = new JLabel(new ImageIcon("タイトル画像.jpg"));
|
244
|
+
|
245
|
+
titleLabel.setBounds(-10, 0, 770, 760);
|
246
|
+
|
247
|
+
add(titleLabel);
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
JButton startButton = new JButton("スタート");
|
252
|
+
|
253
|
+
startButton.setBounds(310, 580, 130, 30);
|
254
|
+
|
255
|
+
startButton.addActionListener(e -> showGamePanel()); // スタート→メイン画面へ遷移
|
256
|
+
|
257
|
+
add(startButton);
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
JButton explainButton = new JButton("あそびかた");
|
262
|
+
|
263
|
+
explainButton.setBounds(310, 650, 130, 30);
|
264
|
+
|
265
|
+
explainButton.addActionListener(e -> showExplainPanel());
|
266
|
+
|
267
|
+
add(explainButton);
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
}
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
/** ルール説明パネル */
|
276
|
+
|
277
|
+
private class ExplainPanel extends JPanel {
|
278
|
+
|
279
|
+
private Icon rule1Icon = new ImageIcon("ルール1.jpg");
|
280
|
+
|
281
|
+
private Icon rule2Icon = new ImageIcon("ルール2.jpg");
|
282
|
+
|
283
|
+
private JLabel ruleLabel;
|
284
|
+
|
285
|
+
private JButton titleButton;
|
286
|
+
|
287
|
+
private JButton nextButton;
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
ExplainPanel() {
|
292
|
+
|
293
|
+
super(null);
|
294
|
+
|
295
|
+
setTitle("魚釣りゲームのルール説明");
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
ruleLabel = new JLabel(rule1Icon);
|
300
|
+
|
301
|
+
ruleLabel.setBounds(0, 150, 760, 460);
|
302
|
+
|
303
|
+
add(ruleLabel);
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
titleButton = new JButton("タイトルへ");
|
308
|
+
|
309
|
+
titleButton.setBounds(50, 520, 180, 30);
|
310
|
+
|
311
|
+
titleButton.addActionListener(e -> showTitlePanel());
|
312
|
+
|
313
|
+
add(titleButton);
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
nextButton = new JButton("つぎへ");
|
318
|
+
|
319
|
+
nextButton.setBounds(100, 540, 130, 30);
|
320
|
+
|
321
|
+
nextButton.addActionListener(e -> showRule2());
|
322
|
+
|
323
|
+
add(nextButton);
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
addComponentListener(new ComponentAdapter() {
|
328
|
+
|
329
|
+
@Override
|
330
|
+
|
331
|
+
public void componentShown(ComponentEvent e) {
|
332
|
+
|
333
|
+
showRule1();
|
334
|
+
|
335
|
+
}
|
336
|
+
|
337
|
+
});
|
338
|
+
|
339
|
+
}
|
340
|
+
|
341
|
+
private void showRule1() {
|
342
|
+
|
343
|
+
ruleLabel.setIcon(rule1Icon);
|
344
|
+
|
345
|
+
nextButton.setVisible(true);
|
346
|
+
|
347
|
+
titleButton.setVisible(false);
|
348
|
+
|
349
|
+
}
|
350
|
+
|
351
|
+
private void showRule2() {
|
352
|
+
|
353
|
+
ruleLabel.setIcon(rule2Icon);
|
354
|
+
|
355
|
+
nextButton.setVisible(false);
|
356
|
+
|
357
|
+
titleButton.setVisible(true);
|
358
|
+
|
359
|
+
}
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
/** ゲームパネル */
|
366
|
+
|
367
|
+
private class GamePanel extends JPanel {
|
368
|
+
|
369
|
+
private long starttime;
|
370
|
+
|
371
|
+
private CatchAction catchAction;
|
372
|
+
|
373
|
+
private JLabel scoreLabel;
|
374
|
+
|
375
|
+
|
376
|
+
|
377
|
+
GamePanel() {
|
378
|
+
|
379
|
+
super(null);
|
380
|
+
|
381
|
+
setTitle("魚釣りゲーム");
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
scoreLabel = new JLabel();
|
386
|
+
|
387
|
+
scoreLabel.setBounds(100, 0, 400, 400);
|
388
|
+
|
389
|
+
add(scoreLabel);
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
JLabel seaLabel = new JLabel(new ImageIcon("海.jpg"));
|
394
|
+
|
395
|
+
seaLabel.setBounds(195, 400, 560, 350);
|
396
|
+
|
397
|
+
add(seaLabel);
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+
JLabel fisherLabel = new JLabel(new ImageIcon("釣りbasic2.jpg"));
|
402
|
+
|
403
|
+
fisherLabel.setBounds(0, 180, 380, 380);
|
404
|
+
|
405
|
+
add(fisherLabel);
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
catchAction = new CatchAction(CATCH_CHANCES);
|
410
|
+
|
411
|
+
catchAction.setOnCatchListener(fish -> {
|
412
|
+
|
413
|
+
gamescore += fish.getScore();
|
414
|
+
|
415
|
+
rewriteScoreLabel();
|
416
|
+
|
417
|
+
showCatchPanel(fish);
|
418
|
+
|
419
|
+
});
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
JButton catchButton = new JButton(catchAction);
|
424
|
+
|
425
|
+
catchButton.setBounds(378, 120, 375, 280);
|
426
|
+
|
427
|
+
add(catchButton);
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
addComponentListener(new ComponentAdapter() {
|
432
|
+
|
433
|
+
@Override
|
434
|
+
|
435
|
+
public void componentShown(ComponentEvent e) {
|
436
|
+
|
437
|
+
starttime = System.currentTimeMillis();
|
438
|
+
|
439
|
+
rewriteScoreLabel();
|
440
|
+
|
441
|
+
catchAction.start();
|
442
|
+
|
443
|
+
}
|
444
|
+
|
445
|
+
@Override
|
446
|
+
|
447
|
+
public void componentHidden(ComponentEvent e) {
|
448
|
+
|
449
|
+
alltime += (int)(System.currentTimeMillis() - starttime);
|
450
|
+
|
451
|
+
catchAction.stop();
|
452
|
+
|
453
|
+
}
|
454
|
+
|
455
|
+
});
|
456
|
+
|
457
|
+
rewriteScoreLabel();
|
458
|
+
|
459
|
+
}
|
460
|
+
|
461
|
+
private void rewriteScoreLabel() {
|
462
|
+
|
463
|
+
scoreLabel.setText("SCORE: " + gamescore + "点");
|
464
|
+
|
465
|
+
}
|
466
|
+
|
467
|
+
}
|
468
|
+
|
469
|
+
/** 釣り上げ */
|
470
|
+
|
471
|
+
private static class CatchAction extends AbstractAction {
|
472
|
+
|
473
|
+
interface OnCatchListener {
|
474
|
+
|
475
|
+
void onCatch(Fish fish);
|
476
|
+
|
477
|
+
}
|
478
|
+
|
479
|
+
private OnCatchListener listener;
|
480
|
+
|
481
|
+
private CatchChance[] catchChances;
|
482
|
+
|
483
|
+
private int totalRatio;
|
484
|
+
|
485
|
+
private Random random = new Random(System.currentTimeMillis());
|
486
|
+
|
487
|
+
private Timer timer;
|
488
|
+
|
489
|
+
private CatchChance chance;
|
490
|
+
|
491
|
+
|
492
|
+
|
493
|
+
CatchAction(CatchChance[] catchChances) {
|
494
|
+
|
495
|
+
this.catchChances = catchChances;
|
496
|
+
|
497
|
+
totalRatio = 0;
|
498
|
+
|
499
|
+
for(CatchChance cr : catchChances) totalRatio += cr.getRatio();
|
500
|
+
|
501
|
+
|
502
|
+
|
503
|
+
chance = catchChances[0];
|
504
|
+
|
505
|
+
putValue(LARGE_ICON_KEY, chance.getIcon());
|
506
|
+
|
507
|
+
}
|
508
|
+
|
509
|
+
void setOnCatchListener(OnCatchListener listener) {
|
510
|
+
|
511
|
+
this.listener = listener;
|
512
|
+
|
513
|
+
}
|
514
|
+
|
515
|
+
void start() {
|
516
|
+
|
517
|
+
chance = catchChances[0];
|
518
|
+
|
519
|
+
putValue(LARGE_ICON_KEY, chance.getIcon());
|
520
|
+
|
521
|
+
int delay = chance.getTime();
|
522
|
+
|
523
|
+
timer = new Timer(delay, new ActionListener() {
|
524
|
+
|
525
|
+
public void actionPerformed(ActionEvent e) {
|
526
|
+
|
527
|
+
timer.stop();
|
528
|
+
|
529
|
+
chance = (chance == catchChances[0] ? getChance() : catchChances[0]);
|
530
|
+
|
531
|
+
putValue(LARGE_ICON_KEY, chance.getIcon());
|
532
|
+
|
533
|
+
timer.setInitialDelay(chance.getTime());
|
534
|
+
|
535
|
+
timer.restart();
|
536
|
+
|
537
|
+
}
|
538
|
+
|
539
|
+
});
|
540
|
+
|
541
|
+
timer.setRepeats(false);
|
542
|
+
|
543
|
+
timer.start();
|
544
|
+
|
545
|
+
}
|
546
|
+
|
547
|
+
private CatchChance getChance() {
|
548
|
+
|
549
|
+
int v = random.nextInt(totalRatio);
|
550
|
+
|
551
|
+
int i = 1;
|
552
|
+
|
553
|
+
while(v>=catchChances[i].getRatio()) v-=catchChances[i++].getRatio();
|
554
|
+
|
555
|
+
return catchChances[i];
|
556
|
+
|
557
|
+
}
|
558
|
+
|
559
|
+
void stop() {
|
560
|
+
|
561
|
+
if(timer != null) {
|
562
|
+
|
563
|
+
timer.stop();
|
564
|
+
|
565
|
+
timer = null;
|
566
|
+
|
567
|
+
}
|
568
|
+
|
569
|
+
}
|
570
|
+
|
571
|
+
@Override
|
572
|
+
|
573
|
+
public void actionPerformed(ActionEvent e) {
|
574
|
+
|
575
|
+
Fish fish = chance.getFish();
|
576
|
+
|
577
|
+
if(fish != null && listener != null) {
|
578
|
+
|
579
|
+
stop();
|
580
|
+
|
581
|
+
listener.onCatch(fish);
|
582
|
+
|
583
|
+
}
|
584
|
+
|
585
|
+
}
|
586
|
+
|
587
|
+
}
|
588
|
+
|
589
|
+
/** 釣り上げパネル */
|
590
|
+
|
591
|
+
private class CatchPanel extends JPanel {
|
592
|
+
|
593
|
+
private JLabel imageLabel;
|
594
|
+
|
595
|
+
CatchPanel() {
|
596
|
+
|
597
|
+
JButton returnButton = new JButton("もどる");
|
598
|
+
|
599
|
+
returnButton.setBounds(280, 650, 130, 30);
|
600
|
+
|
601
|
+
returnButton.addActionListener(e -> {
|
602
|
+
|
603
|
+
if(alltime < 60000) {
|
604
|
+
|
605
|
+
showGamePanel();
|
606
|
+
|
607
|
+
} else {
|
608
|
+
|
609
|
+
//game end
|
610
|
+
|
611
|
+
}
|
612
|
+
|
613
|
+
});
|
614
|
+
|
615
|
+
add(returnButton);
|
616
|
+
|
617
|
+
|
618
|
+
|
619
|
+
imageLabel = new JLabel();
|
620
|
+
|
621
|
+
imageLabel.setBounds(150, 200, 400, 400);
|
622
|
+
|
623
|
+
add(imageLabel);
|
624
|
+
|
625
|
+
|
626
|
+
|
627
|
+
JLabel catchLabel = new JLabel("釣り上げた!");
|
628
|
+
|
629
|
+
catchLabel.setBounds(305, 120, 100, 100);
|
630
|
+
|
631
|
+
add(catchLabel);
|
632
|
+
|
633
|
+
}
|
634
|
+
|
635
|
+
void setFish(Fish fish) {
|
636
|
+
|
637
|
+
imageLabel.setIcon(fish.getIcon());
|
638
|
+
|
639
|
+
}
|
640
|
+
|
641
|
+
}
|
642
|
+
|
643
|
+
}
|
644
|
+
|
645
|
+
```
|