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 / VerticalScroller.java < prev   
Encoding:
Java Source  |  1996-10-04  |  8.3 KB  |  261 lines

  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.util.*;
  4. import java.net.*;
  5.  
  6.  
  7. /***************************************************************************
  8.  Applet:    VerticalScroller
  9.  Author:    Imagination's End, mods by Jeff Rhyason
  10.  Desc:      Doesn't the name basically state it all?
  11.  
  12.  Date Created:  19960422
  13.  Date Modified: 19961005
  14.  ***************************************************************************/
  15.  
  16. /*
  17.                         VerticalScroller
  18.                         
  19.                             Overview
  20.                             
  21.  
  22.     The first 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.     upward 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.     PTSIZE:     the default font size (20)
  46.     STEPS:      number of pixels to jump upward in each movement (3)
  47.     NUMLINES:   the number of lines of text you want scrolled
  48.     FONT:       the default font to use (Helv)
  49.     
  50.                         Per-Line Parameters
  51.  
  52.     Per-Line parameters involve a prefix, and have a line-number
  53.     (0-based) appended.
  54.     
  55.     STRING0:    text to display on line 0 (1st line) (nothing)
  56.     COLOR0:     color that text will be written in (RRGGBB in hex)
  57.     BACKCOLOR0: background text will be written over (same)
  58.     LOCALURL0:  a local URL to visit when the line is clicked
  59.     REMOTEURL0: a remote URL to visit
  60.     
  61.     There is no difference between the LOCALURL and the REMOTEURL;
  62.     if the LOCALURL is entered, it's called first.  If no LOCALURL
  63.     exists, but a REMOTEURL does, it will be called instead.
  64.     
  65.                             Types of Values
  66.                             
  67.     Strings are usually stored with double quotes (" ") around them.
  68.     They are required if the string is to have a space in it, otherwise
  69.     the space would get confused with the HTML standard of delimiting 
  70.     parameters with spaces.  
  71.     
  72.     Colors are represented in a numerical format only.  Each pair of
  73.     digits in a series of three is the hexadecimal representation for 
  74.     the Red, Green, or Blue component (in that order).  
  75.     
  76.                                Examples
  77.                                
  78.     <APPLET CODE=VerticalScroller.class WIDTH=200 HEIGHT=100>
  79.        <PARAM NAME=FONT VALUE=COURIER>
  80.        <PARAM NAME=PTSIZE VALUE=20>
  81.        <PARAM NAME=NUMLINES VALUE=2>
  82.     
  83.        <PARAM NAME=STRING0 VALUE="Hello there!">
  84.        <PARAM NAME=COLOR0 VALUE=0077FF>
  85.        <PARAM NAME=BACKCOLOR0 VALUE=AA0000>
  86.        <PARAM NAME=REMOTEURL0 VALUE=http://disemia.com/>
  87.     
  88.        <PARAM NAME=STRING1 VALUE="How are you?">
  89.        <PARAM NAME=COLOR1 VALUE=00DD00>
  90.     </APPLET>
  91. */
  92.  
  93.  
  94. public class VerticalScroller extends Applet implements Runnable
  95. {
  96.     Thread scroll;
  97.     int timeout, stepSize, topLine, numLines, ptSize, linesPerScreen, offset;
  98.     Color clrBack, clrText;
  99.     String strs[];
  100.     URL localURLS[];
  101.     URL remoteURLS[];
  102.     Color strClrs[],backClrs[];
  103.     int strWidths[];
  104.     FontMetrics fm;
  105.     Font strFont;
  106.     Image os;
  107.     Rectangle r;
  108.     Graphics osg;
  109.  
  110.     public void init()
  111.     {
  112.         fm = null;
  113.         timeout = IEExtend.strInt(getParameter("TIMEOUT"),100);
  114.         clrBack = IEExtend.strColor(getParameter("BACKCOLOR"),new Color(0,0,0));
  115.         clrText = IEExtend.strColor(getParameter("TEXTCOLOR"),new Color(255,255,255));
  116.         ptSize = IEExtend.strInt(getParameter("PTSIZE"), 20);
  117.         stepSize = IEExtend.strInt(getParameter("STEPSIZE"), ptSize/7);
  118.         numLines = IEExtend.strInt(getParameter("NUMLINES"),0);
  119.         strs = new String[numLines];
  120.         strClrs = new Color[numLines];
  121.         backClrs = new Color[numLines];
  122.         localURLS = new URL[numLines];
  123.         remoteURLS = new URL[numLines];
  124.         if(getParameter("FONT") == null)
  125.             strFont = new Font("Helv",Font.BOLD,ptSize);
  126.         else
  127.             strFont = new Font(getParameter("FONT"),Font.PLAIN,ptSize);
  128.  
  129.         r = this.bounds();
  130.         linesPerScreen = (r.height / ptSize) + 2;
  131.         String temp;
  132.         for(int i=0;i<numLines;i++)
  133.         {
  134.             try
  135.             {
  136.                 strs[i] = getParameter("STRING" + i);
  137.                 strClrs[i] = IEExtend.strColor(getParameter("COLOR"+i),clrText);
  138.                 backClrs[i] = IEExtend.strColor(getParameter("BACKCOLOR"+i),null);
  139.                 if(getParameter("LOCALURL"+i)!= null && getParameter("LOCALURL"+i).length() > 0)
  140.                     localURLS[i] = new URL(getDocumentBase(),getParameter("LOCALURL"+i));
  141.                 if(getParameter("REMOTEURL"+i)!=null && getParameter("REMOTEURL"+i).length() > 0)
  142.                     remoteURLS[i] = new URL(getParameter("REMOTEURL"+i));
  143.             }
  144.             catch(Exception e)
  145.             {
  146.                 System.out.println("Malformed URL probably!");
  147.                 System.err.println("Check your line-specific parameters for line " + i );
  148.             }
  149.         }
  150.  
  151.         os = createImage(r.width, r.height);
  152.         osg = os.getGraphics();
  153.  
  154.         topLine = -linesPerScreen;
  155.         offset = 0;
  156.     }
  157.  
  158.     public String getAppletInfo()
  159.     {
  160.         return "VerticalScroller for the Mortar Package, by Big Picture Multimedia";
  161.     }
  162.  
  163.     public void start()
  164.     {
  165.         scroll = new Thread(this);
  166.         scroll.start();
  167.     }
  168.  
  169.     public void stop()
  170.     {
  171.         scroll.stop();
  172.     }
  173.  
  174.     public void run()
  175.     {
  176.         Graphics g = this.getGraphics();
  177.         while(true)
  178.         {
  179.             try
  180.             {
  181.                 Thread.currentThread().sleep(timeout);
  182.             }
  183.             catch(Exception e)
  184.             { 
  185.                 return; 
  186.             }
  187.             moveText(g);
  188.         }
  189.     }
  190.  
  191.     public void paint(Graphics g)
  192.     {
  193.         g.drawImage(os,0,0,this);
  194.     }
  195.  
  196.     private void moveText(Graphics g)
  197.     {
  198.         osg.setColor(clrBack);
  199.         osg.fillRect(0,0,r.width,r.height);
  200.         osg.setFont(strFont);
  201.         if(fm == null)
  202.         {
  203.             fm = osg.getFontMetrics();
  204.             strWidths = new int[numLines];
  205.             for(int i=0;i<numLines;i++)
  206.                 strWidths[i] = r.width/2 - fm.stringWidth(strs[i])/2;
  207.         }
  208.  
  209.         for(int i=0;i<linesPerScreen;i++)
  210.         {
  211.             if(!(topLine + i >= numLines) && (topLine+i >= 0))
  212.             {
  213.                 if(backClrs[topLine+i] != null)
  214.                 {
  215.                     osg.setColor(backClrs[topLine+i]);
  216.                     osg.fillRect(0,(i-1)*ptSize-offset+fm.getMaxDescent(),r.width,ptSize);
  217.                 }
  218.                 osg.setColor(strClrs[topLine+i]);
  219.                 osg.drawString(strs[topLine+i], strWidths[topLine+i], i*ptSize - offset);
  220.             }
  221.         }
  222.         offset += stepSize;
  223.         if(offset >= ptSize)
  224.         {
  225.             offset -= ptSize;
  226.             topLine++;
  227.             if(topLine > numLines)
  228.             {
  229.                 topLine = -linesPerScreen;
  230.             }
  231.         }
  232.         g.drawImage(os,0,0,this);
  233.     }
  234.  
  235.     public boolean mouseDown(Event evt, int x, int y)
  236.     {
  237.         // the y is the only significant thing
  238.         if(y < 0 || y > r.height)
  239.             return true;
  240.  
  241.         int line = (y+offset+ptSize-fm.getMaxDescent()) / ptSize + topLine;
  242.         if(line < 0 || line >= numLines)
  243.             return true;
  244.  
  245.         if(localURLS[line] != null)
  246.         {
  247.             System.out.println(localURLS[line].toString());
  248.             getAppletContext().showDocument(localURLS[line]);
  249.         }
  250.         else if(remoteURLS[line] != null)
  251.         {
  252.             System.out.println(remoteURLS[line].toString());
  253.             getAppletContext().showDocument(remoteURLS[line]);
  254.         }
  255.         return true;
  256.     }
  257. }
  258.  
  259.  
  260.  
  261.