using pngs which are inside a single jar file

Started by Dano, March 22, 2011, 00:11:00

Previous topic - Next topic

Dano

yo,

so ive got an applet and have managed to deploy a basic one, but when i try and put a png inside the jar file and load that it crashes.

ive had alook around the forum and have found sum info on it but none of it work for me. I have tryed using getResource() and getResourceAsStream() but both didnt work.(These are the methods that were found on the forum)

Could someone pls explain how loading a png from a jar works, like how to find the path dynamically or even what the path would be if ive got my image in a package called images, and how to get it out of the jar.

any help would be much appreciated as im stumped.

thanks, dano

CodeBunny

Please be more specific. Include your code to load images, include a description of the error, and since it sounds like you might have an unusual .jar file / package setup, please describe that.

Loading a .png file (or any other supported format) from a jar should work perfectly well - I do it in my own code, and accessing the image is the same as if you wanted to access it for, say, a BufferedImage.

Dano

Here is my code for loading in the textures, two methods here, one is commented out. both work fine in netbeans but when I Build and try it in a browser is crashes java. any ideas?

oh, i use an if statement to cut out the file: bit of the path cause when in netbeans there is no file: at the start but when built and deployed in a browser the path has file: at the start.

package setup in netbeans:

source packages-
                     images-
                               man.png
                     lwjglapplettest-
                                applet.class
                                textureloader.class

when in jar:

metainf - random crap
lwjglapplettest - classes in
images - just man.png




this code is called after gl has been set up.

public void create() {


        URL url = getClass().getClassLoader().getResource("images/man.png");

        String path = url.getPath();

        if (path.startsWith("file:")) {
            System.out.println("has file infront of path, taken out!");
            path = path.substring(5);
            System.out.println(path);
        }

        TextureLoader texLoad = new TextureLoader(path);
        texID = texLoad.setupTexture();



        // InputStream in = getClass().getClassLoader().getResourceAsStream("images/man.png");
        // TextureLoader texLoad = new TextureLoader(in);
        // texID = texLoad.setupTexture();


    }


Heres the texture loader class that im using aswell if that helps, im using PNGDecoder by the way.

public class TextureLoader {
    
    InputStream in;
    PNGDecoder decoder;
    
    public TextureLoader(){
    
    }
    
    public TextureLoader(String filename){
        try {
            in = new FileInputStream(filename);
            decoder = new PNGDecoder(in);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(TextureLoader.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException e) {
           System.out.println("Error decoding " + filename);
        }
        
    }

    public TextureLoader(InputStream in){
        try {

            decoder = new PNGDecoder(in);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(TextureLoader.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException e) {
           System.out.println("Error decoding ");
        }

    }

    
     public int setupTexture() {
        IntBuffer tmp = BufferUtils.createIntBuffer(1);
        GL11.glGenTextures(tmp);
        tmp.rewind();
        
        try {
            
   
   ByteBuffer buf = ByteBuffer.allocateDirect(4*decoder.getWidth()*decoder.getHeight());
   decoder.decode(buf, decoder.getWidth()*4, Format.RGBA);
   
   buf.flip();
   
   
GL11.glBindTexture(GL11.GL_TEXTURE_2D, tmp.get(0));
         
           GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER,
                    GL11.GL_NEAREST);
          
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);
           GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 4);
            

  

} catch (FileNotFoundException ex) {
            System.out.println("Error  not found");
        } catch (IOException e) {
           System.out.println("Error decoding ");
        }
    
            
           

        tmp.rewind();
        return tmp.get(0);
    }
    
}


so yeah im stumped, Any help?

CodeBunny

Hmm. I'm not an applet guy, and I haven't used PNGDecoder, so I don't know what to tell you. Can you print the error?

Are you sure LWJGL is active in the applet?

BTW: You might want to make your setupTexture() call static; pass it a String or InputStream and let the method do the work for you. That way you don't have to make a new instance of your texture loader every time you want to load an image. E.g.:

int texID = TextureLoader.loadTexture("images/man.png");


See? Easier and faster.

Matthias

Forget about using FileInputStream or similar hacks. Just call URL.openStream() - and don't forget to close it.

Dano

