home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / JavaNow / Code / Chap14 / Marquee / Marquee.java < prev    next >
Encoding:
Java Source  |  1996-06-18  |  9.0 KB  |  271 lines

  1. //******************************************************************************
  2. // Marquee.java:    Applet
  3. //
  4. //******************************************************************************
  5. import java.applet.*;
  6. import java.awt.*;
  7.  
  8. //==============================================================================
  9. // Main Class for applet Marquee
  10. //
  11. //==============================================================================
  12. public class Marquee extends Applet implements Runnable
  13. {
  14.     // THREAD SUPPORT:
  15.     //        m_Marquee    is the Thread object for the applet
  16.     //--------------------------------------------------------------------------
  17.     Thread     m_Marquee = null;
  18.  
  19.     // PARAMETER SUPPORT:
  20.     //        Parameters allow an HTML author to pass information to the applet; 
  21.     // the HTML author specifies them using the <PARAM> tag within the <APPLET> 
  22.     // tag.  The following variables are used to store the values of the 
  23.     // parameters.
  24.     //--------------------------------------------------------------------------
  25.  
  26.     // Members for applet parameters
  27.     // <type>       <MemberVar>    = <Default Value>
  28.     //--------------------------------------------------------------------------
  29.     String m_string = "";
  30.     String m_font = "Courier";
  31.     String m_style = "PLAIN";
  32.     int m_size = 12;
  33.     int m_fps = 10;
  34.  
  35.     // parameter names. to change the name of a parameter,
  36.     // you need only make a single change. simply modify
  37.     // the value of the parameter string below.
  38.     //--------------------------------------------------------------------------
  39.     final String PARAM_string = "string";
  40.     final String PARAM_font = "font";
  41.     final String PARAM_style = "style";
  42.     final String PARAM_size = "size";
  43.     final String PARAM_fps = "fps";
  44.  
  45.     // the following data members are used to keep track of the
  46.     // minimum and maximum x offset of the text. This is what
  47.     // makes the applet scroll.
  48.     private int m_nOffset;
  49.     private int m_nMax;
  50.     private int m_nMin;
  51.  
  52.     // Marquee Class Constructor
  53.     //--------------------------------------------------------------------------
  54.     public Marquee()
  55.     {
  56.         // TODO: Add Constructor code here
  57.     }
  58.  
  59.     // Marquee Class Destructor
  60.     //--------------------------------------------------------------------------
  61.     public void finalize()
  62.     {
  63.         // TODO: Add Destructor code here
  64.     }
  65.  
  66.     // APPLET INFO SUPPORT:
  67.     //        The getAppletInfo() method returns a string describing the applet's 
  68.     // author, copyright date, or miscellaneous information.
  69.     //--------------------------------------------------------------------------
  70.     public String getAppletInfo()
  71.     {
  72.         return "Name: Marquee\r\n" +
  73.                "Author: Stephen R. Davis\r\n" +
  74.                "Created for Learn Java Now (c)";
  75.     }
  76.  
  77.     // PARAMETER SUPPORT
  78.     //        The getParameterInfo() method returns an array of strings describing 
  79.     // the parameters understood by this applet.
  80.     // 
  81.     // Marquee Parameter Information:
  82.     //  { "Name", "Type", "Description" },
  83.     //--------------------------------------------------------------------------
  84.     public String[][] getParameterInfo()
  85.     {
  86.         String[][] info =
  87.         {
  88.             { PARAM_string, "String", "String to output in marquee" },
  89.             { PARAM_font, "String", "Font to use" },
  90.             { PARAM_style, "String", "PLAIN, BOLD, or ITALIC" },
  91.             { PARAM_size, "int", "Font size [pts]" },
  92.             { PARAM_fps, "int", "Frame rate" },
  93.         };
  94.         return info;        
  95.     }
  96.  
  97.     // The init() method is called by the AWT when an applet is first loaded or 
  98.     // reloaded.  Override this method to perform whatever initialization your 
  99.     // applet needs, such as initializing data structures, loading images or 
  100.     // fonts, creating frame windows, setting the layout manager, or adding UI 
  101.     // components.
  102.     //--------------------------------------------------------------------------
  103.     public void init()
  104.     {
  105.         // PARAMETER SUPPORT
  106.         //        The following code retrieves the value of each parameter 
  107.         // specified with the <PARAM> tag and stores it in a member 
  108.         // variable.
  109.         //----------------------------------------------------------------------
  110.         String param;
  111.  
  112.         // string: String to output in marquee
  113.         //----------------------------------------------------------------------
  114.         param = getParameter(PARAM_string);
  115.         if (param != null)
  116.             m_string = param;
  117.  
  118.         // font: Font to use
  119.         //----------------------------------------------------------------------
  120.         param = getParameter(PARAM_font);
  121.         if (param != null)
  122.             m_font = param;
  123.  
  124.         // style: PLAIN, BOLD, or ITALIC
  125.         //----------------------------------------------------------------------
  126.         param = getParameter(PARAM_style);
  127.         if (param != null)
  128.             m_style = param;
  129.  
  130.         // size: Font size [pts]
  131.         //----------------------------------------------------------------------
  132.         param = getParameter(PARAM_size);
  133.         if (param != null)
  134.             m_size = Integer.parseInt(param);
  135.  
  136.         // fps: Frame rate
  137.         //----------------------------------------------------------------------
  138.         param = getParameter(PARAM_fps);
  139.         if (param != null)
  140.             m_fps = Integer.parseInt(param);
  141.  
  142.         // resize(600, 80);
  143.  
  144.         // create the font here based on the parameters entered
  145.         int nStyle = Font.PLAIN;
  146.         if (m_style.equalsIgnoreCase("BOLD"))
  147.         {
  148.             nStyle = Font.BOLD;
  149.         }
  150.         if (m_style.equalsIgnoreCase("ITALIC"))
  151.         {
  152.             nStyle = Font.ITALIC;
  153.         }
  154.  
  155.         Font font = new Font(m_font, nStyle, m_size);
  156.  
  157.         // now make this the font for this applet's window
  158.         setFont(font);
  159.  
  160.         // now calculate the size of the string in the current
  161.         // font. use this information to set the minimum and
  162.         // maximum x offset.
  163.         // calculate the left boundary by measuring the
  164.         // size of the message in the current font.
  165.         FontMetrics fm = getFontMetrics(font);
  166.         m_nMin = -fm.stringWidth(m_string);
  167.  
  168.         // the right margin is the width of the window
  169.         m_nMax = size().width;
  170.  
  171.         // now start the string at the far right of the window
  172.         m_nOffset  = m_nMax;
  173.  
  174.         // TODO: Place Addition Initialization code here
  175.     }
  176.  
  177.     // Place additional applet clean up code here.  destroy() is called when 
  178.     // when you applet is terminating and being unloaded.
  179.     //-------------------------------------------------------------------------
  180.     public void destroy()
  181.     {
  182.         // TODO: Place applet cleanup code here
  183.     }
  184.  
  185.     // Marquee Paint Handler
  186.     //--------------------------------------------------------------------------
  187.     public void paint(Graphics g)
  188.     {
  189.         int nVertOffset = size().height;
  190.         nVertOffset = (nVertOffset - m_size) / 2;
  191.         g.drawString(m_string, m_nOffset, nVertOffset + m_size);
  192.  
  193.         // now decrement the offset so that the next
  194.         // time the string will appear one pixel to the left
  195.         // (once the entire string has scrolled off to the left,
  196.         // restart it at the far right)
  197.         m_nOffset--;
  198.         if (m_nOffset < m_nMin)
  199.         {
  200.             m_nOffset = m_nMax;
  201.         }
  202.     }
  203.  
  204.     //        The start() method is called when the page containing the applet 
  205.     // first appears on the screen. The AppletWizard's initial implementation 
  206.     // of this method starts execution of the applet's thread.
  207.     //--------------------------------------------------------------------------
  208.     public void start()
  209.     {
  210.         if (m_Marquee == null)
  211.         { 
  212.             m_Marquee = new Thread(this);
  213.             m_Marquee.start();
  214.         }
  215.         // TODO: Place additional applet Start code here
  216.     }
  217.     
  218.     //        The stop() method is called when the page containing the applet is 
  219.     // no longer on the screen. The AppletWizard's initial implementation of 
  220.     // this method stops execution of the applet's thread.
  221.     //--------------------------------------------------------------------------
  222.     public void stop()
  223.     {
  224.         if (m_Marquee != null)
  225.         {
  226.             m_Marquee.stop();
  227.             m_Marquee = null;
  228.         }
  229.  
  230.         // TODO: Place additional applet Stop code here
  231.     }
  232.  
  233.     // THREAD SUPPORT
  234.     //        The run() method is called when the applet's thread is started. If 
  235.     // your applet performs any ongoing activities without waiting for user 
  236.     // input, the code for implementing that behavior typically goes here. For 
  237.     // example, for an applet that performs animation, the run() method controls
  238.     // the display of images.
  239.     //--------------------------------------------------------------------------
  240.     public void run()
  241.     {
  242.         while (true)
  243.         {
  244.             // calculate the sleep time based on the frames
  245.             // per second. sleep time is in units of
  246.             // milliseconds.
  247.             int nSleepValue = 1000 / m_fps;
  248.             try
  249.             {
  250.                 // repaint the window every nSleepValue
  251.                 // milliseconds
  252.                 repaint();
  253.               // Thread.sleep(50); // we don't want the default
  254.                 Thread.sleep(nSleepValue);
  255.             }
  256.             catch (InterruptedException e)
  257.             {
  258.                 // TODO: Place Exception handling code here in case an 
  259.                 //       InterruptedException is thrown by Thread.sleep(),
  260.                 //         meaning that another thread has interrupted this one
  261.                 stop();
  262.             }
  263.         }
  264.     }
  265.  
  266.  
  267.  
  268.     // TODO: Place additional applet Code here
  269.  
  270. }
  271.