Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/eireneh/swing/AppletFrame.java


1   
2   package com.eireneh.swing;
3   
4   import java.awt.event.*;
5   import java.awt.*;
6   import java.applet.*;
7   import java.net.URL;
8   import java.util.Enumeration;
9   
10  import javax.swing.JFrame;
11  
12  /**
13  * This class simplifies running Applets as applications
14  * It mirrors the actions of a Browser in an application.
15  * The methods without specific JavaDoc comments mirror the methods
16  * of AppletStub and AppletContext, returning null, true, this or ""
17  * where appropriate.
18  * TODO: Maybe it is possible to have main in here using a static
19  * method to find the applet object... Maybe not.
20  * 
21  * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
22  * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
23  * Distribution Licence:<br />
24  * Project B is free software; you can redistribute it
25  * and/or modify it under the terms of the GNU General Public License,
26  * version 2 as published by the Free Software Foundation.<br />
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30  * General Public License for more details.<br />
31  * The License is available on the internet
32  * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
33  * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
34  * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
35  * The copyright to this program is held by it's authors.
36  * </font></td></tr></table>
37  * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
38  * @see docs.Licence
39  * @author Joe Walker
40  */
41  public class AppletFrame extends JFrame
42  {
43      /**
44      * Creates a Frame and runs an Applet in the frame.
45      * Mirrors the actions of a Browser in an application.
46      * @param name The text that should appear in the title bar.
47      * @param app The Applet that we should run in the new frame.
48      * @param width The horizontal size of the frame.
49      * @param height The vertical size of the frame.
50      */
51      public AppletFrame(String name, Applet app, int width, int height)
52      {
53          addWindowListener(new WindowAdapter() {
54              public void windowClosing(WindowEvent e)    { close(); }
55          });
56  
57          app.setStub(new AppletFrameStub());
58          app.init();
59          app.start();
60  
61          /*getContentPane().setLayout(new BorderLayout()); */
62          getContentPane().add("Center", app);
63  
64          setTitle(name);
65          setSize(width, height);
66          setVisible(true);
67      }
68  
69      /**
70      * Creates a Frame and runs an Applet in the frame.
71      * The frame is given the default name of the Applet Class.
72      * @param app The Applet that we should run in the new frame.
73      * @param width The horizontal size of the frame.
74      * @param height The vertical size of the frame.
75      */
76      public AppletFrame(Applet app, int width, int height)
77      {
78          this(app.getClass().getName(), app, width, height);
79      }
80  
81      /**
82      * Creates a Frame and runs an Applet in the frame.
83      * The frame is given the default name of the Applet Class.
84      * @param name The text that should appear in the title bar.
85      * @param app The Applet that we should run in the new frame.
86      */
87      public AppletFrame(String name, Applet app)
88      {
89          this(name, app, 100, 100);
90  
91          Dimension x = app.getPreferredSize();
92          setSize(x.width, x.height);
93      }
94  
95      /**
96      * Creates a Frame and runs an Applet in the frame.
97      * The frame is given the default name of the Applet Class.
98      * @param app The Applet that we should run in the new frame.
99      */
100     public AppletFrame(Applet app)
101     {
102         this(app.getClass().getName(), app);
103     }
104 
105     /**
106     * Close everything down and exit from the JVM
107     */
108     public void close()
109     {
110         dispose();
111         System.exit(0);
112     }
113 
114     /**
115     * So that we can be an AppletStub
116     */
117     public class AppletFrameStub implements AppletStub
118     {
119         public boolean isActive()       { return true; }
120         public URL getDocumentBase()        { return null; }
121         public URL getCodeBase()        { return null; }
122         public String getParameter(String name) { return ""; }
123         public AppletContext getAppletContext() { return afc; }
124         public void appletResize(int w, int h)  { }
125 
126         public class AppletFrameContext implements AppletContext
127         {
128             public AudioClip getAudioClip(URL u)    { return null; }
129             public Image getImage(URL u)        { return null; }
130             public Applet getApplet(String name)    { return null; }
131             public Enumeration getApplets()     { return null; }
132             public void showDocument(URL u)     { }
133             public void showDocument(URL u,String t)    { }
134             public void showStatus(String s)        { }
135         }
136 
137         private AppletFrameContext afc = new AppletFrameContext();
138     }
139 
140     /** Are we still running */
141     private boolean dying = false;
142 }