home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-06-18 | 9.0 KB | 271 lines |
- //******************************************************************************
- // Marquee.java: Applet
- //
- //******************************************************************************
- import java.applet.*;
- import java.awt.*;
-
- //==============================================================================
- // Main Class for applet Marquee
- //
- //==============================================================================
- public class Marquee extends Applet implements Runnable
- {
- // THREAD SUPPORT:
- // m_Marquee is the Thread object for the applet
- //--------------------------------------------------------------------------
- Thread m_Marquee = null;
-
- // PARAMETER SUPPORT:
- // Parameters allow an HTML author to pass information to the applet;
- // the HTML author specifies them using the <PARAM> tag within the <APPLET>
- // tag. The following variables are used to store the values of the
- // parameters.
- //--------------------------------------------------------------------------
-
- // Members for applet parameters
- // <type> <MemberVar> = <Default Value>
- //--------------------------------------------------------------------------
- String m_string = "";
- String m_font = "Courier";
- String m_style = "PLAIN";
- int m_size = 12;
- int m_fps = 10;
-
- // parameter names. to change the name of a parameter,
- // you need only make a single change. simply modify
- // the value of the parameter string below.
- //--------------------------------------------------------------------------
- final String PARAM_string = "string";
- final String PARAM_font = "font";
- final String PARAM_style = "style";
- final String PARAM_size = "size";
- final String PARAM_fps = "fps";
-
- // the following data members are used to keep track of the
- // minimum and maximum x offset of the text. This is what
- // makes the applet scroll.
- private int m_nOffset;
- private int m_nMax;
- private int m_nMin;
-
- // Marquee Class Constructor
- //--------------------------------------------------------------------------
- public Marquee()
- {
- // TODO: Add Constructor code here
- }
-
- // Marquee Class Destructor
- //--------------------------------------------------------------------------
- public void finalize()
- {
- // TODO: Add Destructor code here
- }
-
- // APPLET INFO SUPPORT:
- // The getAppletInfo() method returns a string describing the applet's
- // author, copyright date, or miscellaneous information.
- //--------------------------------------------------------------------------
- public String getAppletInfo()
- {
- return "Name: Marquee\r\n" +
- "Author: Stephen R. Davis\r\n" +
- "Created for Learn Java Now (c)";
- }
-
- // PARAMETER SUPPORT
- // The getParameterInfo() method returns an array of strings describing
- // the parameters understood by this applet.
- //
- // Marquee Parameter Information:
- // { "Name", "Type", "Description" },
- //--------------------------------------------------------------------------
- public String[][] getParameterInfo()
- {
- String[][] info =
- {
- { PARAM_string, "String", "String to output in marquee" },
- { PARAM_font, "String", "Font to use" },
- { PARAM_style, "String", "PLAIN, BOLD, or ITALIC" },
- { PARAM_size, "int", "Font size [pts]" },
- { PARAM_fps, "int", "Frame rate" },
- };
- return info;
- }
-
- // The init() method is called by the AWT when an applet is first loaded or
- // reloaded. Override this method to perform whatever initialization your
- // applet needs, such as initializing data structures, loading images or
- // fonts, creating frame windows, setting the layout manager, or adding UI
- // components.
- //--------------------------------------------------------------------------
- public void init()
- {
- // PARAMETER SUPPORT
- // The following code retrieves the value of each parameter
- // specified with the <PARAM> tag and stores it in a member
- // variable.
- //----------------------------------------------------------------------
- String param;
-
- // string: String to output in marquee
- //----------------------------------------------------------------------
- param = getParameter(PARAM_string);
- if (param != null)
- m_string = param;
-
- // font: Font to use
- //----------------------------------------------------------------------
- param = getParameter(PARAM_font);
- if (param != null)
- m_font = param;
-
- // style: PLAIN, BOLD, or ITALIC
- //----------------------------------------------------------------------
- param = getParameter(PARAM_style);
- if (param != null)
- m_style = param;
-
- // size: Font size [pts]
- //----------------------------------------------------------------------
- param = getParameter(PARAM_size);
- if (param != null)
- m_size = Integer.parseInt(param);
-
- // fps: Frame rate
- //----------------------------------------------------------------------
- param = getParameter(PARAM_fps);
- if (param != null)
- m_fps = Integer.parseInt(param);
-
- // resize(600, 80);
-
- // create the font here based on the parameters entered
- int nStyle = Font.PLAIN;
- if (m_style.equalsIgnoreCase("BOLD"))
- {
- nStyle = Font.BOLD;
- }
- if (m_style.equalsIgnoreCase("ITALIC"))
- {
- nStyle = Font.ITALIC;
- }
-
- Font font = new Font(m_font, nStyle, m_size);
-
- // now make this the font for this applet's window
- setFont(font);
-
- // now calculate the size of the string in the current
- // font. use this information to set the minimum and
- // maximum x offset.
- // calculate the left boundary by measuring the
- // size of the message in the current font.
- FontMetrics fm = getFontMetrics(font);
- m_nMin = -fm.stringWidth(m_string);
-
- // the right margin is the width of the window
- m_nMax = size().width;
-
- // now start the string at the far right of the window
- m_nOffset = m_nMax;
-
- // TODO: Place Addition Initialization code here
- }
-
- // Place additional applet clean up code here. destroy() is called when
- // when you applet is terminating and being unloaded.
- //-------------------------------------------------------------------------
- public void destroy()
- {
- // TODO: Place applet cleanup code here
- }
-
- // Marquee Paint Handler
- //--------------------------------------------------------------------------
- public void paint(Graphics g)
- {
- int nVertOffset = size().height;
- nVertOffset = (nVertOffset - m_size) / 2;
- g.drawString(m_string, m_nOffset, nVertOffset + m_size);
-
- // now decrement the offset so that the next
- // time the string will appear one pixel to the left
- // (once the entire string has scrolled off to the left,
- // restart it at the far right)
- m_nOffset--;
- if (m_nOffset < m_nMin)
- {
- m_nOffset = m_nMax;
- }
- }
-
- // The start() method is called when the page containing the applet
- // first appears on the screen. The AppletWizard's initial implementation
- // of this method starts execution of the applet's thread.
- //--------------------------------------------------------------------------
- public void start()
- {
- if (m_Marquee == null)
- {
- m_Marquee = new Thread(this);
- m_Marquee.start();
- }
- // TODO: Place additional applet Start code here
- }
-
- // The stop() method is called when the page containing the applet is
- // no longer on the screen. The AppletWizard's initial implementation of
- // this method stops execution of the applet's thread.
- //--------------------------------------------------------------------------
- public void stop()
- {
- if (m_Marquee != null)
- {
- m_Marquee.stop();
- m_Marquee = null;
- }
-
- // TODO: Place additional applet Stop code here
- }
-
- // THREAD SUPPORT
- // The run() method is called when the applet's thread is started. If
- // your applet performs any ongoing activities without waiting for user
- // input, the code for implementing that behavior typically goes here. For
- // example, for an applet that performs animation, the run() method controls
- // the display of images.
- //--------------------------------------------------------------------------
- public void run()
- {
- while (true)
- {
- // calculate the sleep time based on the frames
- // per second. sleep time is in units of
- // milliseconds.
- int nSleepValue = 1000 / m_fps;
- try
- {
- // repaint the window every nSleepValue
- // milliseconds
- repaint();
- // Thread.sleep(50); // we don't want the default
- Thread.sleep(nSleepValue);
- }
- catch (InterruptedException e)
- {
- // TODO: Place Exception handling code here in case an
- // InterruptedException is thrown by Thread.sleep(),
- // meaning that another thread has interrupted this one
- stop();
- }
- }
- }
-
-
-
- // TODO: Place additional applet Code here
-
- }
-