質問するログイン新規登録

質問編集履歴

6

改善

2017/12/18 06:00

投稿

eamons
eamons

スコア12

title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,63 @@
1
- **- 0. ![ボールドテキスト](c32c37fa250cef4fde99fb2b742ca868.png)**今回はブロック崩しのブロックを作りたいとおもっているのですが、困っています。ヒントをもらえないでしょうか。エラーは上記の図です
1
+ 今回はブロック崩しのブロックを作りたいとおもっているのですが、困っています。ヒントをもらえないでしょうか。
2
+ ```
3
+ public class Block {
4
+ int iRow, iCol; // このブロックの行と列番号
5
+ int x, y; // ブロックの中心座標
6
+ static final double MARGIN = 0.1; // ブロック間の空白はウインドウ幅/nColの10%
7
+ static final double YMIN = 0.1; // 上端y座標のウインドウ高さに対する比率
8
+ // 省略
9
+ }
10
+ ```
11
+
12
+ ```ここに言語を入力
13
+ public class Block {
14
+ int iRow=3, iCol=5; // このブロックの行と列番号
15
+ int x, y; // ブロックの中心座標
16
+ public boolean collision(Ball ball) {
17
+ // 側面への衝突は考慮しなくてよい.
18
+ // 衝突したらtrueを,しなければfalseを返す
19
+ if (!visible) return false; // すでに消えているので衝突しない
20
+ // y成分の判定: ボール中心とブロック中心間の距離 < ボール半径とブロック高さ/2の和 ?
21
+ if (Math.abs(ball.y - y) < ball.radius + height / 2) { // ブロックとの衝突判定
22
+ if (/* x成分の判定 */) {
23
+ if (block[i][j].y<ball.y); // 上から衝突
24
+ else ; // 下から衝突
25
+ visible = false; // ブロックを消す
26
+ return true; // 衝突した
27
+ }
28
+ }
29
+ return false; // 衝突せず
30
+ }
31
+ public boolean draw(Graphics m_g, Dimension dim) {
32
+ if (!visible) return;
33
+ m_g.setColor(Color.ORANGE);
34
+ }
35
+ public void draw(){
36
+
37
+ }
38
+ public Block(int i, int j){
39
+
40
+ }
41
+
42
+
43
+ static final double MARGIN = 0.1; // ブロック間の空白はウインドウ幅/nColの10%
44
+ static final double YMIN = 0.1; // 上端y座標のウインドウ高さに対する比率
45
+ }
46
+ }
47
+ ```
48
+ に変更したところ、
49
+ block[i][j] = new Block(i, j);のBlock(i, j)
50
+ if (block[i][j].collision(ball)) のcollision(ball)
51
+ public int draw(Graphics m_g, Dimension dim) の draw(Graphics m_g, Dimension dim)
52
+ のエラーはきえましたが
53
+ ```ここに言語を入力
54
+ public boolean draw(Graphics m_g, Dimension dim) {
55
+ if (!visible) return;
56
+ m_g.setColor(Color.ORANGE);
57
+ }
58
+ ```
59
+ Blockのクラスのdrawのreturnが今度はエラーになります
2
- どれもエラーは、The method...is undefined for the type Blocks.Block
60
+ エラーメッセージThis method must return a result of type booleanです。
3
61
  ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
4
62
  // BlockMain.java
5
63
  import javax.swing.JFrame;
@@ -45,7 +103,7 @@
45
103
  thread.start(); // スレッドを開始 (run()が呼ばれる)
46
104
  setPreferredSize(dim);
47
105
  this.addMouseListener(pad);
48
- blocks.collision(ball);
106
+
49
107
 
50
108
  }
51
109
 
@@ -78,7 +136,6 @@
78
136
  }
79
137
  ball.move(dim);
80
138
  pad.move(dim, ball);
81
-
82
139
  repaint(); // 再描画要求
83
140
  }
84
141
  }
@@ -184,43 +241,71 @@
184
241
  }
185
242
  }
