简体   繁体   English

如何从jar文件加载图像?

[英]How do I get images to load from a jar file?

I have a simple game which is running as a JApplet. 我有一个简单的游戏,它以JApplet的形式运行。 When I test it in Eclipse, it runs perfectly fine. 当我在Eclipse中测试它时,它运行得很好。 However, when I export it as an applet and try to test it in a browser, it can't find the images. 但是,当我将其导出为applet并尝试在浏览器中对其进行测试时,它找不到图像。 The folder structure inside the jar file is as follows: jar文件内的文件夹结构如下:

index.html (page that displays the applet)  
shipyard.jar  
_shipyard  
__Shipyard.class (main applet class)  
_img  
__All of my images, saved as .png files  

The HTML code used to display the applet is as follows: 用于显示小程序的HTML代码如下:

<object width="1280" height="720" data="shipyard.jar" type="application/x-java-applet">
    <param name="archive" value="shipyard.jar">
    <param name="code" value="shipyard.Shipyard">
    <param name="align" value="middle">
</object>

My init() and run() methods, which set up the preload queue, then iterate over it to retrieve and prepare the images: 我的init()和run()方法设置了预加载队列,然后对其进行迭代以获取并准备图像:

@Override
public void init(){ 
    markImageForPreload("background");
    markImageForPreload("shipyard_interior");
    markImageForPreload("tooltip");
    markImageForPreload("button");
    for(String s : ShipyardExterior.tilePages) markImageForPreload("tiles/" + s);
    for(ShipSystem sys : ShipSystem.allSystems){
        markImageForPreload("system/interior/" + sys.imgName);
        if(sys.getForceExterior()) markImageForPreload("system/exterior/" + sys.imgName);
    }   
    for(ShipWeapon wep : ShipWeapon.allWeapons){
        markImageForPreload("weapon/" + wep.imgname);
    }

    currentScreen = new LoadingScreen();

    addMouseListener(this);
    addKeyListener(this);
}

@Override
public void run() {

    preloadCount = 0;       
    preloadTotal = preloadQueue.size();

    for(String s : preloadQueue){
        this.prepareImage(getOrLoadImage(s), this);
        preloadCount++;
        repaint();
    }              

    currentScreen = new ShipyardInterior();

    while(true){
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
        try {
            if(this.getMousePosition() != null){
                mouseX = this.getMousePosition().x / 2;
                mouseY = this.getMousePosition().y / 2;
            }
        } catch (NullPointerException e){}
        currentScreen.onUpdate();
        repaint();
        try {
            Thread.sleep(1000 / 60);
        } catch(InterruptedException e) {}

        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    }
}

The method from the JApplet class that loads and retrieves images: JApplet类中的方法,用于加载和检索图像:

public static Image getOrLoadImage(String s){
    Image img = instance.loadedImages.get(s);
    if(img == null){
        System.out.println("Loading " + s + ".png.");
        try {
            img = ImageIO.read(new URL(instance.getCodeBase(), "../img/" + s + ".png"));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(img == null) System.out.println("Image does not exist.");
        else instance.loadedImages.put(s, img);
    }
    return img;
}

"loadedImages" is a static HashMap that stores images by their URLs. “ loadedImages”是一个静态HashMap,它通过其URL存储图像。 When the method is called, it first checks to see if the image is loaded. 调用该方法时,它首先检查是否已加载图像。 If so, it returns the image. 如果是这样,它将返回图像。 If it is not loaded, it does so and adds its Image instance to loadedImages. 如果未加载,则会这样做,并将其Image实例添加到loadedImages。

When I open the page, the applet runs, but the images don't show up. 当我打开页面时,小程序会运行,但是图像不会显示。 Things like drawString(), drawRect(), and other basic render methods work, but not images. 诸如drawString(),drawRect()和其他基本渲染方法之类的东西都可以工作,但是图像则不能。

I have hunted all over the internet and was unable to find anything useful on the subject. 我已经在网上搜寻了很多东西,却找不到任何有用的东西。 One thread told me to self-sign the applet jar, which I did, but that didn't seem to work. 有一个线程告诉我对applet jar进行自签名,我这样做了,但这似乎没有用。 I have played around with various methods of retrieving images (including getClass().getClassLoader().getRescource()), setting the code base, etc., but to no avail. 我玩过各种检索图像的方法(包括getClass()。getClassLoader()。getRescource()),设置代码库等,但都无济于事。 Any suggestions? 有什么建议么?

img = ImageIO.read(new URL(instance.getCodeBase(), "../img/" + s + ".png"));

If I had he time it would be great to hear your reasoning as to why that should work for the described structure. 如果我有他的时间,那么很高兴听到您对为什么这种方法适用于所描述的结构的推理。 AFAIU it should be: AFAIU应该是:

img = ImageIO.read(this.getClass().getResource("/img/" + s + ".png"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM