I new to applet programming. What iam trying to do is when ever a key pressed on key board, it has to display that on the applet. Here is my code.
public class sample extends Applet implements KeyListener {
private Graphics graphic;
@Override
public void init(){
addKeyListener(this);
}
@Override
public void paint(Graphics g){
graphic=g;
g.drawString("hello",20,30);
}
public void keyTyped(KeyEvent e) {
char key=e.getKeyChar();
dis(key,graphic);
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
private void dis(char key, Graphics graphic) {
graphic.drawString(" "+key,50,60);
}
}
But it is not displaying anything on key press. Whats wrong with my code..???
Please help me to find out it!
Do not save the
Graphicsobject. Try to call therepaint()method from within the handlers.