添加spritesheet时出错

时间:2014-01-08 04:06:17

标签: java sprite

输入private SpriteSheet spriteSheet = new SpriteSheet("/sprite_sheet.png");时出错 当我评论出那部分时,该程序运作良好 有任何想法吗?我的模式是8位,spritesheet是32x32

Game.java:

package ca.swimmerwoad.adventuregame;

import java.awt.BorderLayout;
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.awt.image.DataBufferInt;

import javax.swing.JFrame;

import ca.swimmerwoad.adventuregame.gfx.SpriteSheet;

public class Game extends Canvas implements Runnable {

    private static final long serialVersionUID = 1L;

    public static final int WIDTH = 160;
    public static final int HEIGHT = WIDTH/12*9;
    public static final int SCALE = 3;
    public static final String NAME = "Game";

    private JFrame frame;

    public boolean running = false;
    public int tickCount = 0;

private BufferedImage image = new
         BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
    private int[] pixels = ((DataBufferInt)

image.getRaster().getDataBuffer()).getData();

    private SpriteSheet spriteSheet = new SpriteSheet("/sprite_sheet.png");

    public Game() {
            setMinimumSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
            setMaximumSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
            setPreferredSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));

            frame = new JFrame(NAME);

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());

            frame.add(this, BorderLayout.CENTER);
            frame.pack();

            frame.setResizable(false);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
    }

    public synchronized void start() {
            running = true;
            new Thread(this).start();
    }

    public synchronized void stop() {
            running = false;
    }
public void run() {
    long lastTime = System.nanoTime();
    double nsPerTick = 1000000000D/60D;

    int ticks = 0;
    int frames = 0;


    long lastTimer = System.currentTimeMillis();
    double delta = 0;

    while (running) {
            long now = System.nanoTime();
            delta += (now - lastTime) / nsPerTick;
            lastTime = now;
            boolean shouldRender = true;

            while (delta >= 1) {
        ticks++;
            tick();
            render();
            delta -= 1;
            shouldRender = true;
            }

            try {
                    Thread.sleep(2);
            } catch (InterruptedException e) {
            e.printStackTrace();

            }      

            if(shouldRender) {
            frames++;
            render();

if (System.currentTimeMillis() - lastTime > 1000) {
    lastTimer +=1000;
    System.out.println(frames + "," + ticks);
    frames = 0;
    ticks = 0;
}
    }
    }
}

public void tick() {
    tickCount++;

    for (int i=0;i < pixels.length; i++) {
            pixels[i] = i + tickCount;
    }

}

public void render() {
    BufferStrategy bs = getBufferStrategy();
    if (bs == null) {
            createBufferStrategy(3);
            return;
    }

    Graphics g = bs.getDrawGraphics();

    g.setColor(Color.BLACK);
    g.fillRect(0,0,getWidth(), getHeight());

    g.drawImage(image, 0,0, getWidth(), getHeight(), null);


    g.dispose();
    bs.show();
}
public static void main(String[] args) {
    new Game().start(); }
}

SpriteSheet.java

package ca.swimmerwoad.adventuregame.gfx;

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

import javax.imageio.ImageIO;

public class SpriteSheet {

public String path;
public int width;
public int height;

public int[] pixels;

public SpriteSheet(String Path){
    BufferedImage image = null;

    try {
        image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (image == null) {
        return;
    }

    this.path = path;
    this.width = image.getWidth();
    this.height = image.getHeight();

    pixels = image.getRGB(0, 0, width, height, null, 0, width);

    for(int i = 0; i<pixels.length; i++ ){
        pixels[i] = (pixels[i] & 0xff) / 64;
    }

    for(int i = 0; i<8; i++) {
        System.out.println(pixels[i]);
    }
}
}

我的错误是:

Exception in thread "main" java.lang.NullPointerException
at sun.misc.MetaIndex.mayContain(Unknown Source)
at sun.misc.URLClassPath$JarLoader.getResource(Unknown Source)
at sun.misc.URLClassPath.getResource(Unknown Source)
at sun.misc.URLClassPath.getResource(Unknown Source)
at java.lang.ClassLoader.getBootstrapResource(Unknown Source)
at java.lang.ClassLoader.getResource(Unknown Source)
at java.lang.ClassLoader.getResource(Unknown Source)
at java.net.URLClassLoader.getResourceAsStream(Unknown Source)
at java.lang.Class.getResourceAsStream(Unknown Source)
at ca.swimmerwoad.adventuregame.gfx.SpriteSheet.<init>(SpriteSheet.java:20)
at ca.swimmerwoad.adventuregame.Game.<init>(Game.java:33)
at ca.swimmerwo

1 个答案:

答案 0 :(得分:3)

你写过

public SpriteSheet(String Path)

我认为应该是

public SpriteSheet(String path)

否则您想要创建精灵的路径,即path变量为空,因此异常

NPE来自这一行

SpriteSheet.class.getResourceAsStream(path)

你刚刚抓到IOException

 try {
        image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));
    } catch (IOException e) {
        e.printStackTrace();
    }