java - No keyboard input working KeyAdapter -


i wrote application , wanted add keyboard input it. main class extends jpanel add keyadapter constructor. keyadapter new class called "inputadapter" extending keyadapter it's keypressed() , keyreleased() method. on click or release console should print string, e.g. here "test"

i don't know why, console won't print text. also, when tell turn sprites visibility false nothing happens well.

so guess keyadapter isn't working properly, take closer codelines?

i guess issue has nothing other implemented classes wrote because when removing them, issue non working keyboard input remains.

package com.ochs.game;

public class game extends jpanel implements runnable{ private static final long serialversionuid = 1l;  public static final int width = 320; public static final int height = 240; public static final int scale = 3;  public boolean isrunning;  public game() {     addkeylistener(new inputadapter());     setfocusable(true);     requestfocus();     start(); }  public void start() {     isrunning = true;     new thread(this).start(); }  public void stop() {     isrunning = false; }  public void run() {     init();     while(isrunning) {          update();         repaint();          try {             thread.sleep(5);         } catch (interruptedexception e) {             system.out.println("thread sleep failed.");         }     } }  public void init() {  }  public void update() {  }  public void paint(graphics g) {     super.paint(g);     graphics2d g2d = (graphics2d)g;  }  public static void main(string[] args) {     game gamecomponent = new game();     dimension size = new dimension(width*scale, height*scale);      jframe frame = new jframe("invaders");     frame.setvisible(true);     frame.setsize(size);     frame.setlocationrelativeto(null);     frame.setdefaultcloseoperation(jframe.exit_on_close);     frame.setresizable(false);     frame.add(gamecomponent); } public class inputadapter extends keyadapter {      @override     public void keypressed(keyevent arg0) {         system.out.println("test");     }      @override     public void keyreleased(keyevent arg0) {         system.out.println("test");     }   } }    

your code works me:

java version "1.6.0_27" openjdk runtime environment (icedtea6 1.12.6) (6b27-1.12.6-1ubuntu0.12.04.2) openjdk client vm (build 20.0-b12, mixed mode, sharing) 

tip 1 - should override paintcomponent(graphics g) guess, not paint()

public void paintcomponent(graphics g){      super.paintcomponent(g);     //... } 

tip 2 - use addnotify() on jpanel:

public void addnotify(){      super.addnotify();     //start here     new thread(this).start(); } 

tip 3 - launch app way, edt thread (see what swingutilities.invokelater do?)

swingutilities.invokelater(new runnable() {      public void run(){      //your code     } }); 

hope helps!


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -