• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using KeyListener in an applet  RSS feed

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm trying to set up a key listener in class that extends JPanel.
I have 2 classes, the first extends JApplet, see below:


and then there is the PacmanGame.java class which extends JPanel:



I'm really stuck when it comes to adding the keyListener,
I can do it with MouseListener and MouseMotionListener and ActionListener,
by coding the following into the Constructor.
addMouseMotionListener(this);
addMouseListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this); for example.

But I can't seem to do it with keyListener, because apparently it
works differently.
Please can someone show me how to accomplish this. Thanks very much for any help.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I just tried yur code but it didnt work,
I'm sure you need to use addKeyListener(...) somehow.
But I don't know how
Thanks anyway
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
run it, press some keys
uncomment the line, run again

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I don't think I can quite reproduce what you have done.
Remember I'm using a JApplet that uses the setContentPane method.



Also, my other class that extends JPanel has no main method.
Originally my pacman game was an application, and it was all done using a JFrame, and the following code

but now I have no main method or a JFrame, I want my output to go inside a
window in an HTML page.
My MouseListener and MouseMotionListener and ActionListener work fine, but
I don't know how to add the KeyListener.

Thanks for your help, I'll try what yuve done, and maybe I'll get there eventually.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem is you can't get the keyListener to work, on the JPanel?

Whether it is an applet, or a stand-alone, is irrelevant - the JPanel's
focusable flag must be set to true (default is false)
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks, I'll have a go, but after a while of messing about I tend to get annoyed.
Another thing is I can't seem to use extra classes to help simplify my code, it works when I call the JApplet extended class. But when I call an HTML file that that then calls the JApplet extended class, the other classes dont seem to work, see my other Topic in Applets (Problem using JApplets with normal Java classes ) for more info there.
Basically, I can only seem to use 2 classes, the JApplet extended class,
and the JPanel extended class. Which makes things more awquard.

I'll have a go at fixing code, but if you figure out how to ammend the JPanel extended class in the first message of this code, that would be great, but I'll try your setFocusable method too.
Thanks
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I can't manage to do it
I'm going to have a look at some applets on the sun site, and
try and figure it out.

Thanks for your help
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's the applet version (the key pressed will appear in the status bar)

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, but I've just tried your code, and it doesnt work, I chaged the
showStatus line to a println and it still doesnt work.
Also, I need to use the code:

and I'm not sure if this works with

And besides I need the keyPressed and keyReleased methods in the
PacmanGame class really since thats where all the processing and moving
of sprites takes place.

Thanks anyway
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!