dimanche 1 mars 2015

Referencing Pictures from resource folder in a Jar

I can't help it but think I've missed just the Thread to answer my question but I've been looking for a long time now and can't seem to find the help I need.


My problem is quite simple: I've created a game (or rather I'm in the process of it) and I'm trying to add sprites (png files for the enemies and such) to it. Loading them in the IDE works just fine but when I create a jar file and try to open it, it simply says "A Java Exception has occurred". Further investigation revealed the problem is that it can't find the files I told it to load.


Through the threads I've read I gathered this much: It's either that I'm not loading the images properly, meaning that I don't use the proper code for it, or my MANIFEST.mf does not contain a "Class-Path" (meaning that the arguments are blank, which in fact they are)


Finding other code didn't work out for me. People suggested to use a ClassLoader which I could't manage to get working. Trying it out gave me nullpointer exceptions.


Trying to look into the latter didn't do much help, because I couldn't find any helping information, or I'm just not understanding anything.



package platformer.resources;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.*;

public class ImageLoader
{
static String spritePath = "src/platformer/resources/";

public static BufferedImage loadImage(String path)
{
BufferedImage img = null;

try{img= ImageIO.read(new File(spritePath+path));
}catch(IOException e){System.out.println("Couldn't load File: "+spritePath+path);}

return img;
}

public static BufferedImage flipImage(BufferedImage bufferedImage)
{
AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
tx.translate(-bufferedImage.getWidth(null), 0);
AffineTransformOp op = new AffineTransformOp(tx,
AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
bufferedImage = op.filter(bufferedImage, null);

return bufferedImage;
}
}


This is the code I used so far. Other classes would use "loadImage" or "flipImage" and in the IDE it works fine but as a .jar it fails to find the .png.


For example the class "platformer/entities/walker.class" would try to open the "Idle.png". In order to do that it uses the "loadImage("/entities/enemy/walker/Idle.png")" method. Note that in the method the actual path would end up being "src/platformer/resources/entities/enemy/walker/Idle.png".


Again, I'm terribly sorry if this has already been answered but I appreciate your help nonetheless.


Michael


Aucun commentaire:

Enregistrer un commentaire