为什么我看不懂文件?

时间:2015-09-24 06:08:30

标签: java file

嗨,我想制作简单的游戏。但在我创建游戏逻辑之前,我遇到了问题。正如我在标题上提到的那样。我无法读取jpg文件。我收到此错误消息“无法读取输入文件!”。我知道它来自e.getMessage()。但就是这样。我真的不知道为什么会这样。在导弹游戏包中,已经有3个jpg文件。

所以,这是代码。

package missilegame;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

class GraphicObject {
BufferedImage img = null;
int x, y = 0;

public GraphicObject(String name) {
    try {
        img = ImageIO.read(new File(name));
    } catch (Exception e) {
        System.out.println(e.getMessage());
        System.exit(0);
    }
}

public void update() {
}

public void draw(Graphics g) {
    g.drawImage(img, x, y, null);
}

public void keyPressed(KeyEvent event) {

}

public void keyPressed(KeyEvent event, int x, int y) {
    // TODO Auto-generated method stub

}
}

class Missile extends GraphicObject {
    boolean launched = false;

    public Missile(String name) {
        super(name);
        y = -200;
    }

    @Override
    public void update() {
        if (launched)
            y -= 1;
        if (y < -100) {
            launched = false;
        }
    }

    @Override
    public void keyPressed(KeyEvent event, int x, int y) {
        if (event.getKeyCode() == KeyEvent.VK_SPACE) {
            launched = true;
            this.x = x;
            this.y = y;
        }
    }
}

class Enemy extends GraphicObject {
    int dx = -10;

    public Enemy(String name) {
        super(name);
        x = 500;
        y = 0;
    }

    public void update() {
        x += dx;
        if (x < 0) {
            dx = +10;
        }
        if (x > 500)
            dx = -10;
    }

}

class SpaceShip extends GraphicObject {
    public SpaceShip(String name) {
        super(name);
        x = 150;
        y = 350;
    }

    @Override
    public void keyPressed(KeyEvent event) {
        if (event.getKeyChar() == KeyEvent.VK_LEFT) {
            x -= 10;
        }
        if (event.getKeyChar() == KeyEvent.VK_RIGHT) {
            x += 10;
        }
        if (event.getKeyChar() == KeyEvent.VK_UP) {
            y -= 10;
        }
        if (event.getKeyChar() == KeyEvent.VK_DOWN) {
            y += 10;
        }
    }
}

class MyGamePanel extends JPanel implements KeyListener {
    Enemy enemy;
    SpaceShip spaceShip;
    Missile missile;

    public MyGamePanel() {
        this.addKeyListener(this);
        this.requestFocus();
        setFocusable(true);

        enemy = new Enemy("./enemy.jpg");
        spaceShip = new SpaceShip("./spaceship.jpg");
        missile = new Missile("./missile.jpg");
        class MyThread extends Thread {
            public void run() {
                while (true) {
                    enemy.update();
                    spaceShip.update();
                    missile.update();
                    repaint();
                    try {
                        Thread.sleep(50);
                    } catch (Exception e) {
                    }
                }
            }

        }
        Thread t = new MyThread();
        t.start();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        enemy.draw(g);
        spaceShip.draw(g);
        missile.draw(g);
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        spaceShip.keyPressed(e);
        missile.keyPressed(e,spaceShip.x,spaceShip.y);
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }

}

public class MissileGame extends JFrame {
    public MissileGame() {
        setTitle("MY Game");
        add(new MyGamePanel());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(500, 500);
        setVisible(true);
    }

    public static void main(String[] args) {
        new MissileGame();
    }

}

1 个答案:

答案 0 :(得分:2)

  

在missilegame包中,已有3个jpg文件。

但是你的代码没有从missilegame包中读取。它读取文件系统上当前目录中的文件。当前目录是执行java命令的目录。而且你的文件不一定(在文件系统上)。它们是嵌入在应用程序中的资源,应该加载类加载器。

替换

img = ImageIO.read(new File(name));

通过

img = ImageIO.read(GraphicObject.class.getResourceAsStream("/missilegame/" + name));

并将"./enemy.jpg"替换为"enemy.jpg"