2013-01-18 34 views
0

我正在使用LWJGL和Slick2D。我將項目導出到.jar並使用JarSplice生成可執行文件,因此我不需要可執行jar所在位置的庫文件。我的問題是,如果我從Eclipse運行項目然後加載每個圖像,但如果我運行導出的可執行文件jar,圖像不加載,因爲它無法找到。照片是在/ res文件夾,這是載入紋理方法:在導出的jar文件中未加載紋理

private Texture loadTexture(String key) { 
    try { 
     return TextureLoader.getTexture("PNG", new FileInputStream(
       new File("res/" + key + ".png"))); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

,在這裏我加載質地:

Texture background = loadTexture("main_menu/bg"); 

我試過很多辦法出口罐子,但我沒有得到任何工作方式。

+0

不要將「已解決」添加到問題的標題中。指出這一點的正確方法是按照您所做的那樣單擊「已接受」複選框。 – KatieK

回答

0

如果它們在jar中,您可能需要將它們加載爲類路徑資源。請參閱:

getClass().getClassLoader().getResourceAsStream(...) 
0

看看我的老代碼,這可能連工作

ByteBuffer buf = null; 
    try{ 
    texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/" + key + ".png")); 
    int texId = texture.getTextureID(); 
    buf = ByteBuffer.allocateDirect(4*texture.getImageWidth()*texture.getImageHeight()); 
    }catch(IOException e){ 
     e.printStackTrace(); 
    } 

    // Create a new texture object in memory and bind it 
    //int texId = GL11.glGenTextures(); - if the trick with texture.getTextureID() won't work - use THIS 
    GL13.glActiveTexture(GL13.GL_TEXTURE0); 
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId); 

    // All RGB bytes are aligned to each other and each component is 1 byte 
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1); 

    // Upload the texture data and generate mip maps (for scaling) 
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, texture.getImageWidth(), texture.getImageHeight(), 0, 
    GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf); 

    //then you can generate some mipmaps 

    //Setup the ST coordinate system 
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE); 
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE); 

    // Setup what to do when the texture has to be scaled 
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); 
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST_MIPMAP_LINEAR); 

而你所需要的東西是texId

我希望你知道如何使用紋理;)。

0

我所做的是我在NetBeans項目中創建了一個名爲的源文件夾,當我創建一個.jar文件時,我使用它與JarSplice一起加載了lib和native。然後我用winrar打開新的.jar文件,然後將源文件拖入.jar文件中,每次運行.jar文件時都會使用jar文件夾。源是像res一樣的東西,但我重命名它。

所以基本上把你的res文件夾放到.jar中,它就會工作。