Reputation: 11
my big problem is that i am codding a game.i have 2 player in a jframe that they can play cuncurrent. first player play with arrow keys and second with w/a/s/d keys..my instrutor said me that to achieve cuncurrent play i should instantiate two thread that every thread manage its own special player..noe i am confuse that how i can have two thread for tow players that only diffrence between them is the key that they listen...
another question that maybe can help me is can i have a listener that listen only some special keys??for exampe i new a listener that only listen w/s/a/d buttuns??(i am familiar with keyevent.getkeycode but it is not my mean because i want a listener that never listen another keys this is listen all keys and in decision choose codder favorite key)
i will be infinitively greatfull if you help me.
Upvotes: 1
Views: 1007
Reputation: 1070
Ok so heres how i would do it, i would have 2 classes, one called game and the other player, game extends your JFrame and player implements your keyListener, In game you could have 2 instances of Player, one for the instance that uses the D-keys and the other would use asdw or whatever, heres an example:
public void keyPressed(KeyEvent e)
{
switch(playerNumber)
{
case PLAYER_1:
if(e.getKeyCode==VK_UP)
{
//do some code for an "up event"
}
if(e.getKeyCode==VK_DOWN)
{
//do some code for a "down event"
}
if(e.getKeyCode==VK_LEFT)
{
//do some code for a "left event"
}
if(e.getKeyCode==VK_RIGHT)
{
//do some code for a "right event"
}
break;
case PLAYER_2:
if(e.getKeyCode==VK_W)
{
//do some code for an "up event"
}
if(e.getKeyCode==VK_S)
{
//do some code for a "down event"
}
if(e.getKeyCode==VK_A)
{
//do some code for a "left event"
}
if(e.getKeyCode==VK_D)
{
//do some code for a "right event"
}
break;
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public static final int PLAYER_1 = 0;
public static final int PLAYER_2 = 1;
all you would have to do is tell the player instance if its player1 or player2, which would be easy, if you need any more examples or anything let me know, ive made plenty of hotseat games (games where more than 1 person are playing on 1 computer).
Upvotes: 0
Reputation: 109823
before that don't forget to set Focus to the JPanel before listening KeyListener check KeyBindings for extended funcionalities, here is very usefull infos about Listeners in Swing
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
myPanel.grabFocus();
myPanel.requestFocus();//or requestFocusinWindow()
}
});
Upvotes: 2
Reputation: 57421
You can define your own keys processors like this (mainFrame is JFrame instance)
ActionMap actions = ((JComponent)mainFrame.getContentPane()).getActionMap();
InputMap inputs = ((JComponent)mainFrame.getContentPane()).getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
Action a=new AbstractAction() {
public void actionPerformed(ActionEvent e) {
//call your action code here
}
};
actions.put("myAction", a);
inputs.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.CTRL_DOWN_MASK), "myAction");
Upvotes: 2
Reputation: 24895
The keyListener listens to all keys pressed.
But inside the code you can check which key is pressed, if ignore it if it is not one of the keys that you want to react to. In fact, you could use the same class for both listeners and just pass the key that each of them has to react to as parameters.
Having said that, you should not need two threads for it. I understand two threads to avoid the GUI freezing (a thread for managing the GUI, another for doing the calculations of positions). Of course, being it homework, it can be asked this way just so get to learn the basic concepts of threading.
Upvotes: 0
Reputation: 5329
A key listener will listen to all keys. You just have to react to the ones you need. Since you can have any number of key listeners, there is no issue in having a key listener per player.
Upvotes: 1