Java by API/java.applet/AppletStub — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 17:43, 31 мая 2010

implements AppletStub

import java.applet.Applet;
import java.applet.AppletContext;
import java.applet.AppletStub;
import java.applet.AudioClip;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
public class MainClass extends Applet implements ActionListener {
  static MainClass myApplet;
  static MyStub myStub;
  Image im;
  public void init() {
    System.out.println("Code base = " + getCodeBase());
    System.out.println("Document base = " + getDocumentBase());
    System.out.println("\ninit () called");
    System.out.println("isActive () returns " + isActive());
    Button b = new Button("Visit www.jexp.ru");
    b.addActionListener(this);
    add(b);
    b = new Button("Audio");
    b.addActionListener(this);
    add(b);
    String imageName = getParameter("image");
    if (imageName != null)
      im = getImage(getCodeBase(), imageName);
  }
  public void start() {
    System.out.println("start () called");
    System.out.println("isActive () returns " + isActive());
  }
  public void paint(Graphics g) {
    if (im != null)
      g.drawImage(im, 0, 0, this);
  }
  public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("Audio")) {
      String soundName = getParameter("audio");
      if (soundName != null) {
        AudioClip ac = getAudioClip(getDocumentBase(), soundName);
        ac.play();
      }
      return;
    }
    try {
      URL u = new URL("http://www.jexp.ru");
      getAppletContext().showDocument(u);
    } catch (MalformedURLException exc) {
      System.out.println(e);
    }
  }
  public void stop() {
    System.out.println("stop () called");
    System.out.println("isActive () returns " + isActive());
  }
  public void destroy() {
    System.out.println("destroy () called");
    System.out.println("isActive () returns " + isActive());
  }
  public static void main(String[] args) {
    Frame frame = new Frame("AppletAndApp as an Application");
    myApplet = new MainClass();
    frame.add(new Panel().add(myApplet));
    frame.addNotify();
    myApplet.setStub(myStub = new MyStub(args));
    myApplet.init();
    frame.setSize(300, 200);
    frame.setVisible(true);
    myStub.setActive(true);
    myApplet.start();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent w) {
        myStub.setActive(false);
        myApplet.stop();
        myApplet.destroy();
        System.exit(0);
      }
    });
  }
}
class MyStub implements AppletStub {
  private boolean active = false;
  private Hashtable ht = new Hashtable();
  private MyContext context;
  MyStub(String[] args) {
    context = new MyContext(); 
    if ((args.length & 1) != 0)
      return;
    for (int i = 0; i < args.length; i += 2)
      ht.put(args[i], args[i + 1]);
  }
  public boolean isActive() {
    return active;
  }
  public URL getDocumentBase() {
    URL u = null;
    try {
      u = new URL("file:/C:./x.html");
    } catch (MalformedURLException e) {
    }
    return u;
  }
  public URL getCodeBase() {
    URL u = null;
    try {
      u = new URL("file:/C:./");
    } catch (MalformedURLException e) {
    }
    return u;
  }
  public String getParameter(String name) {
    return (String) ht.get(name);
  }
  public AppletContext getAppletContext() {
    return context;
  }
  public void appletResize(int width, int height) {
  }
  public void setActive(boolean active) {
    this.active = active;
  }
}
class MyContext implements AppletContext {
  public AudioClip getAudioClip(URL url) {
    return Applet.newAudioClip(url);
  }
  public Image getImage(URL url) {
    Toolkit tk = Toolkit.getDefaultToolkit();
    return tk.getImage(url);
  }
  public Applet getApplet(String name) {
    return null;
  }
  public Enumeration getApplets() {
    return null;
  }
  public void showDocument(URL url) {
    System.out.println("Showing document " + url);
  }
  public void showDocument(URL url, String frame) {
    try {
      showDocument(new URL(url.toString() + frame));
    } catch (MalformedURLException e) {
    }
  }
  public void showStatus(String message) {
    System.out.println(message);
  }
  public void setStream(String key, InputStream stream) throws IOException {
  }
  public InputStream getStream(String key) {
    return null;
  }
  public Iterator<String> getStreamKeys() {
    return null;
  }
}