I am having a problem that when I create a runnable jar file that nothing is loading on the screen. After some research, I believe the problem is to do with how I am loading the images but I am not sure of the solution to fix that.
The code below is just a snippet of how I load the images.
Thank you~
public class Screen extends JPanel implements Runnable {
public Thread thread = new Thread(this);
public static Image[] tileset_ground = new Image[100];
public static Image[] tileset_air = new Image[100];
public static Image[] tileset_res = new Image[100];
public static Image[] tileset_mob = new Image[100];
public static Image[] tileset_characters = new Image[10];
public static int myWidth, myHeight;
public static int coinage, health;
public static int randomMob = 0;
public static int level = 1, maxLevel = 5;
public static int killed = 0;
public static int killsToWin = 0;
public static int winTime = 4000, winFrame = 0;
public int mobsCreated = 0;
public static boolean isFirst = true;
public static boolean isDebug = false;
public static boolean isWin = false;
public static boolean isMainMenu = true;
public static boolean exit = false;
public static boolean clicked = false;
public static Point mse = new Point(0,0);
public static Room room;
public static Save save;
public static Store store;
public static MainMenu mainMenu;
public static Mob[] mobs = new Mob[100];
public Screen(Frame frame) {
frame.addMouseListener(new KeyHandle());
frame.addMouseMotionListener(new KeyHandle());
thread.start();
}
public void hasWon(){
if(killsToWin == killed){
isWin = true;
killed = 0;
coinage = 10;
}
}
public void define() {
room = new Room();
save = new Save();
store = new Store();
coinage = Values.coinage;
health = Values.health;
for(int i =0; i<tileset_ground.length; i++) {
tileset_ground[i] = new ImageIcon("res/tileset_ground.png").getImage();
tileset_ground[i] = createImage(new FilteredImageSource(tileset_ground[i].getSource(), new CropImageFilter(0,26*i,26,26)));
}
for(int i =0; i<tileset_air.length; i++) {
tileset_air[i] = new ImageIcon("res/tileset_air.png").getImage();
tileset_air[i] = createImage(new FilteredImageSource(tileset_air[i].getSource(), new CropImageFilter(0,26*i,26,26)));
}
tileset_res[0] = new ImageIcon("res/cell.png").getImage();
tileset_res[1] = new ImageIcon("res/coin.png").getImage();
tileset_res[2] = new ImageIcon("res/heart.png").getImage();
tileset_res[3] = new ImageIcon("res/startbutton.png").getImage();
tileset_res[4] = new ImageIcon("res/quitbutton.png").getImage();
tileset_res[5] = new ImageIcon("res/cellchar.png").getImage();
tileset_res[6] = new ImageIcon("res/mainmenu.png").getImage();
tileset_mob[0] = new ImageIcon("res/pixelcody.png").getImage();
tileset_mob[1] = new ImageIcon("res/floatingmob1.png").getImage();
tileset_mob[10] = new ImageIcon("res/pixelraghev.png").getImage();
tileset_characters[1] = new ImageIcon("res/pixelcody.png").getImage();
tileset_characters[2] = new ImageIcon("res/pixelraghev.png").getImage();
tileset_characters[4] = new ImageIcon("res/pixelbio.png").getImage();
tileset_characters[3] = new ImageIcon("res/pixelpeter.png").getImage();
tileset_characters[5] = new ImageIcon("res/pixelkat.png").getImage();
tileset_characters[7] = new ImageIcon("res/pixelvicky.png").getImage();
tileset_characters[6] = new ImageIcon("res/pixelkyle.png").getImage();
This is probably due to an exception being thrown on startup.
In order to load from a .jar, you must modify your code slightly. For your ImageIcon constructors, you need to change them.
Be sure to import ImageIO:
import javax.imageio.ImageIO;
However, due to the fact that you are using arrays of Images, I would recommend simply using ImageIO.read() without messing with using ImageIcons. Also, in my experience, I have always had to preface the path to the image with an additional “/”.
This should work.
One way I try to see if something is going wrong is put a try…catch around the entire program and catch Exception. I then print the stack trace to a file.