home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / plugin / applets / GraphicsTest / AppletFrame.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  3.5 KB  |  115 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)AppletFrame.java    1.11 02/06/13
  38.  */
  39.  
  40. import java.awt.Frame;
  41. import java.awt.Event;
  42. import java.awt.Dimension;
  43. import java.applet.Applet;
  44. import java.awt.AWTEvent;
  45.  
  46. // Applet to Application Frame window
  47. class AppletFrame extends Frame
  48. {
  49.  
  50.     public static void startApplet(String className, 
  51.                                    String title, 
  52.                                    String args[])
  53.     {
  54.        // local variables
  55.        Applet a;
  56.        Dimension appletSize;
  57.  
  58.        try 
  59.        {
  60.           // create an instance of your applet class
  61.           a = (Applet) Class.forName(className).newInstance();
  62.        }
  63.        catch (ClassNotFoundException e) { return; }
  64.        catch (InstantiationException e) { return; }
  65.        catch (IllegalAccessException e) { return; }
  66.  
  67.        // initialize the applet
  68.        a.init();
  69.        a.start();
  70.   
  71.        // create new application frame window
  72.        AppletFrame f = new AppletFrame(title);
  73.   
  74.        // add applet to frame window
  75.        f.add("Center", a);
  76.   
  77.        // resize frame window to fit applet
  78.        // assumes that the applet sets its own size
  79.        // otherwise, you should set a specific size here.
  80.        appletSize =  a.getSize();
  81.        f.pack();
  82.        f.setSize(appletSize);  
  83.  
  84.        // show the window
  85.        f.show();
  86.   
  87.     }  // end startApplet()
  88.   
  89.   
  90.     // constructor needed to pass window title to class Frame
  91.     public AppletFrame(String name)
  92.     {
  93.        // call java.awt.Frame(String) constructor
  94.        super(name);
  95.     }
  96.  
  97.     // needed to allow window close
  98.     public void processEvent(AWTEvent e)
  99.     {
  100.        // Window Destroy event
  101.        if (e.getID() == Event.WINDOW_DESTROY)
  102.        {
  103.           // exit the program
  104.           System.exit(0);
  105.        }    
  106.    }  // end handleEvent()
  107.  
  108. }   // end class AppletFrame
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.