home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / demo / graphi~1 / applet~1.jav < prev    next >
Encoding:
Text File  |  1995-12-04  |  4.0 KB  |  132 lines

  1. /* Generic Applet to Application Frame
  2.  * @(#)AppletFrame.java    1.4  02 Dec 1995 15:28:07
  3.  * @author Kevin A. Smith
  4.  *
  5.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  6.  *
  7.  * Permission to use, copy, modify, and distribute this software
  8.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  9.  * without fee is hereby granted. 
  10.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  11.  * for further important copyright and trademark information and to
  12.  * http://java.sun.com/licensing.html for further important licensing
  13.  * information for the Java (tm) Technology.
  14.  * 
  15.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  16.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  17.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  18.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  19.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  20.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  21.  * 
  22.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  23.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  24.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  25.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  26.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  27.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  28.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  29.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  30.  * HIGH RISK ACTIVITIES.
  31.  *
  32.  * Directions for use: 
  33.  *
  34.  * In order to convert an applet to an application you must provide
  35.  * two things: A main() function in the class body of the applet
  36.  * and an AWT Frame window for the application to exist in.  If your
  37.  * applet requires parameters from the HTML Applet tag, you will need
  38.  * to do additional work.  
  39.  *
  40.  * Here's a sample main() function that you can add to your applet class:
  41.  *
  42.  *  public static void main(String args[])
  43.  *  {
  44.  *     AppletFrame.startApplet( <ClassName>, "Application Title");
  45.  *  }
  46.  *
  47.  * The class AppletFrame provides a simple AWT Frame window for running
  48.  * applications.  
  49.  *
  50.  */
  51.  
  52. import java.awt.Frame;
  53. import java.awt.Event;
  54. import java.awt.Dimension;
  55. import java.applet.Applet;
  56.  
  57. // Applet to Application Frame window
  58. class AppletFrame extends Frame
  59. {
  60.  
  61.     public static void startApplet(String className, 
  62.                                    String title, 
  63.                                    String args[])
  64.     {
  65.        // local variables
  66.        Applet a;
  67.        Dimension appletSize;
  68.  
  69.        try 
  70.        {
  71.           // create an instance of your applet class
  72.           a = (Applet) Class.forName(className).newInstance();
  73.        }
  74.        catch (ClassNotFoundException e) { return; }
  75.        catch (InstantiationException e) { return; }
  76.        catch (IllegalAccessException e) { return; }
  77.  
  78.        // initialize the applet
  79.        a.init();
  80.        a.start();
  81.   
  82.        // create new application frame window
  83.        AppletFrame f = new AppletFrame(title);
  84.   
  85.        // add applet to frame window
  86.        f.add("Center", a);
  87.   
  88.        // resize frame window to fit applet
  89.        // assumes that the applet sets its own size
  90.        // otherwise, you should set a specific size here.
  91.        appletSize =  a.size();
  92.        f.pack();
  93.        f.resize(appletSize);  
  94.  
  95.        // show the window
  96.        f.show();
  97.   
  98.     }  // end startApplet()
  99.   
  100.   
  101.     // constructor needed to pass window title to class Frame
  102.     public AppletFrame(String name)
  103.     {
  104.        // call java.awt.Frame(String) constructor
  105.        super(name);
  106.     }
  107.  
  108.     // needed to allow window close
  109.     public boolean handleEvent(Event e)
  110.     {
  111.        // Window Destroy event
  112.        if (e.id == Event.WINDOW_DESTROY)
  113.        {
  114.           // exit the program
  115.           System.exit(0);
  116.           return true;
  117.        }
  118.        
  119.        // it's good form to let the super class look at any 
  120.        // unhandled events
  121.        return super.handleEvent(e);
  122.  
  123.     }  // end handleEvent()
  124.  
  125. }   // end class AppletFrame
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.