1 /* 2 ******************************************************************************* 3 * Copyright (C) 1996-2010, International Business Machines Corporation and * 4 * others. All Rights Reserved. * 5 ******************************************************************************* 6 */ 7 package com.ibm.icu.dev.demo.impl; 8 import java.applet.Applet; 9 import java.applet.AppletContext; 10 import java.applet.AppletStub; 11 import java.applet.AudioClip; 12 import java.awt.Frame; 13 import java.awt.Image; 14 import java.awt.event.WindowAdapter; 15 import java.awt.event.WindowEvent; 16 import java.io.IOException; 17 import java.io.InputStream; 18 import java.net.URL; 19 import java.util.Enumeration; 20 import java.util.Iterator; 21 22 /** 23 * <p>A Frame that runs an Applet within itself, making it possible 24 * for an applet to run as an application. Usage: 25 * 26 * <pre> 27 * public class MyApplet extends Applet { 28 * public static void main(String args[]) { 29 * MyApplet applet = new MyApplet(); 30 * new AppletFrame("My Applet Running As An App", applet, 640, 480); 31 * } 32 * ... 33 * } 34 * <pre> 35 * 36 * @author Alan Liu 37 */ 38 public class AppletFrame extends Frame implements AppletStub, AppletContext { 39 40 /** 41 * For serialization 42 */ 43 private static final long serialVersionUID = 818828281190757725L; 44 Applet applet; 45 46 /** 47 * Construct a Frame running the given Applet with the default size 48 * of 640 by 480. 49 * When the Frame is closed, the applet's stop() method is called, 50 * the Frame is dispose()d of, and System.exit(0) is called. 51 * 52 * @param name the Frame title 53 * @param applet the applet to be run 54 */ AppletFrame(String name, Applet applet)55 public AppletFrame(String name, Applet applet) { 56 this(name, applet, 640, 480); 57 } 58 59 /** 60 * Construct a Frame running the given Applet with the given size. 61 * When the Frame is closed, the applet's stop() method is called, 62 * the Frame is dispose()d of, and System.exit(0) is called. 63 * 64 * @param name the Frame title 65 * @param applet the applet to be run 66 * @param width width of the Frame 67 * @param height height of the Frame 68 */ AppletFrame(String name, Applet applet, int width, int height)69 public AppletFrame(String name, Applet applet, int width, int height) { 70 super(name); 71 this.applet = applet; 72 applet.setStub(this); 73 74 setSize(width, height); 75 add("Center", applet); 76 show(); 77 addWindowListener(new WindowAdapter() { 78 public void windowClosing(WindowEvent e) { 79 AppletFrame.this.applet.stop(); 80 dispose(); 81 System.exit(0); 82 } 83 }); 84 85 applet.init(); 86 applet.start(); 87 } 88 89 // AppletStub API appletResize(int width, int height)90 public void appletResize(int width, int height) { 91 setSize(width, height); 92 } 93 getAppletContext()94 public AppletContext getAppletContext() { 95 return this; 96 } 97 getCodeBase()98 public URL getCodeBase() { 99 return null; 100 } 101 getDocumentBase()102 public URL getDocumentBase() { 103 return null; 104 } 105 getParameter(String name)106 public String getParameter(String name) { 107 return "PARAMETER"; 108 } 109 isActive()110 public boolean isActive() { 111 return true; 112 } 113 114 115 // AppletContext API getApplet(String name)116 public Applet getApplet(String name) { 117 return applet; 118 } 119 getApplets()120 public Enumeration getApplets() { 121 return null; 122 } 123 getAudioClip(URL url)124 public AudioClip getAudioClip(URL url) { 125 return null; 126 } 127 getImage(URL url)128 public Image getImage(URL url) { 129 return null; 130 } 131 showDocument(URL url)132 public void showDocument(URL url) {} showDocument(URL url, String target)133 public void showDocument(URL url, String target) {} 134 showStatus(String status)135 public void showStatus(String status) { 136 System.out.println(status); 137 } 138 setStream(String key, InputStream stream)139 public void setStream(String key, InputStream stream) throws IOException { 140 } 141 getStream(String key)142 public InputStream getStream(String key) { 143 return null; 144 } 145 getStreamKeys()146 public Iterator getStreamKeys() { 147 return null; 148 } 149 } 150