开发者

how can i run an applet as an application?

I have an Applet class, and I want to make it run as an application, so I wrote to following code:

public static void main(String args[]) {
JFrame app = new JFrame("Applet Container开发者_Python百科");
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(200, 100);
Hangman applet = new Hangman();
applet.init();
app.setLayout(new BorderLayout());
app.setSize(500,500);
app.getContentPane().add(applet, BorderLayout.CENTER);
app.setVisible(true);
}

Note: Hangman is the applet class. And if i run it, it work fine, but what I am tring to do, is to make it run as an application.

When I run the above main, I got the following error:

Exception in thread "main" java.lang.NullPointerException
at java.applet.Applet.getCodeBase(Applet.java:152)
at Me.Hangman.init(Hangman.java:138)
at Me.Client.main(Client.java:54)
Java Result: 1

This error is came from this line in the Hangman class:

danceMusic = getAudioClip(getCodeBase(), "../../audio/dance.au");

GetCodeBase() method returns null, I need help on how can I make this method work properly, or maybe replace it with another method that may access my files to get resources?

Thank you in advance


Applets have a special runtime environment. If you want your code to run also as an application then you can not rely on ANY functionality provided by that environment.

Obviously you are using an Applet-specific function here. You have to search for how to get this done in an application, then detect the environment you are running in and use the appropriate methods.

Edit:

Instead of getCodeBase() I would try:

getClass().getProtectionDomain().getCodeSource().getLocation();

But getAudioClip is also defined in an applet, so that is a no-go too. Instead of java.applet.AudioClip you have to use the javax.sound.sampled API.


Use Hangman.class.getResource() or getResourceAsStream() for loading resources. http://www.java2s.com/Code/JavaAPI/java.lang/ClassgetResourceStringnamerelativetotheclasslocation.htm


I am having a similar problem and came up with the following which works nicely for parameters, codeBase and getImage - I'am still looking for the getAudioClip() part ... it would be great if there was something like ImageIo for Audio ....

// simulate Applet environment
Map<String, String> params = new HashMap<String, String>();
URL documentBase;

/**
 * set the parameter with the given name
 * 
 * @param name
 * @param value
 */
public void setParameter(String name, String value) {
    params.put(name.toUpperCase(), value);
}

@Override
public String getParameter(String name) {
    String result = null;
    if (params.containsKey(name))
        result = params.get(name);
    else {
        try {
            result = super.getParameter(name);
        } catch (java.lang.NullPointerException npe) {
            throw new IllegalArgumentException("parameter " + name + " not set");
        }
    }
    return result;
}

/**
 * @param documentBase
 *          the documentBase to set
 * @throws MalformedURLException
 */
public void setDocumentBase(String documentBase) throws MalformedURLException {
    this.documentBase = new URL(documentBase);
}

@Override
public URL getDocumentBase() {
    URL result = documentBase;
    if (result == null)
        result = super.getDocumentBase();
    return result;
}

@Override
public Image getImage(URL url) {
    Image result = null;
    if (this.documentBase != null) {
        try {
            result = ImageIO.read(url);
        } catch (IOException e) {
        }
    } else {
        result = super.getImage(url);
    }
    return result;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