Using/Implementing AppletContext

java packages » java.applet
Java Example Program / Sample Source Code
import java.applet.Applet;
import java.applet.AppletContext;
import java.applet.AppletStub;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Hashtable;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;

public class AppletStubInterface implements AppletStub {

private Hashtable<String, String> hashtable;
private Applet applet;
private AppletContextExample context;

AppletStubInterface
(String[] args) {
context = new AppletContextExample();
}

public AppletStubInterface(String args[], Applet a) {
applet = a;
hashtable =
new Hashtable<String, String>();
for (int i = 0; i < args.length; i++) {
try {
StringTokenizer parser = new StringTokenizer(args[i], "=");
String name = parser.nextToken
().toString();
String value = parser.nextToken
("\"").toString();
value = value.substring
(1);
hashtable.put
(name, value);
} catch (NoSuchElementException e) {
e.printStackTrace();
}
}
}

public void appletResize(int width, int height) {
applet.resize(width, height);
}

public AppletContext getAppletContext() {
return context;
}

public java.net.URL getCodeBase() {
String host;
if ((host = getParameter("host")) == null) {
try {
host = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
java.net.URL url = null;
try {
url = new java.net.URL("file://" + host);
} catch (Exception e) {
}
return url;
}

public java.net.URL getDocumentBase() {
return getCodeBase();
}

public String getParameter(String param) {
return (String) hashtable.get(param);
}

public boolean isActive() {
return true;
}
}
import java.applet.Applet;
import java.applet.AppletContext;
import java.applet.AudioClip;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Iterator;

public class AppletContextExample implements AppletContext {
public AudioClip getAudioClip(URL url) {
return Applet.newAudioClip(url);
}

public Image getImage(URL url) {
Toolkit toolKit = Toolkit.getDefaultToolkit();
return toolKit.getImage(url);
}

public Applet getApplet(String name) {
return null;
}

public Enumeration<Applet> getApplets() {
return null;
}

public void showDocument(URL url) {
System.out.println("Showing document " + url);
}

public void showDocument(URL url, String target) {
try {
showDocument(new URL(url.toString() + target));
} catch (MalformedURLException e) {
}
}

public void showStatus(String status) {
System.out.println(status);
}

public void setStream(String key, InputStream stream) throws IOException {
}

public InputStream getStream(String key) {
return null;
}

public Iterator<String> getStreamKeys() {
return null;
}
}
Html Code
<html>
    <head>
        <title>ImplementAppletContext</title>
    </head>
    <body>
        <applet code="ImplementAppletContext" width="700" height="500"></applet>
    </body>
</html>

Applet


AppletContext

2011-2012 JavaExamples.org Privacy Policy Powered by Google App Engine