前提・実現したいこと
ブロック崩しが作りたく、このコードを描いた。
keyEventを読み込んで、MyKeyAdapter 内のメソッドを動かしたい。
発生している問題・エラーメッセージ
この中で、ボールを打ち返す Bar クラスにおいてBarを左右キー入力で動かすつもりでコードを描いたがキーを押しても全く反応しない。
該当のソースコード
ブロック崩しのコントローラーとしてのクラス
java
1import objectdraw.*; 2import java.awt.*; 3import java.util.*; 4import java.awt.event.KeyAdapter; 5import java.awt.event.KeyEvent; 6public class BlockBreaks extends WindowController { 7 private final int margin=5; 8 public static boolean game_start=false; 9 private MoveBall ball; 10 public final int size=80; 11 private Bar bar; 12 MyKeyAdapter myKeyAdapter; 13 14 @Override public void begin(){ 15 ball=new MoveBall(0,0,canvas); 16 ball.hide(); 17 ball.setColor(Color.RED); 18 bar=new Bar(150,300,size,canvas,ball); 19 createBlocks(); 20 createRect(); 21 myKeyAdapter = new MyKeyAdapter(); 22 addKeyListener(myKeyAdapter); 23 } 24 25 @Override public void onMouseClick(Location p){ 26 if(!game_start){ 27 ball.moveTo(p); 28 ball.showAndStart(); 29 game_start=true; 30 } 31 } 32 33 public void createBlocks(){ 34 double Block_WIDTH=(canvas.getWidth()-2*margin)/12, 35 Block_HEIGHT=10, 36 WALL_X=margin, 37 WALL_HEIGHT=4, 38 WALL_WIDTH=12, 39 WALL_Y=4*margin+Block_HEIGHT*WALL_HEIGHT; 40 double BlockX = WALL_X; 41 double BlockY = WALL_Y; 42 int level = 0; // Number of levels drawn so far 43 int BlocksInLevel = 0; // Number of rects drawn in 44 // the current level 45 while ( level < WALL_HEIGHT) { 46 BlocksInLevel = 0; 47 BlockX = WALL_X; 48 // Draw one row of rects 49 while ( BlocksInLevel < WALL_WIDTH ) { 50 new Block( BlockX, BlockY, Block_WIDTH,Block_HEIGHT, canvas ,ball); 51 BlockX = BlockX + Block_WIDTH; 52 BlocksInLevel++; 53 54 } 55 BlockY = BlockY - Block_HEIGHT; 56 level++; 57 } 58 59 } 60 61 public void createRect(){ 62 new Line(margin,4*margin,margin,canvas.getHeight()-margin,canvas); 63 64 new Line(canvas.getWidth()-margin,canvas.getHeight()-margin,canvas.getWidth()-margin,4*margin,canvas); 65 new Line(canvas.getWidth()-margin,4*margin,margin,4*margin,canvas); 66 } 67 private class MyKeyAdapter extends KeyAdapter{ 68 69 @Override public void keyPressed(KeyEvent e){ 70 int keycode=e.getKeyCode(); 71 System.out.println(keycode); 72 switch(keycode){ 73 case KeyEvent.VK_RIGHT: 74 Bar.ri_key=true; 75 break; 76 case KeyEvent.VK_LEFT: 77 Bar.lef_key=true; 78 break; 79 } 80 } 81 82 @Override public void keyReleased(KeyEvent e){ 83 Bar.ri_key=false; 84 Bar.lef_key=false; 85 } 86 } 87} 88class Block extends ActiveObject{ 89 DrawingCanvas canvas; 90 FilledRect rect; 91 FramedRect Rect; 92 MoveBall ball; 93 final int pause_time=6; 94 private boolean removed=false; 95 96 public Block(double x,double y,double width,double height,DrawingCanvas Canvas,MoveBall b){ 97 canvas=Canvas; 98 ball=b; 99 rect=new FilledRect(x,y,width,height,canvas); 100 Rect=new FramedRect(x,y,width,height,canvas); 101 Rect.setColor(Color.YELLOW); 102 start(); 103 104 } 105 106 @Override public void run(){ 107 Location up_ri,up_lef,dow_ri,dow_lef; 108 109 while(!removed){ 110 up_ri=new Location(ball.getX(),ball.getY()); 111 up_lef=new Location(ball.getX()+ball.getSize(),ball.getY()); 112 dow_ri=new Location(ball.getX(),ball.getY()+ball.getSize()); 113 dow_lef=new Location(ball.getX()+ball.getSize(),ball.getY()+ball.getSize()); 114 if(rect.contains(up_ri)&&rect.contains(up_lef)||rect.contains(dow_ri)&&rect.contains(dow_lef)){ 115 ball.turnMoveY(); 116 removeFromCanvas(); 117 removed=true; 118 }else if(rect.contains(up_ri)&&rect.contains(dow_ri)||rect.contains(up_lef)&&rect.contains(dow_lef)){ 119 ball.turnMoveX(); 120 removeFromCanvas(); 121 removed=true; 122 }else if (rect.contains(up_ri)||rect.contains(up_lef)||rect.contains(dow_ri)||rect.contains(dow_lef)){ 123 ball.turnMoveY(); 124 ball.turnMoveX(); 125 removeFromCanvas(); 126 removed=true; 127 } 128 pause(pause_time); 129 } 130 131 132 } 133 public void removeFromCanvas(){ 134 rect.removeFromCanvas(); 135 Rect.removeFromCanvas(); 136 } 137 138}
ボールを打ち返すための Bar クラス
java
1import java.applet.Applet; 2import objectdraw.*; 3import java.awt.*; 4import java.awt.event.KeyListener; 5import java.awt.event.KeyEvent; 6public class Bar extends ActiveObject { 7 private DrawingCanvas canvas; 8 private FilledRect bar; 9 MoveBall ball; 10 public static boolean lef_key=false,ri_key=false,gamestart=false; 11 private final int pause_time=6,speed=3,bar_height=5; 12 13 public Bar(double x,double y,int length,DrawingCanvas Canvas,MoveBall Ball){ 14 canvas=Canvas; 15 ball=Ball; 16 gamestart=true; 17 bar=new FilledRect(x,y,length,bar_height,canvas); 18 start(); 19 } 20 21 public Bar(Location point,int length,DrawingCanvas Canvas,MoveBall Ball){ 22 this(point.getX(),point.getY(),length,Canvas,Ball); 23 } 24 @Override public void run(){ 25 Location up_ri,up_lef,dow_ri,dow_lef; 26 while(gamestart){ 27 if(lef_key){ 28 bar.move(-speed,0); 29 } else if(ri_key){ 30 bar.move(speed,0); 31 } 32 up_ri=new Location(ball.getX(),ball.getY()); 33 up_lef=new Location(ball.getX()+ball.getSize(),ball.getY()); 34 dow_ri=new Location(ball.getX(),ball.getY()+ball.getSize()); 35 dow_lef=new Location(ball.getX()+ball.getSize(),ball.getY()+ball.getSize()); 36 if(bar.contains(up_ri)&&bar.contains(up_lef)||bar.contains(dow_ri)&&bar.contains(dow_lef)){ 37 ball.turnMoveY(); 38 }else if(bar.contains(up_ri)&&bar.contains(dow_ri)||bar.contains(up_lef)&&bar.contains(dow_lef)){ 39 ball.turnMoveX(); 40 }else if (bar.contains(up_ri)||bar.contains(up_lef)||bar.contains(dow_ri)||bar.contains(dow_lef)){ 41 ball.turnMoveY(); 42 ball.turnMoveX(); 43 } 44 pause(pause_time); 45 } 46 } 47 public void move(double dx,double dy){ 48 bar.move(dx,dy); 49 } 50}
ボールのクラス
java
1import objectdraw.*; 2import java.awt.*; 3 4public class MoveBall extends ActiveObject{ 5 private FilledOval ball; 6 public final int pauseTime=33, 7 margin=5; 8 private int size=5, 9 ball_speed=8; 10 DrawingCanvas canvas; 11 RandomIntGenerator generator1=new RandomIntGenerator(-5,5); 12 RandomIntGenerator generator2=new RandomIntGenerator(-3,-5); 13 private int move_x=generator1.nextValue(); 14 private int move_y=generator2.nextValue(); 15 public MoveBall(double x,double y,DrawingCanvas Canvas){ 16 canvas=Canvas; 17 ball=new FilledOval(x,y,size,size,Canvas); 18 19 } 20 public MoveBall(Location point,DrawingCanvas Canvas){ 21 this(point.getX(),point.getY(),Canvas); 22 } 23 public void showAndStart(){ 24 ball.show(); 25 start(); 26 } 27 public void moveTo(Location p){ 28 ball.moveTo(p); 29 } 30 public void hide(){ 31 ball.hide(); 32 } 33 @Override public void run(){ 34 35 while(canvas.getHeight()>=ball.getY()){ 36 ball.move(move_x,move_y); 37 pause(pauseTime); 38 if(margin>ball.getX()||(canvas.getWidth()-2*margin)<ball.getX()){ 39 move_x=-move_x; 40 } else if(4*margin>ball.getY()){ 41 move_y=-move_y; 42 } 43 44 } 45 ball.removeFromCanvas(); 46 } 47 48 public boolean contains(Location p){ 49 return ball.contains(p); 50 } 51 public void setColor(Color color){ 52 ball.setColor(color); 53 } 54 public double getX(){ 55 return ball.getX(); 56 } 57 public double getY(){ 58 return ball.getY(); 59 } 60 public void turnMoveX(){ 61 move_x=-move_x; 62 } 63 public void turnMoveY(){ 64 move_y=-move_y; 65 } 66 public int getSize(){ 67 return size; 68 } 69}
MyKeyAdapter クラスの中で Bar クラスの 変数 lef_key か ri_key の真偽を書き換えるようにしてるのですが、反応がないのでおそらく、MyKeyAdapter がうまく動いていなのだと思います。
どのように書き換えるべきか、ググったのですがわかりませんでした。どう書き換えべきでしょうか?
試したこと
MyKeyAdapter 内に System.out.printle(keycode) をいれて試したがキーを押しても反応がなかった。
補足情報(FW/ツールのバージョンなど)
java:ビルド1.8.0_211-b12
回答1件
あなたの回答
tips
プレビュー