home *** CD-ROM | disk | FTP | other *** search
/ The Best of Windows 95.com 1997 April / Win95_04974.iso / docs / 00016 / 01649.exe / data.2 / program / Java / HorizontalScroller.java < prev    next >
Encoding:
Java Source  |  1996-10-04  |  7.0 KB  |  226 lines

  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.util.*;
  4.  
  5. /***************************************************************************
  6.  Applet:    HorizontalScroller
  7.  Author:    Imagination's End, mods by Jeff Rhyason
  8.  Desc:      Doesn't the name basically state it all?
  9.  
  10.  Date Created:  19960422
  11.  Date Modified: 19961005
  12.  
  13.  This is a finicky version.
  14.  ***************************************************************************/
  15.  
  16. /*
  17.                         HorizontalScroller
  18.                         
  19.                             Overview
  20.                             
  21.  
  22.     The second in a series of text-scrolling applets that will
  23.     be included in the Mortar package.  It takes uses an off-
  24.     screen image buffer to update an area of drawn text in 
  25.     increments small enough that the text appears to move 
  26.     right to left somewhat smoothly.
  27.     
  28.     Like most applets, characteristics and data can be 
  29.     customized through PARAM tags in the HTML file from 
  30.     which the applet is invoked.  
  31.     
  32.     Each line of text can be customized individually, to have
  33.     a particular foreground or background colour.  Each line of
  34.     text can be assigned a URL which the browser is referred to
  35.     if a mouseclick is detected over the line.
  36.     
  37.                         General Parameters
  38.                         
  39.     General Parameters are mostly defaults which are used if
  40.     a line does not specify its own characteristics.  
  41.  
  42.     TIMEOUT:    delay between each movement (in milliseconds)
  43.     BACKCOLOR:  the default background color for a line of text (black)
  44.     TEXTCOLOR:  the default foreground color (white) See note on COLORS below
  45.     FADESTEPS:  number of color steps to fade the text to at the ends (default 5)
  46.     TEXT:       the string which will get written
  47.     PTSIZE:     the font size (calculated automatically)
  48.     FONT:       The font to use (defaults to Courier)
  49.     
  50.     Note: It is recommended that, for best performance, unless you have
  51.     a specific need, you do not specify PTSIZE, as it can be calculated
  52.     automatically based on the height of the applet.  Also, applets should
  53.     generally not be too large (40 pixels high is about the limit).
  54.     
  55.     Also, at this time it is required that you choose monospaced fonts for
  56.     the applet to look proper.  (such as Courier, the default).
  57.     
  58.     It is furthermore required that the TEXT you display have at least 
  59.     2 times the FADESTEPS + 2 characters, where FADESTEPS defaults to 5.
  60.     
  61.                             Types of Values
  62.                             
  63.     Strings are usually stored with double quotes (" ") around them.
  64.     They are required if the string is to have a space in it, otherwise
  65.     the space would get confused with the HTML standard of delimiting 
  66.     parameters with spaces.  
  67.     
  68.     Colors are represented in a numerical format only.  Each pair of
  69.     digits in a series of three is the hexadecimal representation for 
  70.     the Red, Green, or Blue component (in that order).  
  71.  
  72.     
  73.     
  74.                                Examples
  75.                                
  76.     <APPLET CODE=HorizontalScroller.class WIDTH=400 HEIGHT=30>
  77.        <PARAM NAME=TEXT VALUE="Hello there, fellow Mortar immortal!  What do you think of Mortar so far?">
  78.        <PARAM NAME=TEXTCOLOR VALUE=EEEEEE>
  79.        <PARAM NAME=BACKCOLOR VALUE=000055>
  80.     </APPLET>
  81.  
  82. */
  83.  
  84. public class HorizontalScroller extends Applet implements Runnable
  85. {
  86.     Thread scroll;
  87.     int timeout,fadeSteps,tWidth,cWidth;
  88.     Color clrBack, clrText, clrFade[];
  89.     String strText;
  90.     Font strFont;
  91.     FontMetrics fm;
  92.     Image os;
  93.  
  94.     public void init()
  95.     {
  96.         fm = null;
  97.         timeout = IEExtend.strInt(getParameter("TIMEOUT"),200);
  98.         clrBack = IEExtend.strColor(getParameter("BACKCOLOR"),new Color(0,0,0));
  99.         clrText = IEExtend.strColor(getParameter("TEXTCOLOR"),new Color(255,255,255));
  100.         strText = new String(getParameter("TEXT"));
  101.         fadeSteps = IEExtend.strInt(getParameter("FADESTEPS"),5);
  102.         clrFade = new Color[fadeSteps];
  103.  
  104.         int ptSize = IEExtend.strInt(getParameter("PTSIZE"),bounds().height);
  105.         if(getParameter("FONT") == null)
  106.             strFont = new Font("Courier",Font.PLAIN,ptSize);
  107.         else
  108.             strFont = new Font(getParameter("FONT"),Font.PLAIN,ptSize);
  109.  
  110.         CalcFade();
  111.  
  112.         os = createImage(bounds().width, bounds().height);
  113.     }
  114.  
  115.     private void CalcFade()
  116.     {
  117.         int lr = clrBack.getRed();
  118.         int lg = clrBack.getGreen();
  119.         int lb = clrBack.getBlue();
  120.         int hr = clrText.getRed();
  121.         int hg = clrText.getGreen();
  122.         int hb = clrText.getBlue();
  123.         int rStep = (hr - lr) / (fadeSteps+2);
  124.         int gStep = (hg - lg) / (fadeSteps+2);
  125.         int bStep = (hb - lb) / (fadeSteps+2);
  126.  
  127.         for(int i=0;i<fadeSteps;i++)
  128.         {
  129.             clrFade[i] = new Color(lr + (i+1)*rStep, lg + (i+1)*gStep, lb + (i+1)*bStep);
  130.         }
  131.     }
  132.  
  133.     public String getAppletInfo()
  134.     {
  135.         return "HorizontalScroller for the Mortar Package, by Big Picture Multimedia";
  136.     }
  137.  
  138.     public void start()
  139.     {
  140.         scroll = new Thread(this);
  141.         scroll.start();
  142.     }
  143.  
  144.     public void stop()
  145.     {
  146.         scroll.stop();
  147.     }
  148.  
  149.     public void run()
  150.     {
  151.         while(true)
  152.         {
  153.             try
  154.             {
  155.                 Thread.currentThread().sleep(timeout);
  156.             }
  157.             catch(Exception e)
  158.             { 
  159.                 return; 
  160.             }
  161.             Graphics g = this.getGraphics();
  162.             moveText(g);
  163.             g.dispose();
  164.             g.finalize();
  165.         }
  166.     }
  167.  
  168.     private void moveText(Graphics h)
  169.     {
  170.         Graphics g = os.getGraphics();
  171.  
  172.         String newStr = strText.substring(1) + strText.charAt(0);
  173.         g.setFont(strFont);
  174.         if(fm == null)
  175.         {
  176.             fm = g.getFontMetrics();
  177.             calcData();
  178.         }
  179.         int height = bounds().height - fm.getDescent();
  180.  
  181.         g.setColor(clrBack);
  182.         g.fillRect(0,0,this.bounds().width,bounds().height);
  183.         drawNewString(g,height,newStr);
  184.  
  185.         strText = newStr;
  186.  
  187.         g.dispose();
  188.         h.drawImage(os, 0, 0, this );
  189.     }
  190.  
  191.     private void drawNewString(Graphics g, int height, String newStr)
  192.     {
  193.         //now, some more complex drawing
  194.         int xAt = 0;
  195.         for(int i=0;i<fadeSteps;i++)
  196.         {
  197.             g.setColor(clrFade[i]);
  198.             g.drawString(newStr.substring(i,i+1), i*cWidth, height);
  199.         }
  200.         g.setColor(clrText);
  201.         g.drawString(newStr.substring(fadeSteps,tWidth-fadeSteps),
  202.             fadeSteps*cWidth,height);
  203.         for(int i=0;i<fadeSteps;i++)
  204.         {
  205.             g.setColor(clrFade[fadeSteps-i-1]);
  206.             g.drawString(newStr.substring(tWidth-fadeSteps+i,
  207.                 tWidth-fadeSteps+1+i), (tWidth-fadeSteps+i)*cWidth,height);
  208.         }
  209.     }
  210.  
  211.     public void paint(Graphics g)
  212.     {
  213.         g.drawImage(os, 0, 0, this );
  214.     }
  215.  
  216.     private void calcData()
  217.     {
  218.         Rectangle r = bounds();
  219.         tWidth = r.width / fm.charWidth('A'); //all widths the same
  220.         cWidth = fm.charWidth('A');
  221.     }
  222. }
  223.  
  224.  
  225.  
  226.