Problem with KeyListener

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2009
Posts: 3
Reputation: Ballen92 is an unknown quantity at this point 
Solved Threads: 0
Ballen92 Ballen92 is offline Offline
Newbie Poster

Problem with KeyListener

 
0
  #1
32 Days Ago
Hello all. I have a problem that seems like it should be relatively simple to fix but I have been scouring the internet and can't find an answer =(
I am trying to add a keylistener to this code, but it doesn't recognize the method addKeyListener(this); I am working with kareltherobot so that's where all of the methods like pickBeeper() and facingNorth() etc. are from. Any help would be appreciated.

  1. import java.awt.event.*;
  2. import kareltherobot.*;
  3.  
  4. public class PacMan extends Robot implements KeyListener, Directions
  5. {
  6. public PacMan(int street, int avenue, Direction direction, int beepers)
  7. {
  8. super(street, avenue, direction, beepers);
  9. addKeyListener(this); //this is the part that doesn't work
  10. }
  11.  
  12. /**Method definitions for the KeyListener interface (there aren't any errors in this part)
  13. **/
  14. public void keyTyped(KeyEvent evt) {
  15. char key = evt.getKeyChar();
  16. if (key == 'w') {
  17. while(!facingNorth())
  18. turnLeft();
  19. if(frontIsClear())
  20. move();
  21. while(nextToABeeper())
  22. pickBeeper();
  23. }
  24. if (key == 'a') {
  25. while(!facingWest())
  26. turnLeft();
  27. while(nextToABeeper())
  28. pickBeeper();
  29. }
  30. if (key == 's') {
  31. while(!facingSouth())
  32. turnLeft();
  33. move();
  34. while(nextToABeeper())
  35. pickBeeper();
  36. }
  37. if (key == 'd') {
  38. while(!facingEast())
  39. turnLeft();
  40. move();
  41. while(nextToABeeper())
  42. pickBeeper();
  43. }
  44. }
  45.  
  46. public void keyPressed(KeyEvent evt) {
  47. char key = evt.getKeyChar();
  48. if (key == 'w') {
  49. while(!facingNorth())
  50. turnLeft();
  51. if(frontIsClear())
  52. move();
  53. while(nextToABeeper())
  54. pickBeeper();
  55. }
  56. if (key == 'a') {
  57. while(!facingWest())
  58. turnLeft();
  59. while(nextToABeeper())
  60. pickBeeper();
  61. }
  62. if (key == 's') {
  63. while(!facingSouth())
  64. turnLeft();
  65. move();
  66. while(nextToABeeper())
  67. pickBeeper();
  68. }
  69. if (key == 'd') {
  70. while(!facingEast())
  71. turnLeft();
  72. move();
  73. while(nextToABeeper())
  74. pickBeeper();
  75. }
  76. }
  77.  
  78. public void keyReleased(KeyEvent evt) {
  79. char key = evt.getKeyChar();
  80. if (key == 'w') {
  81. while(!facingNorth())
  82. turnLeft();
  83. if(frontIsClear())
  84. move();
  85. while(nextToABeeper())
  86. pickBeeper();
  87. }
  88. if (key == 'a') {
  89. while(!facingWest())
  90. turnLeft();
  91. while(nextToABeeper())
  92. pickBeeper();
  93. }
  94. if (key == 's') {
  95. while(!facingSouth())
  96. turnLeft();
  97. move();
  98. while(nextToABeeper())
  99. pickBeeper();
  100. }
  101. if (key == 'd') {
  102. while(!facingEast())
  103. turnLeft();
  104. move();
  105. while(nextToABeeper())
  106. pickBeeper();
  107. }
  108. }
  109. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 798
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster
 
0
  #2
32 Days Ago
Hi Ballen92 and welcome to DaniWeb

What's the error message you get? If I had to guess on the information you've given us, I would say that the method addKeyListener is not defined for your class. What class does Robot extend?
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 3
Reputation: Ballen92 is an unknown quantity at this point 
Solved Threads: 0
Ballen92 Ballen92 is offline Offline
Newbie Poster
 
0
  #3
31 Days Ago
The error I get is:

PacMan.java:10: cannot find symbol
symbol : method addKeyListener(PacMan)
location: class PacMan
addKeyListener(this);

As for the Robot class, it does not extend anything, but it is part of a package called karel. I think you are right in that the method addKeyListener is not defined in my class, and it is definitely not defined in the Robot class, but I do not know how or where to define the method. I notice that many people who used KeyListeners often extend classes like JFrame, JPanel, or JApplet. Unfortunately in this case, I am restricted to extend the class Robot.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 982
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark
 
0
  #4
31 Days Ago
addKeyListener is only implemented for user interface components, so sooner or later you will need to use a JFrame or whatever. How is your pacman displayed?
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 3
Reputation: Ballen92 is an unknown quantity at this point 
Solved Threads: 0
Ballen92 Ballen92 is offline Offline
Newbie Poster
 
0
  #5
31 Days Ago
Well unfortunately this is where I am severely limited in using this kareltherobot package. It is a beginners guide for java programming and basically it creates the user interface for me, I don't deal with the actual graphics coding. I suppose I could tinker with the karel package that is given to me and see if I can add a keylistener somewhere in there. So, just to clarify, I can only add a keylistener to something that is graphically represented, like in the user interface? Also, if I extend JFrame or one of those classes, then should the compiler recognize the method addKeyListener?
Thanks for all the help!
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC