質問編集履歴
4
ソースコードを消してしまった。
test
CHANGED
File without changes
|
test
CHANGED
@@ -26,6 +26,314 @@
|
|
26
26
|
|
27
27
|
###該当のソースコード
|
28
28
|
|
29
|
+
```import java.awt.*;
|
30
|
+
|
31
|
+
import java.awt.event.*;
|
32
|
+
|
33
|
+
import javax.swing.*;
|
34
|
+
|
35
|
+
import java.awt.image.*;
|
36
|
+
|
37
|
+
import java.io.*;
|
38
|
+
|
39
|
+
import javax.imageio.*;
|
40
|
+
|
41
|
+
import java.awt.Graphics;
|
42
|
+
|
43
|
+
import java.awt.Graphics2D;
|
44
|
+
|
45
|
+
import java.awt.geom.AffineTransform;
|
46
|
+
|
47
|
+
import java.awt.image.BufferedImage;
|
48
|
+
|
49
|
+
import java.io.IOException;
|
50
|
+
|
51
|
+
import javax.imageio.ImageIO;
|
52
|
+
|
53
|
+
import javax.swing.JFrame;
|
54
|
+
|
55
|
+
import javax.swing.JPanel;
|
56
|
+
|
57
|
+
import java.awt.Color;
|
58
|
+
|
59
|
+
import java.awt.event.WindowAdapter;
|
60
|
+
|
61
|
+
import java.awt.event.WindowEvent;
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
public class Wanipani extends JPanel {
|
66
|
+
|
67
|
+
// モグラたたきのターゲット
|
68
|
+
|
69
|
+
public class Mogura extends Canvas implements Runnable {
|
70
|
+
|
71
|
+
// 表示非表示を行うスレッド
|
72
|
+
|
73
|
+
// 新しいスレッドに変えると古いスレッドが停止する
|
74
|
+
|
75
|
+
// run() の if 文参照
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
Thread thread1;
|
80
|
+
|
81
|
+
int count;
|
82
|
+
|
83
|
+
Button button;
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
public void init1() {
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
setBackground(Color.yellow);
|
92
|
+
|
93
|
+
button = new Button("スタート");
|
94
|
+
|
95
|
+
setLayout(new BorderLayout());
|
96
|
+
|
97
|
+
Panel pnl = new Panel();
|
98
|
+
|
99
|
+
pnl.add(button);
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
private Image image;
|
108
|
+
|
109
|
+
private int point;
|
110
|
+
|
111
|
+
protected Thread thread;
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
public Mogura(int p,String imagefile) {
|
116
|
+
|
117
|
+
point = p;
|
118
|
+
|
119
|
+
try{
|
120
|
+
|
121
|
+
FileInputStream in = new FileInputStream(imagefile);
|
122
|
+
|
123
|
+
image = ImageIO.read(in);
|
124
|
+
|
125
|
+
in.close();
|
126
|
+
|
127
|
+
}catch (IOException e)
|
128
|
+
|
129
|
+
{
|
130
|
+
|
131
|
+
e.printStackTrace();
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
enableEvents(MouseEvent.MOUSE_PRESSED);
|
136
|
+
|
137
|
+
init();
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
// いったん非表示にして状態を初期化
|
148
|
+
|
149
|
+
// オブジェクト作成時とマウスでたたかれた時に使われる
|
150
|
+
|
151
|
+
protected void init() {
|
152
|
+
|
153
|
+
// 非表示
|
154
|
+
|
155
|
+
setVisible(false);
|
156
|
+
|
157
|
+
// 表示非表示スレッドを実行 &thread に設定
|
158
|
+
|
159
|
+
// thread が上書きされこれまでのスレッドは停止する
|
160
|
+
|
161
|
+
thread = new Thread(this);
|
162
|
+
|
163
|
+
thread.start();
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
// Canvas クラスからのオーバーライド
|
170
|
+
|
171
|
+
// MouseEvent を扱う MouseAdapter 以外のもう一つの方法
|
172
|
+
|
173
|
+
// 使うイベントを enableEvents で登録しておく
|
174
|
+
|
175
|
+
// (コンストラクタ参照)
|
176
|
+
|
177
|
+
protected void processMouseEvent(MouseEvent e) {
|
178
|
+
|
179
|
+
// MousePressed の際に、
|
180
|
+
|
181
|
+
if (e.getID() == MouseEvent.MOUSE_PRESSED) {
|
182
|
+
|
183
|
+
// 得点を追加して (MoguraPanel のメソッド)
|
184
|
+
|
185
|
+
incScore(point);
|
186
|
+
|
187
|
+
// 非表示にして状態を初期化
|
188
|
+
|
189
|
+
init();
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
// Runnable インターフェイスからのオーバーライド
|
198
|
+
|
199
|
+
// 表示非表示を繰り返す
|
200
|
+
|
201
|
+
public void run() {
|
202
|
+
|
203
|
+
// 無限ループ、thread が書き換わったら終了
|
204
|
+
|
205
|
+
for (;;) {
|
206
|
+
|
207
|
+
// 1秒間 sleep
|
208
|
+
|
209
|
+
try {
|
210
|
+
|
211
|
+
int t =(int)(Math.random()*500+1000);
|
212
|
+
|
213
|
+
Thread.sleep(t);
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
} catch (InterruptedException e) {
|
220
|
+
|
221
|
+
e.printStackTrace();
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
// thread 変数をチェック
|
226
|
+
|
227
|
+
if (Thread.currentThread() != thread) {
|
228
|
+
|
229
|
+
break;
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
int ka =(int)(Math.random()*450);
|
234
|
+
|
235
|
+
int ki =(int)(Math.random()*450);
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
// 見た目を設定して表示 or 非表示
|
242
|
+
|
243
|
+
setBounds(ka,ki,100,100);
|
244
|
+
|
245
|
+
setBackground(Color.RED);
|
246
|
+
|
247
|
+
setVisible(!isVisible());
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
public void paint(Graphics g)
|
254
|
+
|
255
|
+
{
|
256
|
+
|
257
|
+
int w = getWidth();
|
258
|
+
|
259
|
+
int h = getHeight();
|
260
|
+
|
261
|
+
g.drawImage(image,0,0,w,h,null);
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
// 得点
|
270
|
+
|
271
|
+
protected int score = 0;
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
public Wanipani() {
|
276
|
+
|
277
|
+
// モグラたたきエリアの設定表示エリアの範囲
|
278
|
+
|
279
|
+
setPreferredSize(new Dimension(500,500));
|
280
|
+
|
281
|
+
// モグラを1体配置
|
282
|
+
|
283
|
+
add(new Mogura(10,"mogura.png"));
|
284
|
+
|
285
|
+
add(new Mogura(-50,"bakudan.png"));
|
286
|
+
|
287
|
+
}
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
// 得点を得たときの挙動
|
292
|
+
|
293
|
+
protected void incScore(int p)
|
294
|
+
|
295
|
+
{
|
296
|
+
|
297
|
+
score += p;
|
298
|
+
|
299
|
+
repaint();
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
// JPanel からのオーバーライド
|
304
|
+
|
305
|
+
// 中身の描画
|
306
|
+
|
307
|
+
public void paint(Graphics g) {
|
308
|
+
|
309
|
+
// 得点を表示
|
310
|
+
|
311
|
+
g.drawString("得点: " + score, 10, 10);
|
312
|
+
|
313
|
+
// 背景を描画
|
314
|
+
|
315
|
+
}
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
public static void main(String[] args) {
|
320
|
+
|
321
|
+
JFrame frame = new JFrame("ワニたたき");
|
322
|
+
|
323
|
+
frame.setContentPane(new Wanipani());
|
324
|
+
|
325
|
+
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
|
326
|
+
|
327
|
+
frame.pack();
|
328
|
+
|
329
|
+
frame.setBackground(Color.blue);
|
330
|
+
|
331
|
+
frame.setVisible(true);
|
332
|
+
|
333
|
+
}
|
334
|
+
|
335
|
+
}
|
336
|
+
|
29
337
|
###補足情報(言語/FW/ツール等のバージョンなど)
|
30
338
|
|
31
339
|
より詳細な情報
|
3
タイトル変更してしまったため
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
ワニワニパニックをjavaで作りたいのですがうまくできません。お助けください。
|
test
CHANGED
File without changes
|
2
お礼
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
楽しく遊べました(#^.^#)
|
test
CHANGED
@@ -26,666 +26,6 @@
|
|
26
26
|
|
27
27
|
###該当のソースコード
|
28
28
|
|
29
|
-
```import java.awt.*;
|
30
|
-
|
31
|
-
import java.awt.event.*;
|
32
|
-
|
33
|
-
import javax.swing.*;
|
34
|
-
|
35
|
-
import java.awt.image.*;
|
36
|
-
|
37
|
-
import java.io.*;
|
38
|
-
|
39
|
-
import javax.imageio.*;
|
40
|
-
|
41
|
-
import java.awt.Graphics;
|
42
|
-
|
43
|
-
import java.awt.Graphics2D;
|
44
|
-
|
45
|
-
import java.awt.geom.AffineTransform;
|
46
|
-
|
47
|
-
import java.awt.image.BufferedImage;
|
48
|
-
|
49
|
-
import java.io.IOException;
|
50
|
-
|
51
|
-
import javax.imageio.ImageIO;
|
52
|
-
|
53
|
-
import javax.swing.JFrame;
|
54
|
-
|
55
|
-
import javax.swing.JPanel;
|
56
|
-
|
57
|
-
import java.awt.Color;
|
58
|
-
|
59
|
-
import java.awt.event.WindowAdapter;
|
60
|
-
|
61
|
-
import java.awt.event.WindowEvent;
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
public class Wanipani extends JPanel {
|
66
|
-
|
67
|
-
// モグラたたきのターゲット
|
68
|
-
|
69
|
-
public class Mogura extends Canvas implements Runnable {
|
70
|
-
|
71
|
-
// 表示非表示を行うスレッド
|
72
|
-
|
73
|
-
// 新しいスレッドに変えると古いスレッドが停止する
|
74
|
-
|
75
|
-
// run() の if 文参照
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
Thread thread1;
|
80
|
-
|
81
|
-
int count;
|
82
|
-
|
83
|
-
Button button;
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
public void init1() {
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
setBackground(Color.yellow);
|
92
|
-
|
93
|
-
button = new Button("スタート");
|
94
|
-
|
95
|
-
setLayout(new BorderLayout());
|
96
|
-
|
97
|
-
Panel pnl = new Panel();
|
98
|
-
|
99
|
-
pnl.add(button);
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
}
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
private Image image;
|
108
|
-
|
109
|
-
private int point;
|
110
|
-
|
111
|
-
protected Thread thread;
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
public Mogura(int p,String imagefile) {
|
116
|
-
|
117
|
-
point = p;
|
118
|
-
|
119
|
-
try{
|
120
|
-
|
121
|
-
FileInputStream in = new FileInputStream(imagefile);
|
122
|
-
|
123
|
-
image = ImageIO.read(in);
|
124
|
-
|
125
|
-
in.close();
|
126
|
-
|
127
|
-
}catch (IOException e)
|
128
|
-
|
129
|
-
{
|
130
|
-
|
131
|
-
e.printStackTrace();
|
132
|
-
|
133
|
-
}
|
134
|
-
|
135
|
-
enableEvents(MouseEvent.MOUSE_PRESSED);
|
136
|
-
|
137
|
-
init();
|
138
|
-
|
139
|
-
}
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
// いったん非表示にして状態を初期化
|
148
|
-
|
149
|
-
// オブジェクト作成時とマウスでたたかれた時に使われる
|
150
|
-
|
151
|
-
protected void init() {
|
152
|
-
|
153
|
-
// 非表示
|
154
|
-
|
155
|
-
setVisible(false);
|
156
|
-
|
157
|
-
// 表示非表示スレッドを実行 &thread に設定
|
158
|
-
|
159
|
-
// thread が上書きされこれまでのスレッドは停止する
|
160
|
-
|
161
|
-
thread = new Thread(this);
|
162
|
-
|
163
|
-
thread.start();
|
164
|
-
|
165
|
-
}
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
// Canvas クラスからのオーバーライド
|
170
|
-
|
171
|
-
// MouseEvent を扱う MouseAdapter 以外のもう一つの方法
|
172
|
-
|
173
|
-
// 使うイベントを enableEvents で登録しておく
|
174
|
-
|
175
|
-
// (コンストラクタ参照)
|
176
|
-
|
177
|
-
protected void processMouseEvent(MouseEvent e) {
|
178
|
-
|
179
|
-
// MousePressed の際に、
|
180
|
-
|
181
|
-
if (e.getID() == MouseEvent.MOUSE_PRESSED) {
|
182
|
-
|
183
|
-
// 得点を追加して (MoguraPanel のメソッド)
|
184
|
-
|
185
|
-
incScore(point);
|
186
|
-
|
187
|
-
// 非表示にして状態を初期化
|
188
|
-
|
189
|
-
init();
|
190
|
-
|
191
|
-
}
|
192
|
-
|
193
|
-
}
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
// Runnable インターフェイスからのオーバーライド
|
198
|
-
|
199
|
-
// 表示非表示を繰り返す
|
200
|
-
|
201
|
-
public void run() {
|
202
|
-
|
203
|
-
// 無限ループ、thread が書き換わったら終了
|
204
|
-
|
205
|
-
for (;;) {
|
206
|
-
|
207
|
-
// 1秒間 sleep
|
208
|
-
|
209
|
-
try {
|
210
|
-
|
211
|
-
int t =(int)(Math.random()*500+1000);
|
212
|
-
|
213
|
-
Thread.sleep(t);
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
} catch (InterruptedException e) {
|
220
|
-
|
221
|
-
e.printStackTrace();
|
222
|
-
|
223
|
-
}
|
224
|
-
|
225
|
-
// thread 変数をチェック
|
226
|
-
|
227
|
-
if (Thread.currentThread() != thread) {
|
228
|
-
|
229
|
-
break;
|
230
|
-
|
231
|
-
}
|
232
|
-
|
233
|
-
int ka =(int)(Math.random()*450);
|
234
|
-
|
235
|
-
int ki =(int)(Math.random()*450);
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
// 見た目を設定して表示 or 非表示
|
242
|
-
|
243
|
-
setBounds(ka,ki,100,100);
|
244
|
-
|
245
|
-
setBackground(Color.RED);
|
246
|
-
|
247
|
-
setVisible(!isVisible());
|
248
|
-
|
249
|
-
}
|
250
|
-
|
251
|
-
}
|
252
|
-
|
253
|
-
public void paint(Graphics g)
|
254
|
-
|
255
|
-
{
|
256
|
-
|
257
|
-
int w = getWidth();
|
258
|
-
|
259
|
-
int h = getHeight();
|
260
|
-
|
261
|
-
g.drawImage(image,0,0,w,h,null);
|
262
|
-
|
263
|
-
}
|
264
|
-
|
265
|
-
}
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
// 得点
|
270
|
-
|
271
|
-
protected int score = 0;
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
public Wanipani() {
|
276
|
-
|
277
|
-
// モグラたたきエリアの設定表示エリアの範囲
|
278
|
-
|
279
|
-
setPreferredSize(new Dimension(500,500));
|
280
|
-
|
281
|
-
// モグラを1体配置
|
282
|
-
|
283
|
-
add(new Mogura(10,"mogura.png"));
|
284
|
-
|
285
|
-
add(new Mogura(-50,"bakudan.png"));
|
286
|
-
|
287
|
-
}
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
// 得点を得たときの挙動
|
292
|
-
|
293
|
-
protected void incScore(int p)
|
294
|
-
|
295
|
-
{
|
296
|
-
|
297
|
-
score += p;
|
298
|
-
|
299
|
-
repaint();
|
300
|
-
|
301
|
-
}
|
302
|
-
|
303
|
-
// JPanel からのオーバーライド
|
304
|
-
|
305
|
-
// 中身の描画
|
306
|
-
|
307
|
-
public void paint(Graphics g) {
|
308
|
-
|
309
|
-
// 得点を表示
|
310
|
-
|
311
|
-
g.drawString("得点: " + score, 10, 10);
|
312
|
-
|
313
|
-
// 背景を描画
|
314
|
-
|
315
|
-
}
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
public static void main(String[] args) {
|
320
|
-
|
321
|
-
JFrame frame = new JFrame("ワニたたき");
|
322
|
-
|
323
|
-
frame.setContentPane(new Wanipani());
|
324
|
-
|
325
|
-
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
|
326
|
-
|
327
|
-
frame.pack();
|
328
|
-
|
329
|
-
frame.setBackground(Color.blue);
|
330
|
-
|
331
|
-
frame.setVisible(true);
|
332
|
-
|
333
|
-
}
|
334
|
-
|
335
|
-
}
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
29
|
###補足情報(言語/FW/ツール等のバージョンなど)
|
344
30
|
|
345
31
|
より詳細な情報
|
346
|
-
|
347
|
-
###前提・実現したいこと
|
348
|
-
|
349
|
-
スタートボタンによりゲームを開始、ゲーム終了後にスタート画面に戻る。
|
350
|
-
|
351
|
-
ワニの出る位置を固定したい。
|
352
|
-
|
353
|
-
1分間でゲーム終了。
|
354
|
-
|
355
|
-
得点のランキング付け。
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
###発生している問題・エラーメッセージ
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
```
|
364
|
-
|
365
|
-
現在の進み具合は青い画面にランダムに画像が表示されクリックすると消え、得点が加算されます。
|
366
|
-
|
367
|
-
何度試してもスタートボタンが表示されないのとワニの位置を固定できない。
|
368
|
-
|
369
|
-
```
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
###該当のソースコード
|
374
|
-
|
375
|
-
```import java.awt.*;
|
376
|
-
|
377
|
-
import java.awt.event.*;
|
378
|
-
|
379
|
-
import javax.swing.*;
|
380
|
-
|
381
|
-
import java.awt.image.*;
|
382
|
-
|
383
|
-
import java.io.*;
|
384
|
-
|
385
|
-
import javax.imageio.*;
|
386
|
-
|
387
|
-
import java.awt.Graphics;
|
388
|
-
|
389
|
-
import java.awt.Graphics2D;
|
390
|
-
|
391
|
-
import java.awt.geom.AffineTransform;
|
392
|
-
|
393
|
-
import java.awt.image.BufferedImage;
|
394
|
-
|
395
|
-
import java.io.IOException;
|
396
|
-
|
397
|
-
import javax.imageio.ImageIO;
|
398
|
-
|
399
|
-
import javax.swing.JFrame;
|
400
|
-
|
401
|
-
import javax.swing.JPanel;
|
402
|
-
|
403
|
-
import java.awt.Color;
|
404
|
-
|
405
|
-
import java.awt.event.WindowAdapter;
|
406
|
-
|
407
|
-
import java.awt.event.WindowEvent;
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
public class Wanipani extends JPanel {
|
412
|
-
|
413
|
-
// モグラたたきのターゲット
|
414
|
-
|
415
|
-
public class Mogura extends Canvas implements Runnable {
|
416
|
-
|
417
|
-
// 表示非表示を行うスレッド
|
418
|
-
|
419
|
-
// 新しいスレッドに変えると古いスレッドが停止する
|
420
|
-
|
421
|
-
// run() の if 文参照
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
Thread thread1;
|
426
|
-
|
427
|
-
int count;
|
428
|
-
|
429
|
-
Button button;
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
public void init1() {
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
setBackground(Color.yellow);
|
438
|
-
|
439
|
-
button = new Button("スタート");
|
440
|
-
|
441
|
-
setLayout(new BorderLayout());
|
442
|
-
|
443
|
-
Panel pnl = new Panel();
|
444
|
-
|
445
|
-
pnl.add(button);
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
}
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
private Image image;
|
454
|
-
|
455
|
-
private int point;
|
456
|
-
|
457
|
-
protected Thread thread;
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
public Mogura(int p,String imagefile) {
|
462
|
-
|
463
|
-
point = p;
|
464
|
-
|
465
|
-
try{
|
466
|
-
|
467
|
-
FileInputStream in = new FileInputStream(imagefile);
|
468
|
-
|
469
|
-
image = ImageIO.read(in);
|
470
|
-
|
471
|
-
in.close();
|
472
|
-
|
473
|
-
}catch (IOException e)
|
474
|
-
|
475
|
-
{
|
476
|
-
|
477
|
-
e.printStackTrace();
|
478
|
-
|
479
|
-
}
|
480
|
-
|
481
|
-
enableEvents(MouseEvent.MOUSE_PRESSED);
|
482
|
-
|
483
|
-
init();
|
484
|
-
|
485
|
-
}
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
// いったん非表示にして状態を初期化
|
494
|
-
|
495
|
-
// オブジェクト作成時とマウスでたたかれた時に使われる
|
496
|
-
|
497
|
-
protected void init() {
|
498
|
-
|
499
|
-
// 非表示
|
500
|
-
|
501
|
-
setVisible(false);
|
502
|
-
|
503
|
-
// 表示非表示スレッドを実行 &thread に設定
|
504
|
-
|
505
|
-
// thread が上書きされこれまでのスレッドは停止する
|
506
|
-
|
507
|
-
thread = new Thread(this);
|
508
|
-
|
509
|
-
thread.start();
|
510
|
-
|
511
|
-
}
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
// Canvas クラスからのオーバーライド
|
516
|
-
|
517
|
-
// MouseEvent を扱う MouseAdapter 以外のもう一つの方法
|
518
|
-
|
519
|
-
// 使うイベントを enableEvents で登録しておく
|
520
|
-
|
521
|
-
// (コンストラクタ参照)
|
522
|
-
|
523
|
-
protected void processMouseEvent(MouseEvent e) {
|
524
|
-
|
525
|
-
// MousePressed の際に、
|
526
|
-
|
527
|
-
if (e.getID() == MouseEvent.MOUSE_PRESSED) {
|
528
|
-
|
529
|
-
// 得点を追加して (MoguraPanel のメソッド)
|
530
|
-
|
531
|
-
incScore(point);
|
532
|
-
|
533
|
-
// 非表示にして状態を初期化
|
534
|
-
|
535
|
-
init();
|
536
|
-
|
537
|
-
}
|
538
|
-
|
539
|
-
}
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
// Runnable インターフェイスからのオーバーライド
|
544
|
-
|
545
|
-
// 表示非表示を繰り返す
|
546
|
-
|
547
|
-
public void run() {
|
548
|
-
|
549
|
-
// 無限ループ、thread が書き換わったら終了
|
550
|
-
|
551
|
-
for (;;) {
|
552
|
-
|
553
|
-
// 1秒間 sleep
|
554
|
-
|
555
|
-
try {
|
556
|
-
|
557
|
-
int t =(int)(Math.random()*500+1000);
|
558
|
-
|
559
|
-
Thread.sleep(t);
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
} catch (InterruptedException e) {
|
566
|
-
|
567
|
-
e.printStackTrace();
|
568
|
-
|
569
|
-
}
|
570
|
-
|
571
|
-
// thread 変数をチェック
|
572
|
-
|
573
|
-
if (Thread.currentThread() != thread) {
|
574
|
-
|
575
|
-
break;
|
576
|
-
|
577
|
-
}
|
578
|
-
|
579
|
-
int ka =(int)(Math.random()*450);
|
580
|
-
|
581
|
-
int ki =(int)(Math.random()*450);
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
// 見た目を設定して表示 or 非表示
|
588
|
-
|
589
|
-
setBounds(ka,ki,100,100);
|
590
|
-
|
591
|
-
setBackground(Color.RED);
|
592
|
-
|
593
|
-
setVisible(!isVisible());
|
594
|
-
|
595
|
-
}
|
596
|
-
|
597
|
-
}
|
598
|
-
|
599
|
-
public void paint(Graphics g)
|
600
|
-
|
601
|
-
{
|
602
|
-
|
603
|
-
int w = getWidth();
|
604
|
-
|
605
|
-
int h = getHeight();
|
606
|
-
|
607
|
-
g.drawImage(image,0,0,w,h,null);
|
608
|
-
|
609
|
-
}
|
610
|
-
|
611
|
-
}
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
// 得点
|
616
|
-
|
617
|
-
protected int score = 0;
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
public Wanipani() {
|
622
|
-
|
623
|
-
// モグラたたきエリアの設定表示エリアの範囲
|
624
|
-
|
625
|
-
setPreferredSize(new Dimension(500,500));
|
626
|
-
|
627
|
-
// モグラを1体配置
|
628
|
-
|
629
|
-
add(new Mogura(10,"mogura.png"));
|
630
|
-
|
631
|
-
add(new Mogura(-50,"bakudan.png"));
|
632
|
-
|
633
|
-
}
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
// 得点を得たときの挙動
|
638
|
-
|
639
|
-
protected void incScore(int p)
|
640
|
-
|
641
|
-
{
|
642
|
-
|
643
|
-
score += p;
|
644
|
-
|
645
|
-
repaint();
|
646
|
-
|
647
|
-
}
|
648
|
-
|
649
|
-
// JPanel からのオーバーライド
|
650
|
-
|
651
|
-
// 中身の描画
|
652
|
-
|
653
|
-
public void paint(Graphics g) {
|
654
|
-
|
655
|
-
// 得点を表示
|
656
|
-
|
657
|
-
g.drawString("得点: " + score, 10, 10);
|
658
|
-
|
659
|
-
// 背景を描画
|
660
|
-
|
661
|
-
}
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
public static void main(String[] args) {
|
666
|
-
|
667
|
-
JFrame frame = new JFrame("ワニたたき");
|
668
|
-
|
669
|
-
frame.setContentPane(new Wanipani());
|
670
|
-
|
671
|
-
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
|
672
|
-
|
673
|
-
frame.pack();
|
674
|
-
|
675
|
-
frame.setBackground(Color.blue);
|
676
|
-
|
677
|
-
frame.setVisible(true);
|
678
|
-
|
679
|
-
}
|
680
|
-
|
681
|
-
}
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
###補足情報(言語/FW/ツール等のバージョンなど)
|
690
|
-
|
691
|
-
より詳細な情報
|
1
お礼
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
助かりました(#^.^#)
|
test
CHANGED
File without changes
|