问题
So I made a pretty big game in Java, and I intended to put it on my site so I messed with it and turned it into an applet. It launches and runs perfectly fine, the only problem is it wont load any of the pictures. Here is the way I am doing it currently: (Keep in mind this is a JPanel extended class that I create an object of in my applet class and add to the applet)
In the Screen(JPanel) class:
for (int i = 0; i < tileset_ground.length; i++) {
tileset_ground[i] = frame.loadImage("res/tileset_ground.png");
tileset_ground[i] = createImage(new FilteredImageSource(tileset_ground[i].getSource(), new CropImageFilter(0, 26 * i, 26, 26)));
}
In the frame(applet) class:
public Image loadImage(String url){
return getImage(getCodeBase(), url);
}
回答1:
I found that when I was trying to load images from a jar file or using an applet I needed to have more information that just the /folder/file.name. I normally use this method now:
This is for setting the Icon of a label or something else.
jLabel.setIcon(ImageIcon(getClass().getResource("/Folder/File.name")));
If I wanted to return the image I would use this:
Image image = ImageIcon(getClass().getResource("/Folder/File.name")).getImage();
There are better ways to do it but I found this was easy and good enough.
来源:https://stackoverflow.com/questions/13771352/converted-java-game-to-applet-wont-load-pictures