我遇到的问题是,当我创建一个可运行的jar文件时,屏幕上没有任何内容。经过一些研究,我认为问题与我如何加载图像有关,但我不确定解决方案。
下面的代码只是我如何加载图片的片段。
谢谢〜
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();
答案 0 :(得分:1)
这可能是由于启动时抛出异常。
要从.jar加载,您必须稍微修改代码。对于ImageIcon构造函数,您需要更改它们。
ImageIcon whatever = new ImageIcon(ImageIO.read(getClass().getResource("path to image")));
请务必导入ImageIO: import javax.imageio.ImageIO;
但是,由于您使用的是图像数组,我建议您只使用ImageIO.read()而不必使用ImageIcons。另外,根据我的经验,我总是必须在附加“/".
的前面添加图像路径这应该有效。
我尝试查看是否出现问题的一种方法是尝试...抓住整个程序并捕获异常。然后我将堆栈跟踪打印到文件。
try {
...
}catch(Exception e){
PrintWriter writer = new PrintWriter("trace.txt");
e.printStackTrace(writer);
writer.close();}