1

我在将 JPEG 加载到球体时遇到问题。目前,我让它工作的唯一方法是将图像与加载它的类放在同一个包中。问题是,我不想将 500 多个纹理全部放在一个包中与类混合。出于组织原因,我宁愿将它们放在文件夹和子文件夹中。

如果纹理在同一个包中,这有效:

InputStream iStream = null;

    try
    {
        iStream = getClass().getResourceAsStream(imageFile);
        TextureData data = TextureIO.newTextureData(iStream, false, null);
        testTex[i] = TextureIO.newTexture(data);
        testTex[i].getImageTexCoords();
        testTex[i].setTexParameteri(GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);
        testTex[i].setTexParameteri(GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_NEAREST);
        iStream.close();
        ...

获取图像的方法采用以下形式:

String[] texture = { "bar1.jpg", "bar2.jpg" }; // imageFile is whichever is called

由于上述代码完美运行,缺点是每个纹理都必须存储在与调用纹理的类相同的包中。但我想从我的项目中的一个文件夹中调用这样的纹理:

String[] texture = { "res/foo/bar2.jpg", "res/foo/bar2.jpg" };

我将如何修改上述代码以在项目的文件夹中查找纹理?

4

2 回答 2

2
// note leading '/' - indicates 'search from root of class-path'
String[] texture = { "/res/foo/bar2.jpg", "/res/foo/bar2.jpg" };
// ...
URL iUrl = getClass().getResource(texture[0]);
TextureData data = TextureIO.newTextureData(iUrl, false, "jpg");

In this example, we use an overloaded form of the TextureIO API loading method in newTextureData(URL,boolean,String).

于 2013-02-20T06:26:01.190 回答
2

I think your problem is just that you're missing a slash before res. This will definitely work:

this.getClass().getResource("/res/img.jpg");

as long as the res package is in your src folder.

于 2013-02-20T06:26:25.827 回答