home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / MsDev / Samples / Microsoft / Scripting / displaytext.java < prev    next >
Encoding:
Java Source  |  1996-08-27  |  6.7 KB  |  192 lines

  1. //******************************************************************************
  2. // displaytext.java:    Applet
  3. //
  4. //******************************************************************************
  5. import java.applet.*;
  6. import java.awt.*;
  7.  
  8. // This is a Java Applet Wizard-generated sample.
  9. // <<< code added >>> comments mark the changes to the Wizard code.
  10. // Search this file for the string "<<<" to find the modified code.
  11.  
  12. //==============================================================================
  13. // Main Class for applet DisplayText
  14. //
  15. //==============================================================================
  16. public class displaytext extends Applet
  17. {
  18.     // <<< Instance Variables >>>
  19.     // The value of the text to be displayed by the applet
  20.     String m_phrase;
  21.  
  22.     // The origin of the text to be displayed, centered
  23.     int m_x;
  24.     int m_y;
  25.     // <<< end added code >>>
  26.  
  27.     // PARAMETER SUPPORT:
  28.     //        Parameters allow an HTML author to pass information to the applet;
  29.     // the HTML author specifies them using the <PARAM> tag within the <APPLET>
  30.     // tag.  The following variables are used to store the values of the
  31.     // parameters.
  32.     //--------------------------------------------------------------------------
  33.  
  34.     // Members for applet parameters
  35.     // <type>       <MemberVar>    = <Default Value>
  36.     //--------------------------------------------------------------------------
  37.     private String m_initialValue = "Hello World";
  38.  
  39.     // Parameter names.  To change a name of a parameter, you need only make
  40.     // a single change.  Simply modify the value of the parameter string below.
  41.     //--------------------------------------------------------------------------
  42.     private final String PARAM_initialValue = "initialValue";
  43.  
  44.     // DisplayText Class Constructor
  45.     //--------------------------------------------------------------------------
  46.     public displaytext()
  47.     {
  48.         // TODO: Add constructor code here
  49.     }
  50.  
  51.     // APPLET INFO SUPPORT:
  52.     //        The getAppletInfo() method returns a string describing the applet's
  53.     // author, copyright date, or miscellaneous information.
  54.     //--------------------------------------------------------------------------
  55.     public String getAppletInfo()
  56.     {
  57.         return "Name: DisplayText\r\n" +
  58.                "Author: Visual J++ Product Development Team\r\n" +
  59.                "Created with Microsoft Visual J++ Version 1.0";
  60.     }
  61.  
  62.     // PARAMETER SUPPORT
  63.     //        The getParameterInfo() method returns an array of strings describing
  64.     // the parameters understood by this applet.
  65.     //
  66.     // DisplayText Parameter Information:
  67.     //  { "Name", "Type", "Description" },
  68.     //--------------------------------------------------------------------------
  69.     public String[][] getParameterInfo()
  70.     {
  71.         String[][] info =
  72.         {
  73.             { PARAM_initialValue, "String", "Initial Text to Display" },
  74.         };
  75.         return info;        
  76.     }
  77.  
  78.     // The init() method is called by the AWT when an applet is first loaded or
  79.     // reloaded.  Override this method to perform whatever initialization your
  80.     // applet needs, such as initializing data structures, loading images or
  81.     // fonts, creating frame windows, setting the layout manager, or adding UI
  82.     // components.
  83.     //--------------------------------------------------------------------------
  84.     public void init()
  85.     {
  86.         // PARAMETER SUPPORT
  87.         //        The following code retrieves the value of each parameter
  88.         // specified with the <PARAM> tag and stores it in a member
  89.         // variable.
  90.         //----------------------------------------------------------------------
  91.         String param;
  92.  
  93.         // initialValue: Initial Text to Display
  94.         //----------------------------------------------------------------------
  95.         param = getParameter(PARAM_initialValue);
  96.         if (param != null)
  97.             m_initialValue = param;
  98.  
  99.         // If you use a ResourceWizard-generated "control creator" class to
  100.         // arrange controls in your applet, you may want to call its
  101.         // CreateControls() method from within this method. Remove the following
  102.         // call to resize() before adding the call to CreateControls();
  103.         // CreateControls() does its own resizing.
  104.         //----------------------------------------------------------------------
  105.         resize(600, 120);
  106.  
  107.         // TODO: Place additional initialization code here
  108.  
  109.         // <<< Additional initialization code >>>
  110.         // Generate the Font for paint and for setText
  111.         setFont(new Font("Helvetica", Font.BOLD, 80));
  112.  
  113.         // Set the display text to the initial value
  114.         setText(m_initialValue);
  115.         // <<< end added code >>>
  116.     }
  117.  
  118.     // Place additional applet clean up code here.  destroy() is called when
  119.     // when you applet is terminating and being unloaded.
  120.     //-------------------------------------------------------------------------
  121.     public void destroy()
  122.     {
  123.         // TODO: Place applet cleanup code here
  124.     }
  125.  
  126.     // DisplayText Paint Handler
  127.     //--------------------------------------------------------------------------
  128.     public void paint(Graphics g)
  129.     {
  130.         // <<< new painting code >>>
  131.         Font font = getFont();
  132.         g.setFont(font);
  133.         g.drawString(m_phrase, m_x, m_y);
  134.         // <<< end modified code >>>
  135.     }
  136.  
  137.     //        The start() method is called when the page containing the applet
  138.     // first appears on the screen. The AppletWizard's initial implementation
  139.     // of this method starts execution of the applet's thread.
  140.     //--------------------------------------------------------------------------
  141.     public void start()
  142.     {
  143.         // TODO: Place additional applet start code here
  144.     }
  145.     
  146.     //        The stop() method is called when the page containing the applet is
  147.     // no longer on the screen. The AppletWizard's initial implementation of
  148.     // this method stops execution of the applet's thread.
  149.     //--------------------------------------------------------------------------
  150.     public void stop()
  151.     {
  152.     }
  153.  
  154.     // TODO: Place additional applet code here
  155.  
  156.     // <<< New Methods to be called by VBScript >>>
  157.     // Set the display text to the specified value and compute the
  158.     // location to display the text so it will be centered.
  159.     public void setText(String string)
  160.     {
  161.         // set the display string
  162.         m_phrase = string;
  163.  
  164.         // get the size of the display string
  165.         Font font = getFont();
  166.         Graphics g = getGraphics();
  167.         g.setFont(font);
  168.         FontMetrics fm = g.getFontMetrics();
  169.         int height = fm.getHeight();
  170.         int width = fm.stringWidth(m_phrase);
  171.  
  172.         // center the string (or left justify if it is too long)
  173.         Dimension dim = size();
  174.         m_x = ( dim.width - width) / 2;
  175.         if (m_x < 0) m_x = 0;
  176.         m_y = ( dim.height + height ) / 2 
  177.             - fm.getLeading() - fm.getDescent() / 2;
  178.         if (m_y > dim.height) m_x = dim.height;
  179.  
  180.         // force a repaint of the applet
  181.         repaint();
  182.     }
  183.  
  184.     // Reset the display text to the initial value
  185.     public void resetText()
  186.     {
  187.         setText(m_initialValue);
  188.     }
  189.     // <<< end added code >>>
  190.  
  191. }
  192.