so have made texture loader static and used the url.openstream() (and closed it lol) but neither have changed much apart from just being better ways of doing it. im really stuck here as dont see why it works fine in netbeans but then when i build and put in browser it crashes java, so no error message comes up either. have put some system outs in to see how far it gets in the browser and it crashes when its trying to open the stream, only thing i can think of is the applet doesnt have permissions to access file, dont think this is the case but i dont know much about applets so it might be.

as for lwjgl being active in the window, i think it is as have drawn quads when just setting up a basic applet but again not overly sure as am running on a different thread as explained in http://www.lwjgl.org/wiki/index.php?title=Basic_LWJGL_Applet which i have used to set up the basic applet i am working from.

Could someone just show me some code that they know works so i can try that out to see if its another part of my code that might be doing it.

thanks,

dano

kappa

do you get any output in the java console when running in the browser? some exception or something?

Dano

no exception, just crashes java.

just noticed that you wrote pngdecoder, Matthias (thats if u are the same Matthias). good job, been using it for desktop version of my game, works a treat. Altho am thinking it might be the problem im having with this applet as it doesnt get passed creating an instance of it. any idea y?

the only thing that i can see thats different between the run in netbeans and in a browser is that the url returned in browser has "file:" at the start of it


jediTofu

Quote from: Dano on March 22, 2011, 14:41:13
Here is my code for loading in the textures, two methods here, one is commented out. both work fine in netbeans but when I Build and try it in a browser is crashes java. any ideas?

oh, i use an if statement to cut out the file: bit of the path cause when in netbeans there is no file: at the start but when built and deployed in a browser the path has file: at the start.

package setup in netbeans:

source packages-
                     images-
                               man.png
                     lwjglapplettest-
                                applet.class
                                textureloader.class

when in jar:

metainf - random crap
lwjglapplettest - classes in
images - just man.png




this code is called after gl has been set up.

public void create() {


        URL url = getClass().getClassLoader().getResource("images/man.png");

        String path = url.getPath();

        if (path.startsWith("file:")) {
            System.out.println("has file infront of path, taken out!");
            path = path.substring(5);
            System.out.println(path);
        }

        TextureLoader texLoad = new TextureLoader(path);
        texID = texLoad.setupTexture();



        // InputStream in = getClass().getClassLoader().getResourceAsStream("images/man.png");
        // TextureLoader texLoad = new TextureLoader(in);
        // texID = texLoad.setupTexture();


    }


Heres the texture loader class that im using aswell if that helps, im using PNGDecoder by the way.

public class TextureLoader {
    
    InputStream in;
    PNGDecoder decoder;
    
    public TextureLoader(){
    
    }
    
    public TextureLoader(String filename){
        try {
            in = new FileInputStream(filename);
            decoder = new PNGDecoder(in);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(TextureLoader.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException e) {
           System.out.println("Error decoding " + filename);
        }
        
    }

    public TextureLoader(InputStream in){
        try {

            decoder = new PNGDecoder(in);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(TextureLoader.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException e) {
           System.out.println("Error decoding ");
        }

    }

    
     public int setupTexture() {
        IntBuffer tmp = BufferUtils.createIntBuffer(1);
        GL11.glGenTextures(tmp);
        tmp.rewind();
        
        try {
            
   
   ByteBuffer buf = ByteBuffer.allocateDirect(4*decoder.getWidth()*decoder.getHeight());
   decoder.decode(buf, decoder.getWidth()*4, Format.RGBA);
   
   buf.flip();
   
   
GL11.glBindTexture(GL11.GL_TEXTURE_2D, tmp.get(0));
         
           GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER,
                    GL11.GL_NEAREST);
          
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);
           GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 4);
            

  

} catch (FileNotFoundException ex) {
            System.out.println("Error  not found");
        } catch (IOException e) {
           System.out.println("Error decoding ");
        }
    
            
           

        tmp.rewind();
        return tmp.get(0);
    }
    
}


so yeah im stumped, Any help?



It looks in the package of the class.  So you probably want your files like this:

source packages-
                     lwjglapplettest-
                                applet.class
                                textureloader.class
                                images-
                                          man.png

This shouldn't matter, but you can also just do "getClass().getResource("images/man.png");" without ".getClassLoad()".

Also, you'll probably need to use getResourceAsStream() instead, as you have commented.
cool story, bro