Mini Appletviewer : Applet « Development « Java Tutorial






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;
  }
}








6.28.Applet
6.28.1.Applet
6.28.2.A real applet
6.28.3.Add JButton to JApplet
6.28.4.Access applet parameters
6.28.5.An Swing-based applet skeleton
6.28.6.Swing Applet
6.28.7.A simple Swing-based applet
6.28.8.Mini Appletviewer
6.28.9.Tic Tac Toe game
6.28.10.WeatherWizard JApplet
6.28.11.Change an applet background color
6.28.12.Read an applet parameters
6.28.13.Calling methods of an Applet from JavaScript code
6.28.14.Passing Parameters to Java Applet
6.28.15.Display message in browser status bar
6.28.16.System properties accessible to applets