質問編集履歴

2

文章を追記

2019/06/15 13:44

投稿

hidekiti
hidekiti

スコア23

test CHANGED
File without changes
test CHANGED
@@ -544,6 +544,10 @@
544
544
 
545
545
  ```
546
546
 
547
+ MyKeyAdapter クラスの中で Bar クラスの 変数 lef_key か ri_key の真偽を書き換えるようにしてるのですが、反応がないのでおそらく、MyKeyAdapter がうまく動いていなのだと思います。
548
+
549
+ どのように書き換えるべきか、ググったのですがわかりませんでした。どう書き換えべきでしょうか?
550
+
547
551
  ### 試したこと
548
552
 
549
553
 

1

コードの全体を表記したのと、javaのバージョンを追記

2019/06/15 13:44

投稿

hidekiti
hidekiti

スコア23

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,6 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
-
3
+ ブロック崩しが作りたく、このコードを描いた。
4
4
 
5
5
  keyEventを読み込んで、MyKeyAdapter 内のメソッドを動かしたい。
6
6
 
@@ -8,13 +8,11 @@
8
8
 
9
9
  ### 発生している問題・エラーメッセージ
10
10
 
11
-
12
-
13
- キーを押しても全く反応しない。
11
+ この中で、ボールを打ち返す Bar クラスにおいてBarを左右キー入力で動かすつもりでコード描いたがキーを押しても全く反応しない。
14
12
 
15
13
  ### 該当のソースコード
16
14
 
17
-
15
+ ブロック崩しのコントローラーとしてのクラス
18
16
 
19
17
  ```java
20
18
 
@@ -192,9 +190,359 @@
192
190
 
193
191
  }
194
192
 