186
243
  public class Blocks {
187
- int nRow = 3, nCol = 5; // nRow: 行数,nCol: 列数
244
+ int nRow = 3, nCol = 5; // nRow: 行数,nCol: 列数
188
245
  Block block[][];
189
- boolean visible = true;
246
+ boolean visible = true;
190
- public Blocks() {
247
+ public Blocks() {
191
- block = new Block[nRow][nCol];
248
+ block = new Block[nRow][nCol];
192
- for (int i = 0; i < nRow; i++) {
249
+ for (int i = 0; i < nRow; i++) {
193
- for (int j = 0; j < nCol; j++) {
250
+ for (int j = 0; j < nCol; j++) {
194
- block[i][j] = new Block(i, j);
251
+ block[i][j] = new Block(i, j);
195
- }
252
+ }
196
- }
253
+ }
197
- }
254
+ }
198
255
 
199
256
  public int collision(Ball ball) {
200
- int numColl = 0; // 衝突回数
257
+ int numColl = 0; // 衝突回数
201
- for (int i = 0; i < nRow; i++) {
258
+ for (int i = 0; i < nRow; i++) {
202
- for (int j = 0; j < nCol; j++) {
259
+ for (int j = 0; j < nCol; j++) {
203
- if (block[i][j].collision(ball)) numColl++;
260
+ if (block[i][j].collision(ball)) numColl++;
204
- }
261
+ }
205
- }
262
+ }
206
- return numColl;
263
+ return numColl;
207
264
  }
208
265
  public int draw(Graphics m_g, Dimension dim) {
209
- int numDrawn = 0; // ブロックの描画回数.0なら面クリア.
266
+ int numDrawn = 0; // ブロックの描画回数.0なら面クリア.
210
267
 
211
-
212
- for (int i = 0; i < nRow; i++) {
268
+ for (int i = 0; i < nRow; i++) {
213
- for (int j = 0; j < nCol; j++) {
269
+ for (int j = 0; j < nCol; j++) {
214
- if (block[i][j].draw(m_g, dim)) numDrawn++;
270
+ if (block[i][j].draw(m_g, dim))
271
+ m_g.setColor(Color.ORANGE);
272
+ numDrawn++;
273
+ }
274
+ }
275
+ return numDrawn;
276
+ }
277
+ public class Block {
278
+ int iRow=3, iCol=5; // このブロックの行と列番号
279
+ int x, y; // ブロックの中心座標
280
+ public boolean collision(Ball ball) {
281
+ // 側面への衝突は考慮しなくてよい.
282
+ // 衝突したらtrueを,しなければfalseを返す
283
+ if (!visible) return false; // すでに消えているので衝突しない
284
+ // y成分の判定: ボール中心とブロック中心間の距離 < ボール半径とブロック高さ/2の和 ?
285
+ if (Math.abs(ball.y - y) < ball.radius + height / 2) { // ブロックとの衝突判定
286
+ if (/* x成分の判定 */) {
287
+ if (block[i][j].y<ball.y); // 上から衝突
288
+ else ; // 下から衝突
289
+ visible = false; // ブロックを消す
290
+ return true; // 衝突した
291
+ }
292
+ }
293
+ return false; // 衝突せず
215
294
  }
295
+ public boolean draw(Graphics m_g, Dimension dim) {
296
+ if (!visible) return;
297
+ m_g.setColor(Color.ORANGE);
216
298
  }
217
- return numDrawn;
299
+ public void draw(){
300
+
301
+ }
302
+ public Block(int i, int j){
303
+
304
+ }
305
+
306
+
307
+ static final double MARGIN = 0.1; // ブロック間の空白はウインドウ幅/nColの10%
308
+ static final double YMIN = 0.1; // 上端y座標のウインドウ高さに対する比率
218
309
  }
219
- public class Block {
220
- int iRow, iCol; // このブロックの行と列番号
221
- int x, y; // ブロックの中心座標
222
- static final double MARGIN = 0.1; // ブロック間の空白はウインドウ幅/nColの10%
223
- static final double YMIN = 0.1; // 上端y座標のウインドウ高さに対する比率
224
- }
310
+ }
225
- }
311
+ }
226
- }

