0

我有一个问题,当我创建一个可运行的 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();
4

1 回答 1

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();}
于 2012-08-17T18:53:30.890 回答