質問編集履歴

6

改善

2017/12/18 06:00

投稿

eamons
eamons

スコア12

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,122 @@
1
- **- 0. ![ボールドテキスト](c32c37fa250cef4fde99fb2b742ca868.png)**今回はブロック崩しのブロックを作りたいとおもっているのですが、困っています。ヒントをもらえないでしょうか。エラーは上記の図です
1
+ 今回はブロック崩しのブロックを作りたいとおもっているのですが、困っています。ヒントをもらえないでしょうか。
2
+
2
-
3
+ ```
4
+
5
+ public class Block {
6
+
7
+ int iRow, iCol; // このブロックの行と列番号
8
+
9
+ int x, y; // ブロックの中心座標
10
+
11
+ static final double MARGIN = 0.1; // ブロック間の空白はウインドウ幅/nColの10%
12
+
13
+ static final double YMIN = 0.1; // 上端y座標のウインドウ高さに対する比率
14
+
15
+ // 省略
16
+
17
+ }
18
+
19
+ ```
20
+
21
+
22
+
23
+ ```ここに言語を入力
24
+
25
+ public class Block {
26
+
27
+ int iRow=3, iCol=5; // このブロックの行と列番号
28
+
29
+ int x, y; // ブロックの中心座標
30
+
31
+ public boolean collision(Ball ball) {
32
+
33
+ // 側面への衝突は考慮しなくてよい.
34
+
35
+ // 衝突したらtrueを,しなければfalseを返す
36
+
37
+ if (!visible) return false; // すでに消えているので衝突しない
38
+
39
+ // y成分の判定: ボール中心とブロック中心間の距離 < ボール半径とブロック高さ/2の和 ?
40
+
41
+ if (Math.abs(ball.y - y) < ball.radius + height / 2) { // ブロックとの衝突判定
42
+
43
+ if (/* x成分の判定 */) {
44
+
45
+ if (block[i][j].y<ball.y); // 上から衝突
46
+
47
+ else ; // 下から衝突
48
+
49
+ visible = false; // ブロックを消す
50
+
51
+ return true; // 衝突した
52
+
53
+ }
54
+
55
+ }
56
+
57
+ return false; // 衝突せず
58
+
59
+ }
60
+
61
+ public boolean draw(Graphics m_g, Dimension dim) {
62
+
63
+ if (!visible) return;
64
+
65
+ m_g.setColor(Color.ORANGE);
66
+
67
+ }
68
+
69
+ public void draw(){
70
+
71
+
72
+
73
+ }
74
+
75
+ public Block(int i, int j){
76
+
77
+
78
+
79
+ }
80
+
81
+
82
+
83
+
84
+
85
+ static final double MARGIN = 0.1; // ブロック間の空白はウインドウ幅/nColの10%
86
+
87
+ static final double YMIN = 0.1; // 上端y座標のウインドウ高さに対する比率
88
+
89
+ }
90
+
91
+ }
92
+
93
+ ```
94
+
95
+ に変更したところ、
96
+
97
+ block[i][j] = new Block(i, j);のBlock(i, j)
98
+
99
+ if (block[i][j].collision(ball)) のcollision(ball)
100
+
101
+ public int draw(Graphics m_g, Dimension dim) の draw(Graphics m_g, Dimension dim)
102
+
103
+ のエラーはきえましたが
104
+
105
+ ```ここに言語を入力
106
+
107
+ public boolean draw(Graphics m_g, Dimension dim) {
108
+
109
+ if (!visible) return;
110
+
111
+ m_g.setColor(Color.ORANGE);
112
+
113
+ }
114
+
115
+ ```
116
+
117
+ Blockのクラスのdrawのreturnが今度はエラーになります
118
+
3
- どれもエラーはThe method...is undefined for the type Blocks.Block
119
+ エラーメッセージはThis method must return a result of type booleanです。
4
120
 
5
121
  ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
6
122
 
@@ -92,7 +208,7 @@
92
208
 
93
209
  this.addMouseListener(pad);
94
210
 
95
- blocks.collision(ball);
211
+
96
212
 
97
213
 
98
214
 
@@ -158,8 +274,6 @@
158
274
 
159
275
  pad.move(dim, ball);
160
276
 
161
-
162
-
163
277
  repaint(); // 再描画要求
164
278
 
165
279
  }
@@ -370,82 +484,138 @@
370
484
 
371
485
  public class Blocks {
372
486
 
373
- int nRow = 3, nCol = 5; // nRow: 行数,nCol: 列数
487
+ int nRow = 3, nCol = 5; // nRow: 行数,nCol: 列数
374
488
 
375
489
  Block block[][];
376
490
 
377
- boolean visible = true;
491
+ boolean visible = true;
378
-
492
+
379
- public Blocks() {
493
+ public Blocks() {
380
-
494
+
381
- block = new Block[nRow][nCol];
495
+ block = new Block[nRow][nCol];
382
-
496
+
383
- for (int i = 0; i < nRow; i++) {
497
+ for (int i = 0; i < nRow; i++) {
384
-
498
+
385
- for (int j = 0; j < nCol; j++) {
499
+ for (int j = 0; j < nCol; j++) {
386
-
500
+
387
- block[i][j] = new Block(i, j);
501
+ block[i][j] = new Block(i, j);
502
+
503
+ }
504
+
505
+ }
506
+
507
+ }
508
+
509
+
510
+
511
+ public int collision(Ball ball) {
512
+
513
+ int numColl = 0; // 衝突回数
514
+
515
+ for (int i = 0; i < nRow; i++) {
516
+
517
+ for (int j = 0; j < nCol; j++) {
518
+
519
+ if (block[i][j].collision(ball)) numColl++;
520
+
521
+ }
522
+
523
+ }
524
+
525
+ return numColl;
526
+
527
+ }
528
+
529
+ public int draw(Graphics m_g, Dimension dim) {
530
+
531
+ int numDrawn = 0; // ブロックの描画回数.0なら面クリア.
532
+
533
+
534
+
535
+ for (int i = 0; i < nRow; i++) {
536
+
537
+ for (int j = 0; j < nCol; j++) {
538
+
539
+ if (block[i][j].draw(m_g, dim))
540
+
541
+ m_g.setColor(Color.ORANGE);
542
+
543
+ numDrawn++;
544
+
545
+ }
546
+
547
+ }
548
+
549
+ return numDrawn;
550
+
551
+ }
552
+
553
+ public class Block {
554
+
555
+ int iRow=3, iCol=5; // このブロックの行と列番号
556
+
557
+ int x, y; // ブロックの中心座標
558
+
559
+ public boolean collision(Ball ball) {
560
+
561
+ // 側面への衝突は考慮しなくてよい.
562
+
563
+ // 衝突したらtrueを,しなければfalseを返す
564
+
565
+ if (!visible) return false; // すでに消えているので衝突しない
566
+
567
+ // y成分の判定: ボール中心とブロック中心間の距離 < ボール半径とブロック高さ/2の和 ?
568
+
569
+ if (Math.abs(ball.y - y) < ball.radius + height / 2) { // ブロックとの衝突判定
570
+
571
+ if (/* x成分の判定 */) {
572
+
573
+ if (block[i][j].y<ball.y); // 上から衝突
574
+
575
+ else ; // 下から衝突
576
+
577
+ visible = false; // ブロックを消す
578
+
579
+ return true; // 衝突した
580
+
581
+ }
582
+
583
+ }
584
+
585
+ return false; // 衝突せず
388
586
 
389
587
  }
390
588
 
589
+ public boolean draw(Graphics m_g, Dimension dim) {
590
+
591
+ if (!visible) return;
592
+
593
+ m_g.setColor(Color.ORANGE);
594
+
391
- }
595
+ }
596
+
392
-
597
+ public void draw(){
598
+
599
+
600
+
393
- }
601
+ }
394
-
395
-
396
-
602
+
397
- public int collision(Ball ball) {
603
+ public Block(int i, int j){
398
-
399
- int numColl = 0; // 衝突回数
604
+
400
-
401
- for (int i = 0; i < nRow; i++) {
605
+
402
-
403
- for (int j = 0; j < nCol; j++) {
606
+
404
-
405
- if (block[i][j].collision(ball)) numColl++;
406
-
407
- }
607
+ }
608
+
609
+
610
+
611
+
612
+
408
-
613
+ static final double MARGIN = 0.1; // ブロック間の空白はウインドウ幅/nColの10%
614
+
615
+ static final double YMIN = 0.1; // 上端y座標のウインドウ高さに対する比率
616
+
409
- }
617
+ }
410
-
411
- return numColl;
618
+
412
-
413
- }
619
+ }
414
-
415
- public int draw(Graphics m_g, Dimension dim) {
620
+
416
-
417
- int numDrawn = 0; // ブロックの描画回数.0なら面クリア.
418
-
419
-
420
-
421
-
422
-
423
- for (int i = 0; i < nRow; i++) {
424
-
425
- for (int j = 0; j < nCol; j++) {
426
-
427
- if (block[i][j].draw(m_g, dim)) numDrawn++;
428
-
429
- }
621
+ }
430
-
431
- }
432
-
433
- return numDrawn;
434
-
435
- }
436
-
437
- public class Block {
438
-
439
- int iRow, iCol; // このブロックの行と列番号
440
-
441
- int x, y; // ブロックの中心座標
442
-
443
- static final double MARGIN = 0.1; // ブロック間の空白はウインドウ幅/nColの10%
444
-
445
- static final double YMIN = 0.1; // 上端y座標のウインドウ高さに対する比率
446
-
447
- }
448
-
449
- }
450
-
451
- }

5

修正

2017/12/18 06:00

投稿

eamons
eamons

スコア12

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- **- 0. ![ボールドテキスト](c32c37fa250cef4fde99fb2b742ca868.png)**今回はブロック崩しのブロックを作りたいとおもっているのですが、エラーが消えなくて困っています。ヒントをもらえないでしょうか。エラーは上記の図です
1
+ **- 0. ![ボールドテキスト](c32c37fa250cef4fde99fb2b742ca868.png)**今回はブロック崩しのブロックを作りたいとおもっているのですが、困っています。ヒントをもらえないでしょうか。エラーは上記の図です
2
2
 
3
3
  どれもエラーは、The method...is undefined for the type Blocks.Block
4
4
 
@@ -444,8 +444,6 @@
444
444
 
445
445
  static final double YMIN = 0.1; // 上端y座標のウインドウ高さに対する比率
446
446
 
447
- // 省略
448
-
449
447
  }
450
448
 
451
449
  }

4

誤字

2017/12/18 04:40

投稿

eamons
eamons

スコア12

test CHANGED
File without changes
test CHANGED
@@ -128,7 +128,7 @@
128
128
 
129
129
  pad.draw(g, dim);
130
130
 
131
- blocks.draw(m_g, dim);
131
+ blocks.draw(g, dim);
132
132
 
133
133
 
134
134
 

3

修正

2017/12/18 04:30

投稿

eamons
eamons

スコア12

test CHANGED
File without changes
test CHANGED
@@ -410,48 +410,26 @@
410
410
 
411
411
  return numColl;
412
412
 
413
+ }
414
+
413
- if (!visible) return false; // すでに消えているので衝突しない
415
+ public int draw(Graphics m_g, Dimension dim) {
414
-
415
- // y成分の判定: ボール中心とブロック中心間の距離 < ボール半径とブロック高さ/2の和 ?
416
+
416
-
417
- if (Math.abs(ball.y - y) < ball.radius + height / 2) { // ブロックとの衝突判定
418
-
419
- if (/* x成分の判定 */) {
420
-
421
- if ; // 上から衝突
422
-
423
- else ; // 下から衝突
424
-
425
- visible = false; // ブロックを消す
417
+ int numDrawn = 0; // ブロックの描画回数.0なら面クリア.
426
-
418
+
419
+
420
+
421
+
422
+
427
- return true; // 衝突した
423
+ for (int i = 0; i < nRow; i++) {
424
+
425
+ for (int j = 0; j < nCol; j++) {
426
+
427
+ if (block[i][j].draw(m_g, dim)) numDrawn++;
428
428
 
429
429
  }
430
430
 
431
431
  }
432
432
 
433
- return false; // 衝突せず
434
-
435
- }
436
-
437
- public int draw(Graphics m_g, Dimension dim) {
438
-
439
- int numDrawn = 0; // ブロックの描画回数.0なら面クリア.
440
-
441
-
442
-
443
-
444
-
445
- for (int i = 0; i < nRow; i++) {
446
-
447
- for (int j = 0; j < nCol; j++) {
448
-
449
- if (block[i][j].draw(m_g, dim)) numDrawn++;
450
-
451
- }
452
-
453
- }
454
-
455
433
  return numDrawn;
456
434
 
457
435
  }

2

修正

2017/12/18 04:28

投稿

eamons
eamons

スコア12

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

1

編集

2017/12/18 03:37

投稿

eamons
eamons

スコア12

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- 今回はブロック崩しのブロックを作りたいとおもっているのですが、エラーが消えなくて困っています。ヒントをもらえないでしょうか。
1
+ **- 0. ![ボールドテキスト](c32c37fa250cef4fde99fb2b742ca868.png)**今回はブロック崩しのブロックを作りたいとおもっているのですが、エラーが消えなくて困っています。ヒントをもらえないでしょうか。エラーは上記の図です
2
2
 
3
3
  ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
4
4
 
@@ -30,7 +30,7 @@
30
30
 
31
31
  }
32
32
 
33
-
33
+ -----------------------------------------------------------------------
34
34
 
35
35
  // BlockCanvas.java
36
36