KeyListener does not work...plz help
i have created an application based on a tutorial that will keep drawing rectangles until any key is pressed, then it will continue when i press again
the problem is that the "KeyListener" does not respond and the application keep drawing without stop
my code is:
publicclass simpleLoopextends JPanelimplements KeyListener, MouseListener{
Thread t;
Random rand =new Random();
boolean e =false;
/** Creates a new instance of simpleLoop */
public simpleLoop(){
setFocusable(true);
requestFocus();
addKeyListener(this);
addMouseListener(this);
t = creatThread();
t.start();
}
private Thread creatThread(){
returnnew Thread(new Runnable(){
publicvoid run(){
while(!e){
try{
Thread.sleep(20);
}catch (InterruptedException ex){
ex.printStackTrace();
}
repaint();
}
}
});
}
protectedvoid paintComponent(Graphics g){
Graphics2D g2d = (Graphics2D)g;
int x;
int y;
int hight,width;
int colorR,colorG,colorB;
//AffineTransform identity = new AffineTransform();
x = rand.nextInt(getWidth());
y = rand.nextInt(getHeight());
hight = rand.nextInt(getHeight()/5);
width = rand.nextInt(getWidth()/5);
Rectangle rec =new Rectangle(x,y,hight,width);
colorB = rand.nextInt(256);
colorG = rand.nextInt(256);
colorR = rand.nextInt(256);
g2d.setColor(new Color(colorR,colorG,colorB));
g2d.fill(rec);
}
publicstaticvoid main(String[] arg){
JFrame frame =new JFrame("simple loop");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(400,400);
frame.setFocusable(true);
frame.requestFocus();
simpleLoop sl =new simpleLoop();
frame.getContentPane().add(sl,BorderLayout.CENTER);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
}
publicvoid keyTyped(KeyEvent e){
System.out.println("pressed type");
}
publicvoid keyPressed(KeyEvent e){
System.out.println("pressed press");
if(this.e ==false){
this.e =true;
}else{
this.e =false;
t = creatThread();
}
}
publicvoid keyReleased(KeyEvent e){
System.out.println("pressed release");
}
publicvoid mouseClicked(MouseEvent e){
}
publicvoid mousePressed(MouseEvent e){
System.out.println("clicked");
repaint();
}
publicvoid mouseReleased(MouseEvent e){
}
publicvoid mouseEntered(MouseEvent e){
}
publicvoid mouseExited(MouseEvent e){
}
}
i am using netbeans and windows xp
NOTE:
i don't know why but the KeyListener does work fine with another application that i have also created it based on another tutorial
publicclass KeyListenerExampleextends JFrame{
DrawPanel dp =new DrawPanel();
/** Creates a new instance of DrawingExample */
public KeyListenerExample(){
super("Simple Drawing Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(dp,BorderLayout.CENTER);
setSize(400,300);
setVisible(true);
}
publicstaticvoid main(String [] args){
new KeyListenerExample();
}
}
class DrawPanelextends JPanel{
privateint keyCode;
privatechar keyChar;
public DrawPanel(){
setFocusable(true);
requestFocus();
addKeyListener(new KeyListener(){
publicvoid keyPressed(KeyEvent e){
keyCode = e.getKeyCode();
keyChar =' ';
repaint();
}
publicvoid keyReleased(KeyEvent e){
}
publicvoid keyTyped(KeyEvent e){
keyChar = e.getKeyChar();
repaint();
}
});
}
protectedvoid paintComponent(Graphics g){
super.paintComponent(g);
g.drawString("press a key...",20,20);
g.drawString("Key Code: "+keyCode,20,50);
g.drawString("Key Char:"+keyChar,20,70);
}
}
plz help....

