无法弄清楚如何使keyadapter工作

时间:2016-03-18 08:16:48

标签: java swing keylistener

我对java很新,但是想学习。 现在我正在开发一个小游戏,以提高自己,但我无法弄清楚如何使这个KeyAdapter工作。 我读了很多关于它的内容,但JPanel还是没有听到#34;我的键盘上会发生什么。

我的董事会课程:

public class Board extends JPanel implements Runnable {
    private static final long DELAY = 1000;
    private static int TILESIZE = 20;
    private static final int WIDTH = 11 * TILESIZE;
    private static final int HEIGHT = 11 * TILESIZE;
    private Map map;
    private Model tree;
    private Model wall;
    private Model floor;

public Board(Map map){
    this.map = map;
    addKeyListener(new TAdapter());
    setPreferredSize(new Dimension(WIDTH, HEIGHT));
    setDoubleBuffered(true);
}

public void start(){
    new Thread(this).start();
}

public void init(){
    tree = Model.getTreeModel();
    wall = Model.getWallModel();
    floor = Model.getFloorModel();
}

public void run() {
    long beforeTime, timeDiff, sleep;

    beforeTime = System.currentTimeMillis();

    init();

    while(map.getPlayer().isAlive()){
        repaint();
        timeDiff = System.currentTimeMillis() - beforeTime;
        sleep = DELAY - timeDiff;

        if (sleep < 0) {
            sleep = 2;
        }

        try {
            Thread.sleep(sleep);
        } catch (InterruptedException e) {
            System.out.println("Interrupted: " + e.getMessage());
        }

        beforeTime = System.currentTimeMillis();
    }
    new GameOverFrame();
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    try {
        render(g);
    }catch (Exception e){
        e.printStackTrace();
    }

    Toolkit.getDefaultToolkit().sync();
}

public void render(Graphics g){

    int startX = map.getPlayerX() - 5 > 0 ? (map.getPlayerX() < map.getWidth()
            ? map.getPlayerX() - 5 : map.getWidth() - 11 ) : 0;
    int startY = map.getPlayerY() - 5 > 0 ? (map.getPlayerY() < map.getHeight()
            ? map.getPlayerY() - 5 : map.getHeight() - 11 ) : 0;

    int x,y;
    y = 0;
    for (int i = startY; i < startY + 11; i++) {
        x=0;
        for (int j = startX; j < startX + 11 ; j++) {
            Items item = map.getMap()[i][j];
            switch(item){
                case FLOOR:
                    g.drawImage(floor.getCurrentImage(), x * TILESIZE, y * TILESIZE, null);
                    break;
                case WALL:
                    g.drawImage(wall.getCurrentImage(),  x * TILESIZE, y * TILESIZE, null);
                    break;
                case MONSTER:
                    g.drawImage(map.getMonster().getModel().getCurrentImage(), x * TILESIZE, y * TILESIZE, null);
                    break;
                case TREE:
                    g.drawImage(tree.getCurrentImage(), x * TILESIZE, y * TILESIZE, null);
                    break;
                case DRAGON:
                    g.drawImage(map.getDragon().getModel().getCurrentImage(), x * TILESIZE, y * TILESIZE, null);
                    break;
                case PLAYER:
                    g.drawImage(map.getPlayer().getModel().getCurrentImage(), x * TILESIZE, y * TILESIZE, null);
                    break;
                default:
                    g.drawImage(wall.getCurrentImage(),  x * TILESIZE, y * TILESIZE, null);
                    break;
            }
            x++;
        }
        y++;
    }
}

private class TAdapter extends KeyAdapter {
    public void keyTyped(KeyEvent e) {
        map.keyTyped(e);
    }
}

我的框架:

public GameFrame() {
    super("DragonSlayer");
    Board board = new Board(MapFactory.createStandartMap());
    add(board);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    pack();
    setResizable(false);
    setVisible(true);
    setLocationRelativeTo(null);
    board.start();
}

我尝试了不同的方法来实现我的目标,尽管他们都失败了。也许有人会

0 个答案:

没有答案