Is there a simple way of detecting whether or not your SimpleApplication is running as an applet (via AppletHarness) or as an application? This is important when you want to open a web page from within a program for example.
Detection of the context could be handled by subclassing your SimpleApplication into two classes, only one of which would be passed to the AppletHarness - so this is good. However, how can you grab the Applet context from within your SimpleApplication?
Set a global variable from the applet harness?
Thanks @normen, however, I’ve got this one working as follows:
pre type="php"
// This is your SimpleApplication, possible “this”
Application app = …
// Now find out if we are in the applet or application
Applet a = AppletHarness.getApplet(app);
if (a != null) {
// Do the applet stuff on your applet “a”
} else {
// Do the application stuff on “System” and “Runtime”
}/pre
Please post your solutions if you have any…
No problems. My particular problems was to open a URL regardless of whether this is in the applet or application context. Here is a solution which relies on the AppletHarness keeping a static final HashMap cross-referencing applications and the applets. Here it is:
pre type="php"
package com.your.package;
import com.jme3.app.Application;
import com.jme3.app.AppletHarness;
import java.applet.Applet;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JOptionPane;
public class BrowserLauncher {
private static final String errMsgBrowser = “Error attempting to launch web browser”;
private static final String errMsgURL = “Error attempting to use bad URL”;
public static void openURL(String urlStr, Application app) {
String osName = System.getProperty(“os.name”);
Applet a = AppletHarness.getApplet(app);
if (a != null) {
try {
URL url = new URL(urlStr);
a.getAppletContext().showDocument(url, “_new”);
} catch (MalformedURLException e) {
JOptionPane.showMessageDialog(null, errMsgURL + “:n” + e.getLocalizedMessage());
}
} else {
try {
if (osName.startsWith(“Mac OS”)) {
Class fileMgr = Class.forName(“com.apple.eio.FileManager”);
Method openURL = fileMgr.getDeclaredMethod(“openURL”,
new Class[] {String.class});
openURL.invoke(null, new Object[] {urlStr});
}
else if (osName.startsWith(“Windows”))
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + urlStr);
else { //assume Unix or Linux
String[] browsers = {
“firefox”, “opera”, “konqueror”, “epiphany”, “mozilla”, “netscape”, “chrome”};
String browser = null;
for (int count = 0; count < browsers.length && browser == null; count++)
if (Runtime.getRuntime().exec(
new String[] {“which”, browsers[count]}).waitFor() == 0)
browser = browsers[count];
if (browser == null)
throw new Exception(“Could not find web browser”);
else
Runtime.getRuntime().exec(new String[] {browser, urlStr});
}
}catch (Exception e) {
JOptionPane.showMessageDialog(null, errMsgBrowser + “:n” + e.getLocalizedMessage());
}
}
}
}/pre
I meant without asking, just like the 3rd post minus my post before