我一直在尝试使用我在网上找到的精灵表,并使用其中的一些图片来制作我正在制作的小游戏。我看过有关如何从精灵表中读取的教程,但是当我试图加载Sprite表时,编译器似乎无法加载文件,这是在资源包中。
这是我得到的错误:
java.lang.IllegalArgumentException: input == null!
Image load failed
at javax.imageio.ImageIO.read(ImageIO.java:1388)
at data.BufferedImageLoader.loadImage(BufferedImageLoader.java:17)
at data.Boot.spriteSheetLoader(Boot.java:113)
at data.Boot.<init>(Boot.java:89)
at data.Boot.main(Boot.java:132)
Exception in thread "main" java.lang.NullPointerException
at data.SpriteSheet.grabSprite(SpriteSheet.java:23)
at data.Boot.spriteSheetLoader(Boot.java:122)
at data.Boot.<init>(Boot.java:89)
at data.Boot.main(Boot.java:132)
这是我正在使用的图片:http://imgur.com/a/D5A1N
主类
package data;
import org.lwjgl.opengl.Display;
import org.newdawn.slick.opengl.Texture;
import helpers.Artist.*;
import static helpers.Artist.*;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.swing.JFrame;
public class Boot extends JFrame {
BufferedImage image;
public Boot(){
beginSession();
//4, in order for this to work, we use a nested if statement(Tile Grid class)
int [][] map = {
{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2, 2, 2, 2, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2, 2, 2, 2, 2, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
{2, 2, 2, 2, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2, 2, 2, 2, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2, 2, 2, 2, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
};
//1can either load the textures like this or*
/*Texture tex = LoadTexture("resources/Grass32.png", "PNG");
Texture tex0 = LoadTexture("resources/Dirt32.png", "PNG");*/
//2like this, method (QuickLoad) is from Artist
/*Texture tex = QuickLoad("Grass32");
Texture tex1 = QuickLoad("Dirt32");*/
//3like this, this is from the Tile constructor/Tile type class
/*Tile tile = new Tile(100, 100, 32, 32, TileType.Grass);
Tile tile1 = new Tile(132, 100, 32, 32, TileType.Grass);*/
//4to fill the screen up with the same tiles
TileGrid grid = new TileGrid(map);
//sets the tile, in order to do that, we say where we want to set it, then we have to grab a tile from the grid, with the coordinates and type
grid.setTile(0, 1, grid.GetTile(0, 0).getType());
//loading the enemy
Enemy e = new Enemy(QuickLoad("RedTank32"), grid.GetTile(9, 13), 32, 32, 2);
while(!Display.isCloseRequested()){
/*1/2 drawQuadTex(tex, 0, 0, 32, 32);
drawQuadTex(tex1, 32, 0, 32, 32);*/
//3/1
/*drawQuadTex(tile.getTexture(), tile.getX(), tile.getY(), tile.getWidth(), tile.getHeight());
drawQuadTex(tile1.getTexture(), tile1.getX(), tile1.getY(), tile1.getWidth(), tile1.getHeight());*/
//3/2instead of the above, we can pass in the arguments into a method in the tile class, and call that method here
/*tile.Draw();
tile1.Draw();*/
//4
grid.Draw();
e.Draw();
spriteSheetLoader();
Display.update();
Display.sync(60);
}
Display.destroy();
}
//this is where i load the sprite sheet
public void spriteSheetLoader(){
//this is for sprite sheet
/*BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
g.drawImage(grass, 150, 150, this);*/
BufferedImageLoader Loader = new BufferedImageLoader();
BufferedImage spriteSheet = null;
try {
//It seems to have a problem loading this
spriteSheet = Loader.loadImage("SpriteSheet.png");
} catch (Exception e) {
e.printStackTrace();
System.out.println("Image load failed");
}
SpriteSheet ss = new SpriteSheet(spriteSheet);
image = ss.grabSprite(0, 0, 32, 32);
}
@Override
public void paint(Graphics g){
g.drawImage(image, 160, 160, null);
repaint();
}
public static void main(String[] args) {
new Boot();
}
}
艺术家班
public class Artist extends Canvas{
private static final long serialVersionUID = 1L;
public static final int WIDTH = 640, HEIGHT = 480;
public static void beginSession(){
Display.setTitle("Game");
try {
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);//setting up camera
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
}
public static void DrawQuad(float x, float y, float width, float height){
glBegin(GL_QUADS);
glVertex2f(x, y);//top left corner
glVertex2f(x + width, y);//top right corner
glVertex2f(x + width, y + height);//bottom right corner
glVertex2f(x, y + height);//bottom left corner
}
public static void drawQuadTex(Texture texture, float x, float y, float width, float height){
texture.bind();
glTranslatef(x, y, 0);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(1, 0);
glVertex2f(width, 0);
glTexCoord2f(1, 1);
glVertex2f(width, height);
glTexCoord2f(0, 1);
glVertex2f(0, height);
glEnd();
glLoadIdentity();
}
//this is to load a single image
public static Texture LoadTexture(String path, String fileType){
Texture tex = null;
InputStream in = ResourceLoader.getResourceAsStream(path);
try {
tex = TextureLoader.getTexture(fileType, in);
} catch (Exception e) {
e.printStackTrace();
}
return tex;
}
//this is to load a single image
public static Texture QuickLoad(String name){
Texture tex = null;
tex = LoadTexture("resources/" + name + ".png", "PNG");
return tex;
}
精灵表类
public class SpriteSheet {
public BufferedImage spriteSheet;
public BufferedImage Grass;
public BufferedImage Dirt;
public SpriteSheet(BufferedImage ss) {
this.spriteSheet = ss;
}
public BufferedImage grabSprite(int x, int y, int width, int height) {
BufferedImage sprite = spriteSheet.getSubimage(x, y, width, height);
return sprite;
}
}
Bufferd Image class
public class BufferedImageLoader {
private BufferedImage image;
public BufferedImage loadImage(String path){
URL url = this.getClass().getResource(path);
try {
image = ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
}
我确实有其他类,但主要是加载单个图像,而不是从精灵表中加载。