简体   繁体   English

从源文件夹加载图像返回为null

[英]Loading image from source folder returned as null

Hello I have a problem with importing an image from my resources folder. 您好我从资源文件夹导入图像有问题。 I have looked all over google (or so I think) and I have no idea what I am doing wrong. 我看了很多谷歌(或者我认为),我不知道我做错了什么。

All help is appreciated thanks 感谢所有帮助

Here is the picture of my java project: 这是我的java项目的图片:

这是我的照片

Here is my Game Code: 这是我的Game代码:

public Game(){

    handler = new Handler();
    this.addKeyListener(new KeyInput(handler));

    new Window(WIDTH, HEIGHT, "Testing", this);

    BufferedImageLoader loader = new BufferedImageLoader();
    level = loader.loadImage("/level.png");

    hud = new HUD();
    spawn = new Spawn(handler, hud);
    r = new Random();
    walls = new WallsMap(handler);
    cam = new Camera(0, 0);

    walls.render();  
    handler.addObject(new Player(WIDTH/2-32, HEIGHT/2-32, ID.Player, handler));
}

Finally here is my BufferedImageLoader class: 最后这是我的BufferedImageLoader类:

    public class BufferedImageLoader {

    private BufferedImage image;

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

To solve your particular problem, either of these two options should work so that the resource can be properly located: 要解决您的特定问题,这两个选项中的任何一个都应该有效,以便可以正确定位资源:

  • Remove the call to getClassLoader() from your loadImage method so that the call is just getClass().getResourceAsStream(path) -OR- 从loadImage方法中删除对getClassLoader()的调用,以便调用只是getClass().getResourceAsStream(path) -OR-
  • Remove the slash from "/level.png" so that your call to loadImage looks like loader.loadImage("level.png") "/level.png"删除斜杠,以便您对loadImage的调用看起来像loader.loadImage("level.png")

However, in general, I agree with mastercork889 , that it is better practice to organize your resources into packages as well. 但是,总的来说,我同意mastercork889 ,将资源组织到包中也是更好的做法。

EDIT: 编辑:

In response to mastercork889 's comment saying that my answer should be AND instead of OR, I thought I should elaborate on why it is indeed exclusive OR. 回应mastercork889的评论说我的答案应该是AND而不是OR,我想我应该详细说明为什么它确实是异或。 I would have just commented, but I'm still too new to stack overflow to be allowed to comment, and this is quite a bit of information anyway :) 我本来只是评论过,但我还是太新了,不能堆栈溢出才能发表评论,这无论如何都是相当多的信息:)

Removing the call to getClassLoader() works because then you're using the getResourceAsStream() method from the Class class, which does extra work before delegation to the getResourceAsStream() method from the ClassLoader class. 删除对getClassLoader()的调用是有效的,因为那时你正在使用Class类中的getResourceAsStream()方法,它在从ClassLoader类委托给getResourceAsStream()方法之前做了额外的工作。

From the Java API : Java API

Before delegation, an absolute resource name is constructed from the given resource name using this algorithm: 在委派之前,使用此算法从给定资源名称构造绝对资源名称:

  • If the name begins with a '/' ('\/'), then the absolute name of the resource is the portion of the name following the '/'. 如果名称以'/'('\\ u002f')开头,则资源的绝对名称是'/'后面的名称部分。

Since your resource isn't in a package, and is at the top level of a source folder (it looks like res is an "Eclipse Source Folder"), the level.png portion following the slash will adequately identify the location of the resource in an absolute way. 由于您的资源不在包中,并且位于源文件夹的顶层(看起来res是“Eclipse源文件夹”),因此斜杠后面的level.png部分将充分识别资源的位置以绝对的方式。

OR 要么

Removing the slash from "/level.png" also works because then when the object's class loader (which should be the system class loader) looks for the resource, it will attempt to resolve your resource in an absolute way. "/level.png"删除斜杠也有效,因为当对象的类加载器(应该是系统类加载器)查找资源时,它将尝试以绝对方式解析您的资源。 The special handling of the slash at the beginning is behavior that is specific to the getResourceAsStream() method in Class , not ClassLoader . 开头斜杠的特殊处理是特定于ClassgetResourceAsStream()方法的行为,而不是ClassLoader

The reason that AND won't work, is because if you call the getResourceAsStream() method from Class and remove the slash, then it will attempt to locate the resource in the same package that the associated class is located in. If you choose to go with the AND option, then you would need to move level.png into the com.plat.gfx package. AND不起作用的原因是因为如果从Class调用getResourceAsStream()方法并删除斜杠,那么它将尝试在相关类所在的同一个包中找到该资源。如果选择使用AND选项,然后您需要将level.png移动到com.plat.gfx包中。

As a final point, I have built a small test program that followed the same format as your example, and once I followed one of the two suggestions, it worked for me. 最后,我已经构建了一个小型测试程序,其格式与您的示例相同,一旦我按照两个建议中的一个,它就适用于我。

The subtleties of resources, classes, and class loaders can be quite tricky. 资源,类和类加载器的细微之处可能非常棘手。 Good luck with your project! 祝你的项目好运!

Don't worry about putting images in a specific folder. 不要担心将图像放在特定文件夹中。 Instead put them also in the src folder under a specific package: com.plat.res 而是将它们也放在特定包下的src文件夹中: com.plat.res

I find that putting images in a specific package makes the package hierarchy look much more efficient, and less spaghetti-like. 我发现将图像放在特定的包中会使包层次结构看起来更有效率,而且不像意大利面。

Also a note on package conventions: domain-extension.domain.main-program.etc . 还有关于包约定的说明: domain-extension.domain.main-program.etc My package hierarchy looks like this: 我的包层次结构如下所示:

com.brennytizer.jumg
com.brennytizer.jumg.res
com.brennytizer.jumg.engine
com.brennytizer.jumg.level
com.brennytizer.jumg.level.maps

If you don't have a domain, write in what you think your domain would be (if you were to buy it in the future), or just use your (backwards) name: 如果您没有域名,请写下您认为您的域名(如果您将来要购买),或者只使用您的(向后)名称:

My name is Jarod Brennfleck , writing program foobar , my package would be: brennfleck.jarod.foobar . 我的名字是Jarod Brennfleck ,编写程序foobar ,我的包将是: brennfleck.jarod.foobar

Once in there use the ImageIO class: ImageIO.read(Game.class.getResourceAsStream("/com/plat/res/leve.png")); 进入那里后使用ImageIO类: ImageIO.read(Game.class.getResourceAsStream("/com/plat/res/leve.png"));

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

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