KeyListener events apparently not firing in java applet
Asked Answered
A

1

7

I've just started learning to program applets, and java GUI in general, I'm attempting to make a simple game. At the moment, i have the main class (Game) and the player class (Player) i have the player drawing to the screen, but i'm struggling to get the key events to work, any help appreciated!

The player image displays perfectly fine, but the position doesn't change when i hit a key at all.

EDIT: wrong code posted for the player, updated

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;
    }
}
Amby answered 29/2, 2012 at 18:52 Comment(1)
1) "I've just started learning to program applets, and java GUI in general" Avoid applets and learn using frames. They are simpler. 2) This millennium, use Swing instead of AWT. 3) An applet size is set by the HTML, don't call setSize() in the init().Retsina
R
9

The panel probably either does not have focus, or is not focusable.


Try this code. I changed the keys to the left/right/up/down arrows. 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,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, x, y, null);
    }

}
Retsina answered 29/2, 2012 at 19:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.