Reputation:
I have a simple game which is running as a JApplet. When I test it in Eclipse, it runs perfectly fine. However, when I export it as an applet and try to test it in a browser, it can't find the images. The folder structure inside the jar file is as follows:
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:
<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:
@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:
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. 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.
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.
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. I have played around with various methods of retrieving images (including getClass().getClassLoader().getRescource()), setting the code base, etc., but to no avail. Any suggestions?
Upvotes: 0
Views: 692
Reputation: 168795
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:
img = ImageIO.read(this.getClass().getResource("/img/" + s + ".png"));
Upvotes: 1