Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - Java KeyListeners
  
User name:
Password:
Reply:
 

UserPost
Squash Monster
Posts: 496/677
That's an interesting solution, though I've already got a working one now. I'll remember your version though, it should be usefull for some other projects. Thanks.
HellSavior
Posts: 14/41
It would be more efficient to do something like this :

class Stepper extends JPanel implements KeyListener
{
...
public Stepper
{ setFocusable(true);
requestFocusInWindow();
addKeyListener(this);
}
public void keyPressed(KeyEvent e)
{ switch(e.getKeyCode())
{ case KeyEvent.VK_LEFT: sendTo.takeKey(1); break;
...
}
}
}

There are a few more methods you have to implement from KeyListener, but I cannot remember them off the top of my head.

EDIT : It does not seem that this board likes spaces. Messy code it is..
neotransotaku
Posts: 1928/4016
is your JPanel surrounded by any derivative Frame class? if so, the JPanel will never see any keys...
Squash Monster
Posts: 491/677
I managed to get the graphics to redraw in my game (switched to JPanel instead of Applet).
Here's a slightly out-dated screenshot (drag to adress bar or copy/paste)
http://squashmonster.tripod.com/Treasure/treasureGameSS2.png

Now I'm trying to read keyboard input to controll the character. I can't figure out what I'm doing wrong though.

Some excerpts from my code:
Stepper.java
class Stepper extends JPanel implements ActionListener
{
KeyboardIn keys = new KeyboardIn(this);

public Stepper()
{
addKeyListener(keys);
}

...



KeyboardIn.java

class KeyboardIn extends KeyAdapter
{ //Input from keyboard class for Treasure Game

Stepper sendTo; //Where to send events after processed

public KeyboardIn(Stepper listener)
{ //Take the Stepper we'll want to send things to.
sendTo = listener;
}

public void keyPressed(KeyEvent e)
{ //Process pressed keys
System.out.println("!");
switch (e.getKeyCode())
{ //Each key we care about has an associated binary flag. Add to it.
case KeyEvent.VK_LEFT: sendTo.takeKey(1); break;
case KeyEvent.VK_UP: sendTo.takeKey(2); break;
case KeyEvent.VK_RIGHT: sendTo.takeKey(4); break;
case KeyEvent.VK_DOWN: sendTo.takeKey(8); break;
case KeyEvent.VK_SPACE: sendTo.takeKey(16); break;
}
}

public void keyReleased(KeyEvent e)
{ //Process released keys
System.out.println("!");
switch (e.getKeyCode())
{ //Each key we care about has an associated binary flag. Subtract.
case KeyEvent.VK_LEFT: sendTo.takeKey(-1); break;
case KeyEvent.VK_UP: sendTo.takeKey(-2); break;
case KeyEvent.VK_RIGHT: sendTo.takeKey(-4); break;
case KeyEvent.VK_DOWN: sendTo.takeKey(-8); break;
case KeyEvent.VK_SPACE: sendTo.takeKey(-16); break;
}
}
}



You'll notice that my KeyboardIn class is set to print the System screen whenever anything happens. It never does though. How did I mess up? Am I using the wrong class?
Acmlm's Board - I2 Archive - Programming - Java KeyListeners


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.010 seconds.