質問編集履歴
4
ソースコードを消してしまった。
title
CHANGED
File without changes
|
body
CHANGED
@@ -12,5 +12,159 @@
|
|
12
12
|
```
|
13
13
|
|
14
14
|
###該当のソースコード
|
15
|
+
```import java.awt.*;
|
16
|
+
import java.awt.event.*;
|
17
|
+
import javax.swing.*;
|
18
|
+
import java.awt.image.*;
|
19
|
+
import java.io.*;
|
20
|
+
import javax.imageio.*;
|
21
|
+
import java.awt.Graphics;
|
22
|
+
import java.awt.Graphics2D;
|
23
|
+
import java.awt.geom.AffineTransform;
|
24
|
+
import java.awt.image.BufferedImage;
|
25
|
+
import java.io.IOException;
|
26
|
+
import javax.imageio.ImageIO;
|
27
|
+
import javax.swing.JFrame;
|
28
|
+
import javax.swing.JPanel;
|
29
|
+
import java.awt.Color;
|
30
|
+
import java.awt.event.WindowAdapter;
|
31
|
+
import java.awt.event.WindowEvent;
|
32
|
+
|
33
|
+
public class Wanipani extends JPanel {
|
34
|
+
// モグラたたきのターゲット
|
35
|
+
public class Mogura extends Canvas implements Runnable {
|
36
|
+
// 表示非表示を行うスレッド
|
37
|
+
// 新しいスレッドに変えると古いスレッドが停止する
|
38
|
+
// run() の if 文参照
|
39
|
+
|
40
|
+
Thread thread1;
|
41
|
+
int count;
|
42
|
+
Button button;
|
43
|
+
|
44
|
+
public void init1() {
|
45
|
+
|
46
|
+
setBackground(Color.yellow);
|
47
|
+
button = new Button("スタート");
|
48
|
+
setLayout(new BorderLayout());
|
49
|
+
Panel pnl = new Panel();
|
50
|
+
pnl.add(button);
|
51
|
+
|
52
|
+
}
|
53
|
+
|
54
|
+
private Image image;
|
55
|
+
private int point;
|
56
|
+
protected Thread thread;
|
57
|
+
|
58
|
+
public Mogura(int p,String imagefile) {
|
59
|
+
point = p;
|
60
|
+
try{
|
61
|
+
FileInputStream in = new FileInputStream(imagefile);
|
62
|
+
image = ImageIO.read(in);
|
63
|
+
in.close();
|
64
|
+
}catch (IOException e)
|
65
|
+
{
|
66
|
+
e.printStackTrace();
|
67
|
+
}
|
68
|
+
enableEvents(MouseEvent.MOUSE_PRESSED);
|
69
|
+
init();
|
70
|
+
}
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
// いったん非表示にして状態を初期化
|
75
|
+
// オブジェクト作成時とマウスでたたかれた時に使われる
|
76
|
+
protected void init() {
|
77
|
+
// 非表示
|
78
|
+
setVisible(false);
|
79
|
+
// 表示非表示スレッドを実行 &thread に設定
|
80
|
+
// thread が上書きされこれまでのスレッドは停止する
|
81
|
+
thread = new Thread(this);
|
82
|
+
thread.start();
|
83
|
+
}
|
84
|
+
|
85
|
+
// Canvas クラスからのオーバーライド
|
86
|
+
// MouseEvent を扱う MouseAdapter 以外のもう一つの方法
|
87
|
+
// 使うイベントを enableEvents で登録しておく
|
88
|
+
// (コンストラクタ参照)
|
89
|
+
protected void processMouseEvent(MouseEvent e) {
|
90
|
+
// MousePressed の際に、
|
91
|
+
if (e.getID() == MouseEvent.MOUSE_PRESSED) {
|
92
|
+
// 得点を追加して (MoguraPanel のメソッド)
|
93
|
+
incScore(point);
|
94
|
+
// 非表示にして状態を初期化
|
95
|
+
init();
|
96
|
+
}
|
97
|
+
}
|
98
|
+
|
99
|
+
// Runnable インターフェイスからのオーバーライド
|
100
|
+
// 表示非表示を繰り返す
|
101
|
+
public void run() {
|
102
|
+
// 無限ループ、thread が書き換わったら終了
|
103
|
+
for (;;) {
|
104
|
+
// 1秒間 sleep
|
105
|
+
try {
|
106
|
+
int t =(int)(Math.random()*500+1000);
|
107
|
+
Thread.sleep(t);
|
108
|
+
|
109
|
+
|
110
|
+
} catch (InterruptedException e) {
|
111
|
+
e.printStackTrace();
|
112
|
+
}
|
113
|
+
// thread 変数をチェック
|
114
|
+
if (Thread.currentThread() != thread) {
|
115
|
+
break;
|
116
|
+
}
|
117
|
+
int ka =(int)(Math.random()*450);
|
118
|
+
int ki =(int)(Math.random()*450);
|
119
|
+
|
120
|
+
|
121
|
+
// 見た目を設定して表示 or 非表示
|
122
|
+
setBounds(ka,ki,100,100);
|
123
|
+
setBackground(Color.RED);
|
124
|
+
setVisible(!isVisible());
|
125
|
+
}
|
126
|
+
}
|
127
|
+
public void paint(Graphics g)
|
128
|
+
{
|
129
|
+
int w = getWidth();
|
130
|
+
int h = getHeight();
|
131
|
+
g.drawImage(image,0,0,w,h,null);
|
132
|
+
}
|
133
|
+
}
|
134
|
+
|
135
|
+
// 得点
|
136
|
+
protected int score = 0;
|
137
|
+
|
138
|
+
public Wanipani() {
|
139
|
+
// モグラたたきエリアの設定表示エリアの範囲
|
140
|
+
setPreferredSize(new Dimension(500,500));
|
141
|
+
// モグラを1体配置
|
142
|
+
add(new Mogura(10,"mogura.png"));
|
143
|
+
add(new Mogura(-50,"bakudan.png"));
|
144
|
+
}
|
145
|
+
|
146
|
+
// 得点を得たときの挙動
|
147
|
+
protected void incScore(int p)
|
148
|
+
{
|
149
|
+
score += p;
|
150
|
+
repaint();
|
151
|
+
}
|
152
|
+
// JPanel からのオーバーライド
|
153
|
+
// 中身の描画
|
154
|
+
public void paint(Graphics g) {
|
155
|
+
// 得点を表示
|
156
|
+
g.drawString("得点: " + score, 10, 10);
|
157
|
+
// 背景を描画
|
158
|
+
}
|
159
|
+
|
160
|
+
public static void main(String[] args) {
|
161
|
+
JFrame frame = new JFrame("ワニたたき");
|
162
|
+
frame.setContentPane(new Wanipani());
|
163
|
+
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
|
164
|
+
frame.pack();
|
165
|
+
frame.setBackground(Color.blue);
|
166
|
+
frame.setVisible(true);
|
167
|
+
}
|
168
|
+
}
|
15
169
|
###補足情報(言語/FW/ツール等のバージョンなど)
|
16
170
|
より詳細な情報
|
3
タイトル変更してしまったため
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
ワニワニパニックをjavaで作りたいのですがうまくできません。お助けください。
|
body
CHANGED
File without changes
|
2
お礼
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
楽しく遊べました(#^.^#)
|
body
CHANGED
@@ -12,335 +12,5 @@
|
|
12
12
|
```
|
13
13
|
|
14
14
|
###該当のソースコード
|
15
|
-
```import java.awt.*;
|
16
|
-
import java.awt.event.*;
|
17
|
-
import javax.swing.*;
|
18
|
-
import java.awt.image.*;
|
19
|
-
import java.io.*;
|
20
|
-
import javax.imageio.*;
|
21
|
-
import java.awt.Graphics;
|
22
|
-
import java.awt.Graphics2D;
|
23
|
-
import java.awt.geom.AffineTransform;
|
24
|
-
import java.awt.image.BufferedImage;
|
25
|
-
import java.io.IOException;
|
26
|
-
import javax.imageio.ImageIO;
|
27
|
-
import javax.swing.JFrame;
|
28
|
-
import javax.swing.JPanel;
|
29
|
-
import java.awt.Color;
|
30
|
-
import java.awt.event.WindowAdapter;
|
31
|
-
import java.awt.event.WindowEvent;
|
32
|
-
|
33
|
-
public class Wanipani extends JPanel {
|
34
|
-
// モグラたたきのターゲット
|
35
|
-
public class Mogura extends Canvas implements Runnable {
|
36
|
-
// 表示非表示を行うスレッド
|
37
|
-
// 新しいスレッドに変えると古いスレッドが停止する
|
38
|
-
// run() の if 文参照
|
39
|
-
|
40
|
-
Thread thread1;
|
41
|
-
int count;
|
42
|
-
Button button;
|
43
|
-
|
44
|
-
public void init1() {
|
45
|
-
|
46
|
-
setBackground(Color.yellow);
|
47
|
-
button = new Button("スタート");
|
48
|
-
setLayout(new BorderLayout());
|
49
|
-
Panel pnl = new Panel();
|
50
|
-
pnl.add(button);
|
51
|
-
|
52
|
-
}
|
53
|
-
|
54
|
-
private Image image;
|
55
|
-
private int point;
|
56
|
-
protected Thread thread;
|
57
|
-
|
58
|
-
public Mogura(int p,String imagefile) {
|
59
|
-
point = p;
|
60
|
-
try{
|
61
|
-
FileInputStream in = new FileInputStream(imagefile);
|
62
|
-
image = ImageIO.read(in);
|
63
|
-
in.close();
|
64
|
-
}catch (IOException e)
|
65
|
-
{
|
66
|
-
e.printStackTrace();
|
67
|
-
}
|
68
|
-
enableEvents(MouseEvent.MOUSE_PRESSED);
|
69
|
-
init();
|
70
|
-
}
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
// いったん非表示にして状態を初期化
|
75
|
-
// オブジェクト作成時とマウスでたたかれた時に使われる
|
76
|
-
protected void init() {
|
77
|
-
// 非表示
|
78
|
-
setVisible(false);
|
79
|
-
// 表示非表示スレッドを実行 &thread に設定
|
80
|
-
// thread が上書きされこれまでのスレッドは停止する
|
81
|
-
thread = new Thread(this);
|
82
|
-
thread.start();
|
83
|
-
}
|
84
|
-
|
85
|
-
// Canvas クラスからのオーバーライド
|
86
|
-
// MouseEvent を扱う MouseAdapter 以外のもう一つの方法
|
87
|
-
// 使うイベントを enableEvents で登録しておく
|
88
|
-
// (コンストラクタ参照)
|
89
|
-
protected void processMouseEvent(MouseEvent e) {
|
90
|
-
// MousePressed の際に、
|
91
|
-
if (e.getID() == MouseEvent.MOUSE_PRESSED) {
|
92
|
-
// 得点を追加して (MoguraPanel のメソッド)
|
93
|
-
incScore(point);
|
94
|
-
// 非表示にして状態を初期化
|
95
|
-
init();
|
96
|
-
}
|
97
|
-
}
|
98
|
-
|
99
|
-
// Runnable インターフェイスからのオーバーライド
|
100
|
-
// 表示非表示を繰り返す
|
101
|
-
public void run() {
|
102
|
-
// 無限ループ、thread が書き換わったら終了
|
103
|
-
for (;;) {
|
104
|
-
// 1秒間 sleep
|
105
|
-
try {
|
106
|
-
int t =(int)(Math.random()*500+1000);
|
107
|
-
Thread.sleep(t);
|
108
|
-
|
109
|
-
|
110
|
-
} catch (InterruptedException e) {
|
111
|
-
e.printStackTrace();
|
112
|
-
}
|
113
|
-
// thread 変数をチェック
|
114
|
-
if (Thread.currentThread() != thread) {
|
115
|
-
break;
|
116
|
-
}
|
117
|
-
int ka =(int)(Math.random()*450);
|
118
|
-
int ki =(int)(Math.random()*450);
|
119
|
-
|
120
|
-
|
121
|
-
// 見た目を設定して表示 or 非表示
|
122
|
-
setBounds(ka,ki,100,100);
|
123
|
-
setBackground(Color.RED);
|
124
|
-
setVisible(!isVisible());
|
125
|
-
}
|
126
|
-
}
|
127
|
-
public void paint(Graphics g)
|
128
|
-
{
|
129
|
-
int w = getWidth();
|
130
|
-
int h = getHeight();
|
131
|
-
g.drawImage(image,0,0,w,h,null);
|
132
|
-
}
|
133
|
-
}
|
134
|
-
|
135
|
-
// 得点
|
136
|
-
protected int score = 0;
|
137
|
-
|
138
|
-
public Wanipani() {
|
139
|
-
// モグラたたきエリアの設定表示エリアの範囲
|
140
|
-
setPreferredSize(new Dimension(500,500));
|
141
|
-
// モグラを1体配置
|
142
|
-
add(new Mogura(10,"mogura.png"));
|
143
|
-
add(new Mogura(-50,"bakudan.png"));
|
144
|
-
}
|
145
|
-
|
146
|
-
// 得点を得たときの挙動
|
147
|
-
protected void incScore(int p)
|
148
|
-
{
|
149
|
-
score += p;
|
150
|
-
repaint();
|
151
|
-
}
|
152
|
-
// JPanel からのオーバーライド
|
153
|
-
// 中身の描画
|
154
|
-
public void paint(Graphics g) {
|
155
|
-
// 得点を表示
|
156
|
-
g.drawString("得点: " + score, 10, 10);
|
157
|
-
// 背景を描画
|
158
|
-
}
|
159
|
-
|
160
|
-
public static void main(String[] args) {
|
161
|
-
JFrame frame = new JFrame("ワニたたき");
|
162
|
-
frame.setContentPane(new Wanipani());
|
163
|
-
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
|
164
|
-
frame.pack();
|
165
|
-
frame.setBackground(Color.blue);
|
166
|
-
frame.setVisible(true);
|
167
|
-
}
|
168
|
-
}
|
169
|
-
|
170
|
-
|
171
|
-
|
172
15
|
###補足情報(言語/FW/ツール等のバージョンなど)
|
173
|
-
より詳細な情報
|
174
|
-
###前提・実現したいこと
|
175
|
-
スタートボタンによりゲームを開始、ゲーム終了後にスタート画面に戻る。
|
176
|
-
ワニの出る位置を固定したい。
|
177
|
-
1分間でゲーム終了。
|
178
|
-
得点のランキング付け。
|
179
|
-
|
180
|
-
###発生している問題・エラーメッセージ
|
181
|
-
|
182
|
-
```
|
183
|
-
現在の進み具合は青い画面にランダムに画像が表示されクリックすると消え、得点が加算されます。
|
184
|
-
何度試してもスタートボタンが表示されないのとワニの位置を固定できない。
|
185
|
-
```
|
186
|
-
|
187
|
-
###該当のソースコード
|
188
|
-
```import java.awt.*;
|
189
|
-
import java.awt.event.*;
|
190
|
-
import javax.swing.*;
|
191
|
-
import java.awt.image.*;
|
192
|
-
import java.io.*;
|
193
|
-
import javax.imageio.*;
|
194
|
-
import java.awt.Graphics;
|
195
|
-
import java.awt.Graphics2D;
|
196
|
-
import java.awt.geom.AffineTransform;
|
197
|
-
import java.awt.image.BufferedImage;
|
198
|
-
import java.io.IOException;
|
199
|
-
import javax.imageio.ImageIO;
|
200
|
-
import javax.swing.JFrame;
|
201
|
-
import javax.swing.JPanel;
|
202
|
-
import java.awt.Color;
|
203
|
-
import java.awt.event.WindowAdapter;
|
204
|
-
import java.awt.event.WindowEvent;
|
205
|
-
|
206
|
-
public class Wanipani extends JPanel {
|
207
|
-
// モグラたたきのターゲット
|
208
|
-
public class Mogura extends Canvas implements Runnable {
|
209
|
-
// 表示非表示を行うスレッド
|
210
|
-
// 新しいスレッドに変えると古いスレッドが停止する
|
211
|
-
// run() の if 文参照
|
212
|
-
|
213
|
-
Thread thread1;
|
214
|
-
int count;
|
215
|
-
Button button;
|
216
|
-
|
217
|
-
public void init1() {
|
218
|
-
|
219
|
-
setBackground(Color.yellow);
|
220
|
-
button = new Button("スタート");
|
221
|
-
setLayout(new BorderLayout());
|
222
|
-
Panel pnl = new Panel();
|
223
|
-
pnl.add(button);
|
224
|
-
|
225
|
-
}
|
226
|
-
|
227
|
-
private Image image;
|
228
|
-
private int point;
|
229
|
-
protected Thread thread;
|
230
|
-
|
231
|
-
public Mogura(int p,String imagefile) {
|
232
|
-
point = p;
|
233
|
-
try{
|
234
|
-
FileInputStream in = new FileInputStream(imagefile);
|
235
|
-
image = ImageIO.read(in);
|
236
|
-
in.close();
|
237
|
-
}catch (IOException e)
|
238
|
-
{
|
239
|
-
e.printStackTrace();
|
240
|
-
}
|
241
|
-
enableEvents(MouseEvent.MOUSE_PRESSED);
|
242
|
-
init();
|
243
|
-
}
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
// いったん非表示にして状態を初期化
|
248
|
-
// オブジェクト作成時とマウスでたたかれた時に使われる
|
249
|
-
protected void init() {
|
250
|
-
// 非表示
|
251
|
-
setVisible(false);
|
252
|
-
// 表示非表示スレッドを実行 &thread に設定
|
253
|
-
// thread が上書きされこれまでのスレッドは停止する
|
254
|
-
thread = new Thread(this);
|
255
|
-
thread.start();
|
256
|
-
}
|
257
|
-
|
258
|
-
// Canvas クラスからのオーバーライド
|
259
|
-
// MouseEvent を扱う MouseAdapter 以外のもう一つの方法
|
260
|
-
// 使うイベントを enableEvents で登録しておく
|
261
|
-
// (コンストラクタ参照)
|
262
|
-
protected void processMouseEvent(MouseEvent e) {
|
263
|
-
// MousePressed の際に、
|
264
|
-
if (e.getID() == MouseEvent.MOUSE_PRESSED) {
|
265
|
-
// 得点を追加して (MoguraPanel のメソッド)
|
266
|
-
incScore(point);
|
267
|
-
// 非表示にして状態を初期化
|
268
|
-
init();
|
269
|
-
}
|
270
|
-
}
|
271
|
-
|
272
|
-
// Runnable インターフェイスからのオーバーライド
|
273
|
-
// 表示非表示を繰り返す
|
274
|
-
public void run() {
|
275
|
-
// 無限ループ、thread が書き換わったら終了
|
276
|
-
for (;;) {
|
277
|
-
// 1秒間 sleep
|
278
|
-
try {
|
279
|
-
int t =(int)(Math.random()*500+1000);
|
280
|
-
Thread.sleep(t);
|
281
|
-
|
282
|
-
|
283
|
-
} catch (InterruptedException e) {
|
284
|
-
e.printStackTrace();
|
285
|
-
}
|
286
|
-
// thread 変数をチェック
|
287
|
-
if (Thread.currentThread() != thread) {
|
288
|
-
break;
|
289
|
-
}
|
290
|
-
int ka =(int)(Math.random()*450);
|
291
|
-
int ki =(int)(Math.random()*450);
|
292
|
-
|
293
|
-
|
294
|
-
// 見た目を設定して表示 or 非表示
|
295
|
-
setBounds(ka,ki,100,100);
|
296
|
-
setBackground(Color.RED);
|
297
|
-
setVisible(!isVisible());
|
298
|
-
}
|
299
|
-
}
|
300
|
-
public void paint(Graphics g)
|
301
|
-
{
|
302
|
-
int w = getWidth();
|
303
|
-
int h = getHeight();
|
304
|
-
g.drawImage(image,0,0,w,h,null);
|
305
|
-
}
|
306
|
-
}
|
307
|
-
|
308
|
-
// 得点
|
309
|
-
protected int score = 0;
|
310
|
-
|
311
|
-
public Wanipani() {
|
312
|
-
// モグラたたきエリアの設定表示エリアの範囲
|
313
|
-
setPreferredSize(new Dimension(500,500));
|
314
|
-
// モグラを1体配置
|
315
|
-
add(new Mogura(10,"mogura.png"));
|
316
|
-
add(new Mogura(-50,"bakudan.png"));
|
317
|
-
}
|
318
|
-
|
319
|
-
// 得点を得たときの挙動
|
320
|
-
protected void incScore(int p)
|
321
|
-
{
|
322
|
-
score += p;
|
323
|
-
repaint();
|
324
|
-
}
|
325
|
-
// JPanel からのオーバーライド
|
326
|
-
// 中身の描画
|
327
|
-
public void paint(Graphics g) {
|
328
|
-
// 得点を表示
|
329
|
-
g.drawString("得点: " + score, 10, 10);
|
330
|
-
// 背景を描画
|
331
|
-
}
|
332
|
-
|
333
|
-
public static void main(String[] args) {
|
334
|
-
JFrame frame = new JFrame("ワニたたき");
|
335
|
-
frame.setContentPane(new Wanipani());
|
336
|
-
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
|
337
|
-
frame.pack();
|
338
|
-
frame.setBackground(Color.blue);
|
339
|
-
frame.setVisible(true);
|
340
|
-
}
|
341
|
-
}
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
###補足情報(言語/FW/ツール等のバージョンなど)
|
346
16
|
より詳細な情報
|
1
お礼
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
助かりました(#^.^#)
|
body
CHANGED
File without changes
|