尝试使用 KeyListeners 和 ImageIcons 时出错?

Getting errors trying to use KeyListeners and ImageIcons?

我目前正在尝试为学校制作迷宫游戏,但我很困惑为什么会出现这些错误。以下是涉及的代码片段。此外,需要导入的所有内容都已导入。首先是玩家 class,其中包含玩家的位置,然后是他们的图像。

public class Player {
int playerX;
int playerY;
int moveSpeed = 5; //I can edit move speed here
Image character;

//getters and setters to utilize the player's location and image
public Player(){ //constructor for initial starting points
    playerX = 50;
    playerY = 50;
    ImageIcon player = new ImageIcon("E://Workspace//Maze//images//Player.jpg");
    character = player.getImage();
}

public void setPlayerX(int playerX){
    this.playerX = playerX;
}
public int getPlayerX(){
    return playerX;
}

public void setPlayerY(int playerY){
    this.playerY = playerY;
}
public int getPlayerY(){
    return playerY;
}

public void setMoveSpeed(int moveSpeed){
    this.moveSpeed = moveSpeed;
}
public int getMoveSpeed(){
    return moveSpeed;
}

public Image getPlayerImage(){
    return character;
}
}

现在是创建 JPanel 的布局 class,应该是最终将玩家图像绘制到游戏中的布局,也是应该允许玩家移动的布局。

public class Layout extends JPanel implements ActionListener { //GUI with a      non null FlowLayout
Player p = new Player();
Maze m = new Maze();

//500 x 500 seemed like a good size for the maze game
int x = 500;
int y = 500;

public Layout(){
    setLayout(new FlowLayout());
    addKeyListener(new Move());
    setFocusable(true);
}

//for use in setting and getting the borders of the game
public void setX(int x){
    this.x = x;
}
public int getX(){
    return x;
}

public void setY(int y){
    this.y = y;
}
public int getY(){
    return y;
}

public void paintGame(Graphics g){
    super.paint(g);
    g.drawImage(p.getPlayerImage(), p.getPlayerX(), p.getPlayerY(), null);
}
@Overrride
public void actionPerformed(ActionEvent ae){
    repaint();
}
}

class Move extends Layout implements KeyListener { //Inheritance
public void keyPressed(KeyEvent press) { //for the movement in the game
    //I used both keys so that if the player woukld like to use WASD or the    arrow keys either will work
    if(press.getKeyCode() == KeyEvent.VK_W || press.getKeyCode() ==  KeyEvent.VK_UP){
        //move up
        p.setPlayerY(p.getPlayerY() - p.getMoveSpeed());
    }
    else if(press.getKeyCode() == KeyEvent.VK_S || press.getKeyCode() == KeyEvent.VK_DOWN){
        //move down
        p.setPlayerY(p.getPlayerY() + p.getMoveSpeed());
    }
    else if(press.getKeyCode() == KeyEvent.VK_A || press.getKeyCode() == KeyEvent.VK_LEFT){
        //move left
        p.setPlayerX(p.getPlayerX() - p.getMoveSpeed());
    }
    else if(press.getKeyCode() == KeyEvent.VK_D || press.getKeyCode() == KeyEvent.VK_RIGHT){
        //move right
        p.setPlayerX(p.getPlayerX() + p.getMoveSpeed());
    }
}


public void keyReleased(KeyEvent release) {
    //nothing is needed here
}

public void keyTyped(KeyEvent e) {
    //does nothing if a key is type (no need for it)
}
}

最后是 Play class,它应该 运行 游戏(目前我只是想让玩家在尚未实现地图的情况下穿过空白 space ).

public class Play {
public static void main(String[]args) { 
    play();
}

public static void play(){
    Layout l = new Layout();
    Player p = new Player();
    Maze m = new Maze();

    l.setSize(l.getX(), l.getY()); //size can be changed in layout
    l.setVisible(true);
}
}

现在由于某种原因,我在尝试 运行 时遇到了很多错误。我希望尽快解决这个问题,这样我就可以开始实施我的迷宫地图了。以下是我收到的错误:

Exception in thread "main" java.lang.WhosebugError
at java.lang.ref.SoftReference.get(Unknown Source)
at sun.misc.SoftCache$ValueCell.strip(Unknown Source)
at sun.misc.SoftCache$ValueCell.access0(Unknown Source)
at sun.misc.SoftCache.get(Unknown Source)
at sun.awt.SunToolkit.getImageFromHash(Unknown Source)
at sun.awt.SunToolkit.getImage(Unknown Source)
at javax.swing.ImageIcon.<init>(Unknown Source)
at javax.swing.ImageIcon.<init>(Unknown Source)
at mazeGame.Player.<init>(Player.java:16)
at mazeGame.Layout.<init>(Layout.java:11)
at mazeGame.Move.<init>(Layout.java:49)
at mazeGame.Layout.<init>(Layout.java:20)

at mazeGame.Move.<init>(Layout.java:49) at mazeGame.Layout.<init>(Layout.java:20) 在 Eclipse 停止输出错误之前继续至少 20 行。 请帮我解决这个问题,因为我目前还在研究如何让玩家移动,并且需要开始为迷宫创建地图。很抱歉 post,但我想 post 任何可能给我错误的东西。

尝试替换行:

ImageIcon player = new ImageIcon("E://Workspace//Maze//images//Player.jpg");
character = player.getImage();

与:

character = ImageIO.read(new File("E://Workspace//Maze//images//Player.jpg"));

我认为问题可能是由 ImageIcon 的异步加载引起的(图像将通过使用 MediaTracker 来预加载以监视图像的加载状态。)http://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html

问题是您的构造函数中有无限递归。 Layout() 调用 addKeyListener(new Move());,而 Move 扩展 Layout,因此 Move 的构造函数也调用 Layout(),后者又调用 Move().

您需要重构代码以避免这种情况。一种简单的方法是使 Move 不扩展 Layout,并将播放器作为构造函数参数传递给 Move.

class Move implements KeyListener {
    final Player p;
    Move(Player p) {
        this.p = p;
    }
    ...
}

并在 Layout 中:

addKeyListener(new Move(p));

还可以考虑使用 key bindings 而不是密钥侦听器。这通常是更好的方法。