Question

Im having trouble getting the key pressed event to work, it doesn't recognize key input, can someone please help?

I have three classes player, exe, and board

Here is the board class

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Board extends JPanel implements ActionListener{
private Timer timer;
private Player player;

//private Floor

public Board() {

    addKeyListener( new TAdapter() );
    setFocusable(true);
    setBackground(Color.WHITE);
    setDoubleBuffered(true);

    player = new Player();
    //floor = new Floor();

    timer = new Timer(5, this);
    timer.start();
}

public void paint(Graphics g) {

    super.paint(g);

    Graphics2D g2d = (Graphics2D)g;
    g2d.drawImage(player.getImage(), player.getX(), player.getY(), this);
    // g2d.drawImage(floor.getImage(), floor.getX(), floor.getY(), this);
    //System.out.println(player.getX() + ", " + player.getY());
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}

public void actionPerformed(ActionEvent e) {
    // checkPlayerOnGround();
    player.move();
    repaint();  
}

private class TAdapter extends KeyAdapter implements KeyListener{

    public void keyTyped(KeyEvent e) {
        player.keyReleased(e);
        System.out.println("Released");
    }

    public void keyPressed(KeyEvent e) {
        player.keyPressed(e);
        System.out.println("Pressed");

    }
}

}

here is the player class

import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.Timer;
import java.util.TimerTask;
public class Player{

private String player = "player.jpg";
private int moveX;
private int moveY;
private int x;
private int y;
private Image image;
private boolean canFall = false;

//private Timer jumpTimer = new Timer();
boolean canJump = true;

public Player() {
    ImageIcon playeriImage = new ImageIcon(this.getClass().getResource(player));
    image = playeriImage.getImage();
    x = 40;
    y = 60;
}

public void checkFall() {
    if(canFall) {
        moveY = -4;
    }
}

public void move() {
    //checkFall();
    x = moveX + x;
    y  = moveY + y;

}

public int getX() {
    return x;
}

public int getY() {
    return y;
}

public Image getImage() {
    return image;
}

public void keyPressed(KeyEvent e) {

    int key = e.getKeyCode();

    if (key == KeyEvent.VK_A) {
        moveX = -4;

    }

    if (key == KeyEvent.VK_D) {
        moveX = 4;
    }

    if (key == KeyEvent.VK_W) {
        if(canJump)
            this.jump();

    }

}

public void keyReleased(KeyEvent e) {
    int key = e.getKeyCode();

    if (key == KeyEvent.VK_A) {
        moveX = 0;

    }

    if (key == KeyEvent.VK_D) {
        moveX = 0;

    }

}

here is the exe class

import javax.swing.JFrame;

public class Exe extends JFrame {

public Exe() {

    add(new Board());

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500, 500);
    setLocationRelativeTo(null);
    setTitle("TSA Video Game");
    setResizable(false);
    setVisible(true);
}

public static void main(String[] args) {
    new Exe();

}

}

thanks in advance

Was it helpful?

Solution

KeyListener will only respond to key events IF the component is focusable AND has focus.

It would be better to use the Key Bindings API which will allow you to overcome this limitation (if you want to)

I would also recommend that you override paintComponent instead of paint. You should also not be calling Graphics#dispose, as you did not create the Graphics context, this could actually prevent anything from being painted on some systems

I would also suggest that a delay of 5 milliseconds (on your Timer) might be a little excessive (5 milliseconds = 200fps!).

A value of 16 (60fps) or 40 (25fps) may be more suitable and reduce the overhead on the system...IMHO

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top