Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2004

Tip: Looking for answers? Try searching our database.

KeyListener kinda deaf (in simple game)

Thread view: 
nammtar667@hotmail.com - 22 Apr 2004 00:15 GMT
Hey everyone...

I'm trying to write a simple word-guessing game (Lingo, user should
guess 5 letter words etc).
I've created some custom AWT Components (sorry for the dutch naming):
Lettervak: extends Canvas
Woordveld: extends Container and contains Lettervak's
Speelveld: extends Container and contains Woordveld's

The Speelveld (playing field in english) is the main component I want
to talk to from the applet itself.
I don't care where a key is typed, I just want to be able to send the
message to the correct component so the letter can be drawn in a
Lettervak. So I though I'd add a KeyListener to the Applet itself.
It registers (I can see in the debugger), but it doesn't receive any
key events (I can tell by adding a breakpoint there, so it's not in
code further down).
Can anyone tell me what I'm doing wrong? Below is the code of the
Applet itself:
(may look a bit messy, because I've been fooling around to get it to
work, sorry...)
---
package lingo;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class LingoWeb
extends Applet {
private boolean isStandalone = false;

// Components
Speelveld grid;

//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}

//Construct the applet
public LingoWeb() {
this.addKeyListener( new SpeelveldListener() );
}

//Initialize the applet
public void init() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}

//Component initialization
private void jbInit() throws Exception {
Speelveld grid = new Speelveld( 10, 20, 5, 5, 40 );

this.setFocusable( true );
this.requestFocusInWindow();
this.setLayout(null);
this.add(grid);
this.repaint();
}

//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}

//Get parameter info
public String[][] getParameterInfo() {
return null;
}

class SpeelveldListener implements KeyListener {
public void keyTyped( KeyEvent e ) {
char c = e.getKeyChar();
grid.invoer(c);
}

public void keyPressed( KeyEvent e ) {
System.out.println( e.getKeyChar() );
}
public void keyReleased( KeyEvent e ) {}
}
}
---

Thanks!
Tim.
Fahd Shariff - 27 Apr 2004 08:53 GMT
I think you havent initialised "grid".

The line:
char c = e.getKeyChar() ;
works fine i.e. you are able to capture the key that the user has
typed.

But the line:
grid.invoer(c);
does not work because I do not think grid has been initialised. This
is because in jbInit() you are declaring a completely new variable
grid and hence the grid on line 12 never gets initialised (if you know
what i mean).

I suggest you try fixing jbInit() as follows:

//Component initialization
private void jbInit() throws Exception {
//before: Speedveld grid = new Speelveld( 10, 20, 5, 5, 40 );
grid = new Speelveld( 10, 20, 5, 5, 40 );
...

Hope this works!

Fahd

> Hey everyone...
>
[quoted text clipped - 89 lines]
> Thanks!
> Tim.
nammtar667@hotmail.com - 28 Apr 2004 22:46 GMT
Hello Fahd,

Yes, you're absolutely right, haha, stupid mistake on my 'count...
changed that immediately.
However, it doesn't help getting the keyListener to work.
In the meantime I've been trying all kinds of other things, but to no
avail.
Do you happen to have any other suggestions?
If it helps to see all the code, I'd be happy to post it.

Thanks!
Tim.

>I think you havent initialised "grid".
>
[quoted text clipped - 115 lines]
>> Thanks!
>> Tim.
Fahd Shariff - 29 Apr 2004 08:45 GMT
I can't see what the problem is. I tried running it and it seems to
print out the char that was typed. This means that the listener is
working fine. So i assume "grid.invoer(c)" is not doing what u expect
it to?

PS: Code would be useful :)

> Hello Fahd,
>
[quoted text clipped - 34 lines]
> >
> >Fahd
nammtar667@hotmail.com - 29 Apr 2004 19:27 GMT
Yeah, me neither, I've gone over it again and again and I've tried
lots of stuff and other things I saw on the web, to no avail...
What version of Java are you using? And what IDE, if any?

Anyway, I've attached the code in a zip-file, it's all there.
If you can find anything, I'd be very grateful!

Thanks in advance!
Tim.

>I can't see what the problem is. I tried running it and it seems to
>print out the char that was typed. This means that the listener is
[quoted text clipped - 41 lines]
>> >
>> >Fahd


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.