193
+ class Block extends ActiveObject{
194
+
195
+ DrawingCanvas canvas;
196
+
197
+ FilledRect rect;
198
+
199
+ FramedRect Rect;
200
+
201
+ MoveBall ball;
202
+
203
+ final int pause_time=6;
204
+
205
+ private boolean removed=false;
206
+
207
+
208
+
209
+ public Block(double x,double y,double width,double height,DrawingCanvas Canvas,MoveBall b){
210
+
211
+ canvas=Canvas;
212
+
213
+ ball=b;
214
+
215
+ rect=new FilledRect(x,y,width,height,canvas);
216
+
217
+ Rect=new FramedRect(x,y,width,height,canvas);
218
+
219
+ Rect.setColor(Color.YELLOW);
220
+
221
+ start();
222
+
223
+
224
+
225
+ }
226
+
227
+
228
+
229
+ @Override public void run(){
230
+
231
+ Location up_ri,up_lef,dow_ri,dow_lef;
232
+
233
+
234
+
235
+ while(!removed){
236
+
237
+ up_ri=new Location(ball.getX(),ball.getY());
238
+
239
+ up_lef=new Location(ball.getX()+ball.getSize(),ball.getY());
240
+
241
+ dow_ri=new Location(ball.getX(),ball.getY()+ball.getSize());
242
+
243
+ dow_lef=new Location(ball.getX()+ball.getSize(),ball.getY()+ball.getSize());
244
+
245
+ if(rect.contains(up_ri)&&rect.contains(up_lef)||rect.contains(dow_ri)&&rect.contains(dow_lef)){
246
+
247
+ ball.turnMoveY();
248
+
249
+ removeFromCanvas();
250
+
251
+ removed=true;
252
+
253
+ }else if(rect.contains(up_ri)&&rect.contains(dow_ri)||rect.contains(up_lef)&&rect.contains(dow_lef)){
254
+
255
+ ball.turnMoveX();
256
+
257
+ removeFromCanvas();
258
+
259
+ removed=true;
260
+
261
+ }else if (rect.contains(up_ri)||rect.contains(up_lef)||rect.contains(dow_ri)||rect.contains(dow_lef)){
262
+
263
+ ball.turnMoveY();
264
+
265
+ ball.turnMoveX();
266
+
267
+ removeFromCanvas();
268
+
269
+ removed=true;
270
+
271
+ }
272
+
273
+ pause(pause_time);
274
+
275
+ }
276
+
277
+
278
+
279
+
280
+
281
+ }
282
+
283
+ public void removeFromCanvas(){
284
+
285
+ rect.removeFromCanvas();
286
+
287
+ Rect.removeFromCanvas();
288
+
289
+ }
290
+
291
+
292
+
293
+ }
294
+
195
295
  ```
196
296
 
197
-
297
+ ボールを打ち返すための Bar クラス
298
+
299
+ ```java
300
+
301
+ import java.applet.Applet;
302
+
303
+ import objectdraw.*;
304
+
305
+ import java.awt.*;
306
+
307
+ import java.awt.event.KeyListener;
308
+
309
+ import java.awt.event.KeyEvent;
310
+
311
+ public class Bar extends ActiveObject {
312
+
313
+ private DrawingCanvas canvas;
314
+
315
+ private FilledRect bar;
316
+
317
+ MoveBall ball;
318
+
319
+ public static boolean lef_key=false,ri_key=false,gamestart=false;
320
+
321
+ private final int pause_time=6,speed=3,bar_height=5;
322
+
323
+
324
+
325
+ public Bar(double x,double y,int length,DrawingCanvas Canvas,MoveBall Ball){
326
+
327
+ canvas=Canvas;
328
+
329
+ ball=Ball;
330
+
331
+ gamestart=true;
332
+
333
+ bar=new FilledRect(x,y,length,bar_height,canvas);
334
+
335
+ start();
336
+
337
+ }
338
+
339
+
340
+
341
+ public Bar(Location point,int length,DrawingCanvas Canvas,MoveBall Ball){
342
+
343
+ this(point.getX(),point.getY(),length,Canvas,Ball);
344
+
345
+ }
346
+
347
+ @Override public void run(){
348
+
349
+ Location up_ri,up_lef,dow_ri,dow_lef;
350
+
351
+ while(gamestart){
352
+
353
+ if(lef_key){
354
+
355
+ bar.move(-speed,0);
356
+
357
+ } else if(ri_key){
358
+
359
+ bar.move(speed,0);
360
+
361
+ }
362
+
363
+ up_ri=new Location(ball.getX(),ball.getY());
364
+
365
+ up_lef=new Location(ball.getX()+ball.getSize(),ball.getY());
366
+
367
+ dow_ri=new Location(ball.getX(),ball.getY()+ball.getSize());
368
+
369
+ dow_lef=new Location(ball.getX()+ball.getSize(),ball.getY()+ball.getSize());
370
+
371
+ if(bar.contains(up_ri)&&bar.contains(up_lef)||bar.contains(dow_ri)&&bar.contains(dow_lef)){
372
+
373
+ ball.turnMoveY();
374
+
375
+ }else if(bar.contains(up_ri)&&bar.contains(dow_ri)||bar.contains(up_lef)&&bar.contains(dow_lef)){
376
+
377
+ ball.turnMoveX();
378
+
379
+ }else if (bar.contains(up_ri)||bar.contains(up_lef)||bar.contains(dow_ri)||bar.contains(dow_lef)){
380
+
381
+ ball.turnMoveY();
382
+
383
+ ball.turnMoveX();
384
+
385
+ }
386
+
387
+ pause(pause_time);
388
+
389
+ }
390
+
391
+ }
392
+
393
+ public void move(double dx,double dy){
394
+
395
+ bar.move(dx,dy);
396
+
397
+ }
398
+
399
+ }
400
+
401
+ ```
402
+
403
+ ボールのクラス
404
+
405
+ ```java
406
+
407
+ import objectdraw.*;
408
+
409
+ import java.awt.*;
410
+
411
+
412
+
413
+ public class MoveBall extends ActiveObject{
414
+
415
+ private FilledOval ball;
416
+
417
+ public final int pauseTime=33,
418
+
419
+ margin=5;
420
+
421
+ private int size=5,
422
+
423
+ ball_speed=8;
424
+
425
+ DrawingCanvas canvas;
426
+
427
+ RandomIntGenerator generator1=new RandomIntGenerator(-5,5);
428
+
429
+ RandomIntGenerator generator2=new RandomIntGenerator(-3,-5);
430
+
431
+ private int move_x=generator1.nextValue();
432
+
433
+ private int move_y=generator2.nextValue();
434
+
435
+ public MoveBall(double x,double y,DrawingCanvas Canvas){
436
+
437
+ canvas=Canvas;
438
+
439
+ ball=new FilledOval(x,y,size,size,Canvas);
440
+
441
+
442
+
443
+ }
444
+
445
+ public MoveBall(Location point,DrawingCanvas Canvas){
446
+
447
+ this(point.getX(),point.getY(),Canvas);
448
+
449
+ }
450
+
451
+ public void showAndStart(){
452
+
453
+ ball.show();
454
+
455
+ start();
456
+
457
+ }
458
+
459
+ public void moveTo(Location p){
460
+
461
+ ball.moveTo(p);
462
+
463
+ }
464
+
465
+ public void hide(){
466
+
467
+ ball.hide();
468
+
469
+ }
470
+
471
+ @Override public void run(){
472
+
473
+
474
+
475
+ while(canvas.getHeight()>=ball.getY()){
476
+
477
+ ball.move(move_x,move_y);
478
+
479
+ pause(pauseTime);
480
+
481
+ if(margin>ball.getX()||(canvas.getWidth()-2*margin)<ball.getX()){
482
+
483
+ move_x=-move_x;
484
+
485
+ } else if(4*margin>ball.getY()){
486
+
487
+ move_y=-move_y;
488
+
489
+ }
490
+
491
+
492
+
493
+ }
494
+
495
+ ball.removeFromCanvas();
496
+
497
+ }
498
+
499
+
500
+
501
+ public boolean contains(Location p){
502
+
503
+ return ball.contains(p);
504
+
505
+ }
506
+
507
+ public void setColor(Color color){
508
+
509
+ ball.setColor(color);
510
+
511
+ }
512
+
513
+ public double getX(){
514
+
515
+ return ball.getX();
516
+
517
+ }
518
+
519
+ public double getY(){
520
+
521
+ return ball.getY();
522
+
523
+ }
524
+
525
+ public void turnMoveX(){
526
+
527
+ move_x=-move_x;
528
+
529
+ }
530
+
531
+ public void turnMoveY(){
532
+
533
+ move_y=-move_y;
534
+
535
+ }
536
+
537
+ public int getSize(){
538
+
539
+ return size;
540
+
541
+ }
542
+
543
+ }
544
+
545
+ ```
198
546
 
199
547
  ### 試したこと
200
548
 
@@ -208,4 +556,4 @@
208
556
 
209
557
 
210
558
 
211
- ここにより詳細な情報を記載してください。
559
+ java:ビルド1.8.0_211-b12