5

修正

2017/12/18 06:00

投稿

eamons
eamons

スコア12

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,4 @@
1
- **- 0. ![ボールドテキスト](c32c37fa250cef4fde99fb2b742ca868.png)**今回はブロック崩しのブロックを作りたいとおもっているのですが、エラーが消えなくて困っています。ヒントをもらえないでしょうか。エラーは上記の図です
1
+ **- 0. ![ボールドテキスト](c32c37fa250cef4fde99fb2b742ca868.png)**今回はブロック崩しのブロックを作りたいとおもっているのですが、困っています。ヒントをもらえないでしょうか。エラーは上記の図です
2
2
  どれもエラーは、The method...is undefined for the type Blocks.Block
3
3
  ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
4
4
  // BlockMain.java
@@ -221,7 +221,6 @@
221
221
  int x, y; // ブロックの中心座標
222
222
  static final double MARGIN = 0.1; // ブロック間の空白はウインドウ幅/nColの10%
223
223
  static final double YMIN = 0.1; // 上端y座標のウインドウ高さに対する比率
224
- // 省略
225
224
  }
226
225
  }
227
226
  }

4

誤字

2017/12/18 04:40

投稿

eamons
eamons

スコア12

title CHANGED
File without changes
body CHANGED
@@ -63,7 +63,7 @@
63
63
  ball.draw(m_g, dim);
64
64
  g.drawImage(m_image, 0, 0, this); // 裏画面を表画面にコピー
65
65
  pad.draw(g, dim);
66
- blocks.draw(m_g, dim);
66
+ blocks.draw(g, dim);
67
67
 
68
68
  }
69
69
 

3

修正

2017/12/18 04:30

投稿

eamons
eamons

スコア12

title CHANGED
File without changes
body CHANGED
@@ -204,17 +204,6 @@
204
204
  }
205
205
  }
206
206
  return numColl;
207
- if (!visible) return false; // すでに消えているので衝突しない
208
- // y成分の判定: ボール中心とブロック中心間の距離 < ボール半径とブロック高さ/2の和 ?
209
- if (Math.abs(ball.y - y) < ball.radius + height / 2) { // ブロックとの衝突判定
210
- if (/* x成分の判定 */) {
211
- if ; // 上から衝突
212
- else ; // 下から衝突
213
- visible = false; // ブロックを消す
214
- return true; // 衝突した
215
- }
216
- }
217
- return false; // 衝突せず
218
207
  }
219
208
  public int draw(Graphics m_g, Dimension dim) {
220
209
  int numDrawn = 0; // ブロックの描画回数.0なら面クリア.

2

修正

2017/12/18 04:28

投稿

eamons
eamons

スコア12

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,5 @@
1
1
  **- 0. ![ボールドテキスト](c32c37fa250cef4fde99fb2b742ca868.png)**今回はブロック崩しのブロックを作りたいとおもっているのですが、エラーが消えなくて困っています。ヒントをもらえないでしょうか。エラーは上記の図です
2
+ どれもエラーは、The method...is undefined for the type Blocks.Block
2
3
  ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
3
4
  // BlockMain.java
4
5
  import javax.swing.JFrame;

1

編集

2017/12/18 03:37

投稿

eamons
eamons

スコア12

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,4 @@
1
- 今回はブロック崩しのブロックを作りたいとおもっているのですが、エラーが消えなくて困っています。ヒントをもらえないでしょうか。
1
+ **- 0. ![ボールドテキスト](c32c37fa250cef4fde99fb2b742ca868.png)**今回はブロック崩しのブロックを作りたいとおもっているのですが、エラーが消えなくて困っています。ヒントをもらえないでしょうか。エラーは上記の図です
2
2
  ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
3
3
  // BlockMain.java
4
4
  import javax.swing.JFrame;
@@ -14,7 +14,7 @@
14
14
  w.setVisible(true); //表示する
15
15
  }
16
16
  }
17
-
17
+ -----------------------------------------------------------------------
18
18
  // BlockCanvas.java
19
19
  import java.awt.Canvas;
20
20
  import java.awt.Color;