无法读取输入文件-Java

时间:2015-04-21 00:58:04

标签: java file stack-trace

所以我正在学习一系列教程,学习如何在java中制作游戏(我还很新),我认为我完全遵循他的代码,但是当我运行它时它仍会打印堆栈跟踪。这是我的代码......

Game.java

package com.game.src.main;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.swing.JFrame;

public class Game extends Canvas implements Runnable{
    private static final long serialVersionUID = 1L;
    public static final int WIDTH = 320;
    public static final int HEIGHT = WIDTH / 12 * 9;
    public static final int SCALE = 2;
    public final String TITLE = "2D Space Game";

    private JFrame frame = new JFrame(TITLE);
    private boolean running = false;
    private Thread thread;
    private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    private BufferedImage spriteSheet = null;

    private BufferedImage player;

    public void init(){
        BufferedImageLoader loader = new BufferedImageLoader();
        spriteSheet = loader.loadImage("/sprite_sheet.png");

        SpriteSheet ss = new SpriteSheet(spriteSheet);
        player = ss.grabImage(1, 1, 32, 32);
    }

    public void run(){
        long lastTime = System.nanoTime();
        final double amountOfTicks = 60.0;
        double ns = 1000000000 / amountOfTicks;
        double delta = 0;
        long now = 0;
        int updates = 0, frames = 0;
        long timer = System.currentTimeMillis();

        init();
        while(running){
            now = System.nanoTime();
            delta += (now - lastTime) / ns;
            lastTime = now;
            if(delta >= 1){
                tick();
                updates++;
                delta--;
            }
            render();
            frames++;

            if(System.currentTimeMillis() - timer > 1000){
                timer += 1000;
                frame.setTitle("2-D Space Game  ||  Updates: " + updates + ", FPS: " + frames);
                frames = 0;
                updates = 0;
            }
        }
        stop();
    }

    private void tick(){

    }

    private void render(){
        BufferStrategy bs = this.getBufferStrategy();
            try {
                Thread.sleep(2);
            } catch (InterruptedException e){
                e.printStackTrace();
            }
        if(bs == null){
            createBufferStrategy(3);
            return;
        }

        Graphics g = bs.getDrawGraphics();
        /////////////////////////////////////

        g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
        g.drawImage(player, 100, 100, this);

        /////////////////////////////////////
        g.dispose();
        bs.show();
    }

    private synchronized void start(){
        if(running) return;
        running = true;
        thread = new Thread(this);
        thread.start();
    }

    private synchronized void stop(){
        if(!running) return;
        try{
            thread.join();
        }catch(InterruptedException e){
            e.printStackTrace();
        }
        System.exit(1);
    }

    public static void main(String args[]){
        Game game = new Game();
        game.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
        game.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
        game.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));

        game.frame.add(game);
        game.frame.pack();
        game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        game.frame.setResizable(false);
        game.frame.setLocationRelativeTo(null);
        game.frame.setVisible(true);
        game.start();
    }
}

SpriteSheet.java

package com.game.src.main;

import java.awt.image.BufferedImage;

public class SpriteSheet {
    private BufferedImage image;

    public SpriteSheet(BufferedImage image){
        this.image = image;
    }

    public BufferedImage grabImage(int col, int row, int width, int height){
        return image.getSubimage((col * 32) - 32, (row * 32) - 32, width, height);
    }
}

BufferedImageLoader.java

package com.game.src.main;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class BufferedImageLoader {
    private BufferedImage image;

    public BufferedImage loadImage(String path){
        try {
            image = ImageIO.read(getClass().getResource(path));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        return image;
    }
}

现在这里是显示的堆栈跟踪:

Exception in thread "Thread-0" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(ImageIO.java:1388)
    at com.game.src.main.BufferedImageLoader.loadImage(BufferedImageLoader.java:13)
    at com.game.src.main.Game.init(Game.java:30)
    at com.game.src.main.Game.run(Game.java:45)
    at java.lang.Thread.run(Thread.java:745)

有人可以帮忙吗? BTW: sprite_sheet.png确实存在

3 个答案:

答案 0 :(得分:0)

这里的问题在于您对png的处理方式,以及您指定的位置不在您的构建路径中,也不在您期望的位置。如果您向我提供了您所在位置的pwd,我将使用您应该使用的实际路径进行更新

您需要提供完整路径

/home/me/myprogram/sprite_sheet.png

或使用相对路径

./sprite_sheet.png

您的程序正在根目录/

中查找该文件

答案 1 :(得分:0)

好的我明白了。我的sprite_sheet.png在我的“res”文件夹中,我需要将它添加到构建路径

答案 2 :(得分:-1)

您需要提供相对于CLASSPATH的完整路径或相对于当前类包的相对路径。