🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.api
1 messages
1 total messages Started by nammtar667 Wed, 21 Apr 2004 23:14
KeyListener kinda deaf (for simple game)
#124
Author: nammtar667
Date: Wed, 21 Apr 2004 23:14
93 lines
3263 bytes
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.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads