java - Why can't i use keyPressed anymore once the if-statement is true in the run()-methode? -


i'm trying make kind of brickbreaker game once press start button ("starten" = true). can't use keypressed()-utility anymore before pressed start button use keypressed()-utility. please tell me why can't use keypressed()-utility anymore , give me possible solution too?

import java.util.*; import java.awt.*; import java.awt.event.*;  import javax.swing.*;  public class vgkernel extends jpanel implements keylistener {  public rectangle screen, ball, block; // screen area , ball location/size. public rectangle bounds;  // boundaries of drawing area. public jframe frame; // jframe put graphics into. public vgtimertask vgtask; // timertask runs game. public boolean down, right, starten = false; // direction of ball's travel. public jbutton start;  public vgkernel(){     super();     screen = new rectangle(0, 0, 600, 400);     ball   = new rectangle(0, 0, 20, 20);     block = new rectangle(260, 350, 40, 10);     bounds = new rectangle(0, 0, 600, 400); // give starter values.     frame = new jframe("vgkernel");     vgtask = new vgtimertask();     addkeylistener(this);     setfocusable(true);     start = new jbutton("start");     start.addactionlistener(new actionlistener() {         @override         public void actionperformed(actionevent e) {             starten = true;         }     });     add(start); }  class vgtimertask extends timertask{     public void run(){         repaint();          if(starten){         moveball();         frame.repaint();         }     }   }  // instance methods:   public void paintcomponent(graphics g){     // drawing area bounds game logic.     bounds = g.getclipbounds();     // clear drawing area, draw ball.     g.clearrect(screen.x, screen.y, screen.width, screen.height);     g.fillrect(ball.x, ball.y, ball.width, ball.height);     g.fillrect(block.x, block.y, block.width, block.height);   }    public void moveball(){   // ball should own class method.     if (right) ball.x+=1; // if right true, move ball right,     else ball.x-=1;       // otherwise move left.     if (down)  ball.y+=1; // same up/down.     else ball.y-=1;     if (ball.x > (bounds.width - ball.width)) // detect edges , bounce.       { right = false; ball.x = bounds.width -  ball.width; }     if (ball.y > (bounds.height - ball.height))       { down  = false; ball.y = bounds.height - ball.height;}     if (ball.x == 0) { right = true; ball.x = 0; }     if (ball.y == 0) { down  = true; ball.y = 0; }   }    public void keypressed(keyevent evt) {       if(evt.getkeycode() == keyevent.vk_g && block.x > 0) {           block.x -= 20;       }        if(evt.getkeycode() == keyevent.vk_h && block.x < 540) {           block.x += 20;       }   }    public void keytyped(keyevent evt){  }   public void keyreleased(keyevent evt){ }    public void startactionperformed(java.awt.event.actionevent evt) {       starten = true;   }    public static void main(string arg[]){     java.util.timer vgtimer = new java.util.timer();  // create timer.     vgkernel panel = new vgkernel(); // create , instance of our kernel.      // set intial ball movement direction.     panel.down = true;     panel.right = true;      // set our jframe     panel.frame.setdefaultcloseoperation(jframe.exit_on_close);     panel.frame.setsize(panel.screen.width, panel.screen.height);     panel.frame.setcontentpane(panel);      panel.frame.setvisible(true);      // set timer vgtask regularly.     vgtimer.schedule(panel.vgtask, 0, 10);   } 

}

you need make sure whatever component you're listening keyevents on has focus.

once click button, button has focus instead of jpanel. more info here: http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html

you can use requestfocusinwindow() method on jpanel, or can call setfocusable(false) on jbutton.

or use key bindings instead: http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -