尝试使用KeyListeners和ImageIcons时出错?

时间:2015-05-04 10:25:19

标签: java swing graphics stack-overflow imageicon

我目前正在尝试为学校制作一个迷宫游戏,我很困惑为什么我会收到这些错误。以下是涉及的代码片段。此外,还需要导入所有需要导入的内容。首先是Player类,它具有玩家的位置,然后是他们的图像。

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的布局类,应该是最终将玩家图像绘制到游戏中的内容,也是允许玩家移动的内容。

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类(目前我只是想让玩家在没有实现地图的情况下移动到空白区域)。

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.StackOverflowError
at java.lang.ref.SoftReference.get(Unknown Source)
at sun.misc.SoftCache$ValueCell.strip(Unknown Source)
at sun.misc.SoftCache$ValueCell.access$300(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)

在Eclipse停止输出错误之前,at mazeGame.Move.<init>(Layout.java:49) at mazeGame.Layout.<init>(Layout.java:20)将持续至少20行。 请帮助我解决这个问题,因为我目前仍然坚持如何让玩家移动,并且需要开始为迷宫创建地图。很抱歉这个很长的帖子,但我想发布任何可能给我错误的内容。

2 个答案:

答案 0 :(得分:2)

问题是你的构造函数中有无限递归。 Layout()调用addKeyListener(new Move());Move延长Layout,因此Move的构造函数也会调用Layout()Move()会调用Move }。

您需要重构代码以避免这种情况。一个简单的方法是使移动不扩展布局,并将播放器作为构造函数参数传递给class Move implements KeyListener { final Player p; Move(Player p) { this.p = p; } ... }

Layout

addKeyListener(new Move(p));

id A  B
1  10 100
2  10 101
3  20 102

另请考虑使用key bindings而不是密钥监听器。这通常是一种更好的方法。

答案 1 :(得分:0)

尝试替换行:

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