Results 1 to 5 of 5
Thread: A problem with KeyListener
- 07-22-2011, 12:13 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 53
- Rep Power
- 0
A problem with KeyListener
Hello everyone!
Well I did a lil' bit of Java on university but now I wanted to program an applet Pong clone.
Problem is that I can't seem to get the keyListener listening to my input...
So: How do I get my keylistener to work?
EDIT: I noticed it might not be necessary to get a runnable aplet for a pong clone, but I still like to know why this program wasn't workingJava Code:import java.applet.*; import java.awt.*; import java.awt.event.*; public class main extends Applet implements Runnable,KeyListener { /** * */ private static final long serialVersionUID = 2305235281454935668L; /** * @param args */ int y; boolean pause; Graphics offscr; Thread runner; int Score[] = new int[2]; static int height,width; Image offscreenImage; public void init() { pause=false; this.setSize(650,400); runner = new Thread(this); width = getWidth(); height = getHeight(); y = height/2; offscreenImage = createImage(width, height); offscr = offscreenImage.getGraphics(); addKeyListener(this); runner.start(); } public void paint(Graphics g) { offscr.setColor(Color.black); offscr.fillRect(0,0,width,height); offscr.setColor(Color.white); offscr.fillRect(20,y-50,20,60); g.drawImage(offscreenImage, 0, 0, this); } public void update(Graphics g) { paint(g); } @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent e) { int keyCode = e.getKeyCode(); switch( keyCode ) { case KeyEvent.VK_UP: y++; System.out.println("up"); break; case KeyEvent.VK_DOWN: y++; System.out.println("up"); break; case KeyEvent.VK_SPACE: if (pause) { pause=false; } else { pause=true; } break; case KeyEvent.VK_ESCAPE: // handle right break; } } @Override public void run() { while (true) { repaint(); y++; try { Thread.sleep (20); } catch (InterruptedException ex){} } } }
Thanks in advance,
ReskaillevLast edited by Reskaillev; 07-22-2011 at 12:21 AM.
- 07-22-2011, 12:43 AM #2
Does the component with the listener have the focus?
I added a println:
When I click on the window then press a key it printsJava Code:public void keyTyped(KeyEvent e) { System.out.println("kT e=" + e); //<<<< Show when calledLast edited by Norm; 07-22-2011 at 12:49 AM.
- 07-22-2011, 01:01 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
You need to be more precise about "wasn't working". Is keyTyped() ever called? Add some debugging code (and remember to click in the applet window to give it focus)I noticed it might not be necessary to get a runnable aplet for a pong clone, but I still like to know why this program wasn't working
Observe the behaviour for each of the keys mentioned in that method. Read the KeyEvent API docs to see what events the keyTyped() method will find out about.Java Code:@Override public void keyTyped(KeyEvent e) { [color=green]System.out.println("keyTyped: " + e);[/color] int keyCode = e.getKeyCode(); // etc
-----
You should observe standard Java coding conventions and give your class a descriptive name starting with an uppercase letter eg call it PongApplet rather than main.
Consider the more modern JApplet class and Swing technologies like timers etc.
[Edit] Slow... ;(
- 07-22-2011, 01:07 AM #4
Member
- Join Date
- Jul 2011
- Posts
- 53
- Rep Power
- 0
Thanks for answering! both of you! Its much appreciated
Well I tried the extra code (the console output) and it seems my arrowkeys (VK.UP and VK.DOWN and so on) don't respond to the keylistner...
When typing any other key it does respond properly ...
EDIT: to rule it out: yes my arrowkeys do work :pLast edited by Reskaillev; 07-22-2011 at 01:10 AM.
- 07-22-2011, 02:29 AM #5
Similar Threads
-
KeyListener Graphics Problem
By Makesfolkslose in forum New To JavaReplies: 2Last Post: 05-26-2011, 04:07 AM -
Problem with Keylistener, some help pls
By syon in forum AWT / SwingReplies: 1Last Post: 01-21-2011, 01:31 AM -
AWT KeyListener Problem
By plm-pusik in forum New To JavaReplies: 15Last Post: 11-10-2010, 03:38 PM -
KeyListener problem
By siyi90 in forum AWT / SwingReplies: 7Last Post: 02-08-2010, 10:16 AM -
Problem with KeyListener in Jtable
By sandeepsai17 in forum New To JavaReplies: 0Last Post: 06-30-2009, 10:14 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks