KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > AppletFrame


1 /*
2  * @(#)AppletFrame.java 1.16 06/02/22
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)AppletFrame.java 1.16 06/02/22
39  */

40
41 import java.awt.Frame JavaDoc;
42 import java.awt.Event JavaDoc;
43 import java.awt.Dimension JavaDoc;
44 import java.applet.Applet JavaDoc;
45 import java.awt.AWTEvent JavaDoc;
46
47 // Applet to Application Frame window
48
class AppletFrame extends Frame JavaDoc
49 {
50
51     public static void startApplet(String JavaDoc className,
52                                    String JavaDoc title,
53                                    String JavaDoc args[])
54     {
55        // local variables
56
Applet JavaDoc a;
57        Dimension JavaDoc appletSize;
58
59        try
60        {
61           // create an instance of your applet class
62
a = (Applet JavaDoc) Class.forName(className).newInstance();
63        }
64        catch (ClassNotFoundException JavaDoc e) { return; }
65        catch (InstantiationException JavaDoc e) { return; }
66        catch (IllegalAccessException JavaDoc e) { return; }
67
68        // initialize the applet
69
a.init();
70        a.start();
71   
72        // create new application frame window
73
AppletFrame f = new AppletFrame(title);
74   
75        // add applet to frame window
76
f.add("Center", a);
77   
78        // resize frame window to fit applet
79
// assumes that the applet sets its own size
80
// otherwise, you should set a specific size here.
81
appletSize = a.getSize();
82        f.pack();
83        f.setSize(appletSize);
84
85        // show the window
86
f.show();
87   
88     } // end startApplet()
89

90   
91     // constructor needed to pass window title to class Frame
92
public AppletFrame(String JavaDoc name)
93     {
94        // call java.awt.Frame(String) constructor
95
super(name);
96     }
97
98     // needed to allow window close
99
public void processEvent(AWTEvent JavaDoc e)
100     {
101        // Window Destroy event
102
if (e.getID() == Event.WINDOW_DESTROY)
103        {
104           // exit the program
105
System.exit(0);
106        }
107    } // end handleEvent()
108

109 } // end class AppletFrame
110

111
112
113
114
115
116
Popular Tags