Converted Java Game to Applet; Wont load pictures

心不动则不痛 提交于 2019-12-20 06:42:41

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!