Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Applet wont respond to keyboard input

 
Harry Singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The applet below responds to mouse but not to keyboard. What is the problem ?
Thanks in advance

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class PacMan1 extends Applet {
private int x_cd, y_cd;
public void init() {
x_cd = 10;
y_cd = 20;
setEnabled(true);
setForeground(Color.yellow);
setBackground(Color.lightGray);
addMouseListener( new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
System.out.println( "Inside the mouse Clicked" );
if (evt.getID() == MouseEvent.MOUSE_CLICKED) {
x_cd = evt.getX();
y_cd = evt.getY();
}
repaint();
}
});

addKeyListener( new KeyAdapter() {
public void KeyPressed(KeyEvent evt) {
char keyPressed;
System.out.println ( "Inside the process key event" );
keyPressed = evt.getKeyChar();
if (evt.getID() == KeyEvent.KEY_TYPED) {
switch (keyPressed) {
case 'R':
x_cd++;
break;
case 'L':
x_cd--;
break;
case 'T':
y_cd++;
break;
case 'B':
y_cd--;
break;
}
// End swtich
repaint();
}
}
});
}

public void paint(Graphics g) {
System.out.println( "Inside paint" );
g.fillArc(x_cd, y_cd, 50, 50, 45, 300);
}
/*public void update(Graphics g) {
System.out.println( "Inside update" );
paint(g);
}*/
}
 
Noah Carroll
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harry,
Neat little applet. It was fun to play around with. You only had a few things wrong with your program. The method you should have used for the keyListener is keyTyped(KeyEvent e). You tried to use KeyPressed(KeyEvent e), which is wrong because of the capital K. I tried with keyTyped() and keyPressed(). keyPressed() did nothing at all. If you go back and change that it will work, but it still has logic errors. You used the chars T and B. I assume that means for top and bottom. You need to remember that the positive y-axis runs from top to bottom. So to make it move towards the bottom, you need to add not subtract, and vice versa to move up. Also you had capital letters. in order to make that work you have to put your keyboard on caps lock. This is kind of bothersome, so i would recommend using lowercase letters. below is the code that i ended up with for addKeyListener()

addKeyListener( new KeyAdapter() {
public void keyTyped(KeyEvent evt) {
char keyPressed;
System.out.println("Inside the process key event");
keyPressed = evt.getKeyChar();
if (evt.getID() == KeyEvent.KEY_TYPED) {
switch (keyPressed) {
case 'r':
x_cd++;
break;

case 'l':
x_cd--;
break;

case 't':
y_cd--;
break;

case 'b':
y_cd++;
break;
}
// End swtich
repaint();
}
}
});

I hope it helps
Noah Carroll noahcaroll@juno.com
[This message has been edited by Noah Carroll (edited September 22, 2000).]
 
Harry Singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a LOT, Noah. I would have never figured the incorrect case out. Another frustrating point I found was that applet viewer just wont take key strokes. I usually test my code just with applet viewer. Now I'll be more careful.
 
Noah Carroll
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harry,
You are welcome for the help, I spent quite a while trying to figure it out myself. I only used applet viewer to solve the problem. The keystrokes seemed to work fine for me. good luck with the final project!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic