The keylistener event is obviously not triggered in the Java applet

I've just started learning to program applets and Java GUIs. I'm trying to make a simple game At present, I have main categories (games) and player categories (players). I let players draw on the screen, but it is difficult for me to make key events work. Any help is appreciated!

The player image display is completely normal, but the position will not change when I press the key completely

Edit: release error code for player, update

Game. java:

public class Game extends Applet {

    Player player;

    @Override
    public void init() {
        player = new Player(this);
        this.setSize(800,600);
        addKeyListener(player);
    }

    @Override
    public void paint(Graphics g) {        
        player.paint(g);
    }



}

Player. java:

public class Player implements KeyListener {

    Game game;
    Image image;
    int x;
    int y;

    public Player(Game game) {
        this.game = game;
        x = 0;
        y = 0;
        image = game.getImage(game.getCodeBase(),"images/Player.png");
    }

    public void paint(Graphics g) {
        g.drawImage(image,x,y,game);
    }

    @Override
    public void keyTyped(KeyEvent e) {
        return;
    }

    @Override
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_A:
                x -= 1;
                break;
            case KeyEvent.VK_D:
                x += 1;
                break;
            case KeyEvent.VK_S:
                y += 1;
                break;
            case KeyEvent.VK_W:
                y -= 1;
                break;
        }
        game.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        return;
    }
}

Solution

The panel may either have no focus or no focus

Try this code I changed the key to left / right / up / down arrow For simplicity, I left it as an AWT based applet

// <applet code='Game' width=400 height=100></applet>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Game extends Applet implements KeyListener {

    Player player;

    @Override
    public void init() {
        player = new Player(getImage(32));
        addKeyListener(this);
        setFocusable(true);
        requestFocusInWindow();
    }

    @Override
    public void paint(Graphics g) {
        player.paint(g);
    }

    public Image getImage(int size) {
        Image img = createImage(size,size);
        Graphics g = img.getGraphics();
        g.setColor(Color.RED);
        g.fillRect(0,size,size);

        g.dispose();

        return img;
    }

    @Override
    public void keyTyped(KeyEvent e) {
        return;
    }

    @Override
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_LEFT:
                player.x -= 1;
                break;
            case KeyEvent.VK_RIGHT:
                player.x += 1;
                break;
            case KeyEvent.VK_DOWN:
                player.y += 1;
                break;
            case KeyEvent.VK_UP:
                player.y -= 1;
                break;
        }
        repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        return;
    }
}

class Player {

    Image image;
    int x;
    int y;

    public Player(Image image) {
        x = 0;
        y = 0;
        this.image = image;
    }

    public void paint(Graphics g) {
        g.drawImage(image,null);
    }

}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>