Loading Image in Java Applet

我只是一个虾纸丫 提交于 2019-12-20 03:11:45

问题


When I try to run an applet in applet viewer it is not able to find resources (Image). I try to load resource like this:

String cb= this.getCodeBase().toString();
String imgPath = cb+"com/blah/Images/a.png";
System.out.println("imgPath:"+imgPath);
java.net.URL imgURL = Applet.class.getResource(path);

but when i run it in appet viewer path is like this: imgPath:file:D:/Work/app/build/classes/com/blah/Images/a.png

though image is there in this path, is prefix file: causing problem, how can i test this code?

Will this code work when deployed in server and codebase returns a server URL?


回答1:


Is your applet supposed to load images after it is loaded? Or would you be better served bundling necessary image resources in the jar with your applet?

I work daily on an applet-based application with plenty of graphics in the GUI. They are bundled in the jar-file. This si what we do:

// get the class of an object instance - any object.  
// We just defined an empty one, and did everything as static.
class EmptyClass{}
Class loadClass = new EmptyClass().getClass();
// load the image and put it directly into an ImageIcon if it suits you
ImageIcon ii = new ImageIcon(loadClass.getResource("/com/blah/Images/a.png"));
// and add the ImageIcon to your JComponent or JPanel in a JLabel
aComponent.add(new JLabel(ii)); 

Make sure your image is actuallly in the jar where you think it is. Use:

jar -tf <archive_file_name>

... to get a listing.




回答2:


Just use /com/blah/Images/a.png as the path. getResource() is clever enough to find it.




回答3:


The context classloader should work with jars.

ClassLoader cl = Thread.getContextClassLoader();
ImageIcon icon = new ImageIcon(cl.getResource("something.png"), "description");



回答4:


Try this code it's only 2 methods out of the class I use to load images but it works fine for loading when using an applet.

private URL getURL(String filename) {
    URL url = null;
    try 
    {
        url = this.getClass().getResource("" + extention + filename); //extention isn't needed if you are loading from the jar file normally. but I have it for loading from files deeper within my jar file like say. gameAssets/Images/ 
    }
    //catch (MalformedURLException e) { e.printStackTrace(); }
    catch (Exception e) { }

    return url;
}

//observerwin in this case would be an applet. Simply have the class have something like this: Applet observerwin

public void load(String filename) {

    Toolkit tk = Toolkit.getDefaultToolkit();
    image = tk.getImage(getURL(filename));
    while(getImage().getWidth(observerwin) <= 0){loaded = false;}
    double x = observerwin.getSize().width/2  - width()/2;
    double y = observerwin.getSize().height/2 - height()/2;
    at = AffineTransform.getTranslateInstance(x, y);
    loaded = true;
}

I can post the rest of the class I use if needed



来源:https://stackoverflow.com/questions/1763053/loading-image-in-java-applet

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