简体   繁体   English

Slick2D正在加载图像无法正常工作

[英]Slick2D Loading image wont work

So I'm trying to load an image using Slick2D but for some reason I get the following error when I attempt to draw the image to the screen: 因此,我尝试使用Slick2D加载图像,但是由于某些原因,当我尝试将图像绘制到屏幕上时出现以下错误:

Sat Sep 28 16:37:59 NZST 2013 ERROR:null
java.lang.NullPointerException
    at org.newdawn.slick.Graphics.drawImage(Graphics.java:1350)
    at org.newdawn.slick.Graphics.drawImage(Graphics.java:1399)
    at Game.render(Game.java:58)
    at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:703)
    at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:456)
    at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:361)
    at Game.main(Game.java:42)
Sat Sep 28 16:37:59 NZST 2013 ERROR:Game.render() failure - check the game code.
org.newdawn.slick.SlickException: Game.render() failure - check the game code.
    at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:706)
    at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:456)
    at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:361)
    at Game.main(Game.java:42)

I am using this to load my image: 我正在使用它来加载我的图像:

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class Textures {

    static Image grass;
    Game game;


    public Textures() {
            try {
                grass = new Image("grass.jpg");
            } catch (SlickException e) {
                e.printStackTrace();
            }
    }

}

I am using this to draw my image: 我正在使用它来绘制我的图像:

for(int x = 0; x < app.getWidth(); x+=32) {
    for(int y = 0; y < app.getHeight(); y+=32) {
        g.drawImage(textures.grass, x, y);
    }
}

If you get a NullPointerException when trying to draw an Image , it's usually because the image loader didn't find the image at the location specified. 如果在尝试绘制Image时收到NullPointerException ,通常是因为图像加载器在指定位置找不到图像。 If you're using Eclipse , be sure to store your images in a source folder (for instance named "resources"). 如果您使用的是Eclipse ,请确保将图像存储在source folder (例如,名为“ resources”)。 This ensures images are moved to the bin folder on compilation (may require refreshing the source folder). 这样可以确保将图像在编译时移至bin文件夹(可能需要刷新源文件夹)。

Regardless of your setup, be sure the path to the image is correct. 无论设置如何,请确保图像路径正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM