Mega Code Archive

 
Categories / Java Tutorial / Development
 

Mini Appletviewer

import java.applet.Applet; import java.applet.AppletContext; import java.applet.AppletStub; import java.applet.AudioClip; import java.awt.BorderLayout; import java.awt.Image; import java.awt.Toolkit; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLClassLoader; import java.util.Enumeration; import java.util.Iterator; import javax.swing.JFrame; public class MinAppletviewer {   public static void main(String args[]) throws Exception {     AppSupport theAppSupport = new AppSupport();     JFrame f = new JFrame();     URL toload = new URL(args[0]);     String host = toload.getHost();     int port = toload.getPort();     String protocol = toload.getProtocol();     String path = toload.getFile();     int join = path.lastIndexOf('/');     String file = path.substring(join + 1);     path = path.substring(0, join + 1);     theAppSupport.setCodeBase(new URL(protocol, host, port, path));     theAppSupport.setDocumentBase(theAppSupport.getCodeBase());     URL[] bases = { theAppSupport.getCodeBase() };     URLClassLoader loader = new URLClassLoader(bases);     Class theAppletClass = loader.loadClass(file);     Applet theApplet = (Applet) (theAppletClass.newInstance());     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     f.add(theApplet, BorderLayout.CENTER);     theApplet.setStub(theAppSupport);     f.setSize(200, 200);     f.setVisible(true);     theApplet.init();     theApplet.start();   } } class AppSupport implements AppletStub, AppletContext {   private URL codeBase;   private URL documentBase;   public AppSupport() throws Exception {     URL url = null;     String urlpart = System.getProperty("user.dir");     urlpart = urlpart.replace(File.separatorChar, '/');     url = new URL("file://" + urlpart + "/");     codeBase = documentBase = url;   }   public void appletResize(int w, int h) {   }   public AppletContext getAppletContext() {     return (AppletContext) this;   }   public URL getDocumentBase() {     return documentBase;   }   public void setDocumentBase(URL url) {     documentBase = url;   }   public URL getCodeBase() {     return codeBase;   }   public void setCodeBase(URL url) {     codeBase = url;   }   public String getParameter(String s) {     return null;   }   public boolean isActive() {     return true;   }   public Applet getApplet(String name) {     return null;   }   public Enumeration getApplets() {     return new Enumeration() {       public boolean hasMoreElements() {         return false;       }       public Object nextElement() {         return null;       }     };   }   public AudioClip getAudioClip(URL url) {     return Applet.newAudioClip(url);   }   public Image getImage(URL url) {     return Toolkit.getDefaultToolkit().getImage(url);   }   public void showDocument(URL url) {   }   public void showDocument(URL url, String target) {   }   public void showStatus(String status) {     System.err.println(status);   }   public void setStream(String key, InputStream stream) throws IOException {   }   public InputStream getStream(String key) {     return null;   }   public Iterator<String> getStreamKeys() {     return null;   } }