home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 October / PCO1097.ISO / FilesBBS / FREI / LSCROLL.EXE / LinkScroll.java < prev    next >
Encoding:
Java Source  |  1997-09-02  |  12.7 KB  |  513 lines

  1. /************************************************************************************************
  2.  
  3.  Displays a vertically scrolling a list of web links, clicking on a link will send the browser
  4.  to that site.
  5.  
  6. <applet code="LinkScroll.class" width=w height=h>
  7. <param name="delay" value="25">
  8. <param name="pause" value="2000">
  9. <param name="fgcolor" value="color">
  10. <param name="bgcolor" value="color">
  11. <param name="vlcolor" value="#663399">
  12. <param name="shadow" value="x,y,color">
  13. <param name="border" value="size,color">
  14. <param name="font" value="name,style,size">
  15. <param name="link-1" value="string">
  16. <param name="url-1" value="url" or "url,target">
  17. <param name="status-1" value="string">
  18.        ...
  19. <param name="link-n" value="string">
  20. <param name="url-n" value="url" or "url,target">
  21. <param name="status-n" value="string">
  22. </applet>
  23.  
  24. ************************************************************************************************/
  25.  
  26. import java.awt.*;
  27. import java.net.*;
  28. import java.util.*;
  29. import java.applet.Applet;
  30.  
  31. public class LinkScroll extends Applet implements Runnable {
  32.  
  33.   // Thread control variables.
  34.  
  35.   Thread scrollThread;
  36.   boolean paused = false;
  37.  
  38.   // Parameters and defaults.
  39.  
  40.   int    delay     = 25;
  41.   int    pause     = 2000;
  42.   Color  fgColor   = Color.blue;
  43.   Color  bgColor   = Color.white;
  44.   Color  vlColor   = new Color(102,51,153);
  45.   int    shxOffset = 0;
  46.   int    shyOffset = 0;
  47.   Color  shColor   = Color.lightGray;
  48.   int    bdSize    = 0;
  49.   Color  bdColor;
  50.   String fontName  = "Dialog";
  51.   int    fontStyle = Font.PLAIN;
  52.   int    fontSize  = 12;
  53.  
  54.   // Link data vectors.
  55.  
  56.   Vector linkList   = new Vector();
  57.   Vector urlList    = new Vector();
  58.   Vector targetList = new Vector();
  59.   Vector statusList = new Vector();
  60.   Vector hitList    = new Vector();
  61.  
  62.   // Current link data.
  63.  
  64.   String  link;
  65.   URL     url;
  66.   String  target;
  67.   String  status;
  68.   Boolean hit;
  69.   int     currentLink;
  70.  
  71.   // Values for the offscreen image.
  72.  
  73.   int xOffset;
  74.   int yOffset;
  75.   int yOffsetBase;
  76.   int yOffsetMax;
  77.   int yOffsetMin;
  78.   Font font;
  79.   Dimension offDimension;
  80.   Image offImage;
  81.   Graphics offGraphics;
  82.  
  83.   // Status window flag.
  84.  
  85.   boolean mouseIn = false;
  86.  
  87.   public void init() {
  88.  
  89.     String s, t, u;
  90.     StringTokenizer st;
  91.     int i, n;
  92.     Graphics g;
  93.     Dimension d;
  94.     FontMetrics fm;
  95.  
  96.     // Get delay time.
  97.  
  98.     try {
  99.       s = getParameter("delay");
  100.       if (s != null)
  101.         if ((n = Integer.parseInt(s)) > 0)
  102.           delay = n;
  103.     }
  104.     catch (Exception e) {}
  105.  
  106.     // Get pause time.
  107.     
  108.     try {
  109.       s = getParameter("pause");
  110.       if (s != null)
  111.         if ((n = Integer.parseInt(s)) > 0)
  112.           pause = n;
  113.     }
  114.     catch (Exception e) {}
  115.     
  116.     // Get foreground, background and visited-link colors.
  117.  
  118.     try {
  119.       s = getParameter("fgcolor");
  120.       if (s != null)
  121.         fgColor = getColorParm(s);
  122.     }
  123.     catch (Exception e) {}
  124.     bdColor = fgColor;
  125.  
  126.     try {
  127.       s = getParameter("bgcolor");
  128.       if (s != null)
  129.         bgColor = getColorParm(s);
  130.     }
  131.     catch (Exception e) {}
  132.  
  133.     try {
  134.       s = getParameter("vlcolor");
  135.       if (s != null)
  136.         vlColor = getColorParm(s);
  137.     }
  138.     catch (Exception e) {}
  139.  
  140.     // Get shadow offsets and color.
  141.  
  142.     try {
  143.       s = getParameter("shadow");
  144.       if (s != null) {
  145.         st = new StringTokenizer(s, ",");
  146.         if ((n = Integer.parseInt(st.nextToken())) > 0)
  147.           shxOffset = n;
  148.         if ((n = Integer.parseInt(st.nextToken())) > 0)
  149.           shyOffset = n;
  150.         shColor = getColorParm(st.nextToken());
  151.       }
  152.     }
  153.     catch (Exception e) {}
  154.  
  155.     // Get border size and color.
  156.  
  157.     try {
  158.       s = getParameter("border");
  159.       if (s != null) {
  160.         st = new StringTokenizer(s, ",");
  161.         if ((n = Integer.parseInt(st.nextToken())) > 0)
  162.           bdSize = n;
  163.         bdColor = getColorParm(st.nextToken());
  164.       }
  165.     }
  166.     catch (Exception e) {}
  167.  
  168.     // Get the font.
  169.  
  170.     try {
  171.       s = getParameter("font");
  172.  
  173.       // Font name.
  174.  
  175.       st = new StringTokenizer(s, ",");
  176.       t = st.nextToken();
  177.       if (t.equalsIgnoreCase("Courier"))
  178.         fontName = "Courier";
  179.       else if (t.equalsIgnoreCase("Dialog"))
  180.         fontName = "Dialog";
  181.       else if (t.equalsIgnoreCase("Helvetica"))
  182.         fontName = "Helvetica";
  183.       else if (t.equalsIgnoreCase("Symbol"))
  184.         fontName = "Symbol";
  185.       else if (t.equalsIgnoreCase("TimesRoman"))
  186.         fontName = "TimesRoman";
  187.  
  188.       // Font style.
  189.  
  190.       t = st.nextToken();
  191.       if (t.equalsIgnoreCase("plain"))
  192.         fontStyle = Font.PLAIN;
  193.       else if (t.equalsIgnoreCase("bold"))
  194.         fontStyle = Font.BOLD;
  195.       else if (t.equalsIgnoreCase("italic"))
  196.         fontStyle = Font.ITALIC;
  197.       else if (t.equalsIgnoreCase("boldItalic"))
  198.         fontStyle = Font.BOLD + Font.ITALIC;
  199.  
  200.       // Font size.
  201.  
  202.       t = st.nextToken();
  203.       if ((n = Integer.parseInt(t)) > 0)
  204.           fontSize = n;
  205.     }
  206.     catch (Exception e) {}
  207.  
  208.     // Get the link data.
  209.  
  210.     s = null;
  211.     i = 1;
  212.     hit = Boolean.FALSE;
  213.     do {
  214.       try {
  215.         s = getParameter("link-" + i);
  216.         if (s != null) {
  217.           link = s;
  218.  
  219.           // Get the URL, target and status window message.
  220.  
  221.           url = null;
  222.           target = null;
  223.           try {
  224.             t = getParameter("url-" + i);
  225.             st = new StringTokenizer(t, ",");
  226.             u = st.nextToken();
  227.             try {
  228.               url = new URL(getDocumentBase(), u);
  229.               if (st.hasMoreTokens())
  230.                 target = st.nextToken();
  231.             }
  232.             catch (MalformedURLException e) {}
  233.           }
  234.           catch (Exception e) {}
  235.  
  236.           status = null;
  237.           try {
  238.             t = getParameter("status-" + i);
  239.             if (t != null)
  240.               status = t;
  241.             }
  242.           catch (Exception e) {}
  243.  
  244.           // Add the link data to the lists.
  245.  
  246.           linkList.addElement(link);
  247.           urlList.addElement(url);
  248.           targetList.addElement(target);
  249.           statusList.addElement(status);
  250.           hitList.addElement(hit);
  251.  
  252.           i++;
  253.         }
  254.       }
  255.       catch (Exception e) {}
  256.     } while (s != null);
  257.  
  258.     // Set the font values.
  259.  
  260.     g = getGraphics();
  261.     d = size();
  262.     font = g.getFont();
  263.     g.setFont(font = new Font(fontName, fontStyle, fontSize));
  264.     fm = g.getFontMetrics();
  265.     xOffset = fm.getMaxAdvance();
  266.     yOffsetBase = size().height - (size().height - fm.getHeight()) / 2 - fm.getDescent();
  267.     yOffsetMin = -fm.getHeight() - fm.getDescent();
  268.     yOffsetMax = d.height + fm.getHeight();
  269.     yOffset = yOffsetMax;
  270.  
  271.     // Initialize the current link.
  272.  
  273.     link = (String) linkList.firstElement();
  274.     url  = (URL) urlList.firstElement();
  275.     status = (String) statusList.firstElement();
  276.     hit  = (Boolean) hitList.firstElement();
  277.     currentLink = 0;
  278.   }
  279.  
  280.   private Color getColorParm(String s) {
  281.  
  282.       int r, g, b;
  283.  
  284.       // Check if a pre-defined color is specified.
  285.  
  286.       if (s.equalsIgnoreCase("black"))
  287.         return(Color.black);
  288.       if (s.equalsIgnoreCase("blue"))
  289.         return(Color.blue);
  290.       if (s.equalsIgnoreCase("cyan"))
  291.         return(Color.cyan);
  292.       if (s.equalsIgnoreCase("darkGray"))
  293.         return(Color.darkGray);
  294.       if (s.equalsIgnoreCase("gray"))
  295.         return(Color.gray);
  296.       if (s.equalsIgnoreCase("green"))
  297.         return(Color.green);
  298.       if (s.equalsIgnoreCase("lightGray"))
  299.         return(Color.lightGray);
  300.       if (s.equalsIgnoreCase("magenta"))
  301.         return(Color.magenta);
  302.       if (s.equalsIgnoreCase("orange"))
  303.         return(Color.orange);
  304.       if (s.equalsIgnoreCase("pink"))
  305.         return(Color.pink);
  306.       if (s.equalsIgnoreCase("red"))
  307.         return(Color.red);
  308.       if (s.equalsIgnoreCase("white"))
  309.         return(Color.white);
  310.       if (s.equalsIgnoreCase("yellow"))
  311.         return(Color.yellow);
  312.  
  313.       // If the color is specified in HTML format, build it from
  314.       // the red, green, blue values.
  315.  
  316.       if (s.length() == 7 && s.charAt(0) == '#') {
  317.         r = Integer.parseInt(s.substring(1,3),16);
  318.         g = Integer.parseInt(s.substring(3,5),16);
  319.         b = Integer.parseInt(s.substring(5,7),16);
  320.         return(new Color(r, g, b));
  321.       }
  322.  
  323.       // Default to black.
  324.  
  325.       return(Color.black);
  326.   }
  327.  
  328.   public void start() {
  329.  
  330.       if (scrollThread == null) {
  331.         scrollThread = new Thread(this);
  332.         scrollThread.start();
  333.       }
  334.   }
  335.  
  336.   public void stop() {
  337.  
  338.     if (scrollThread != null) {
  339.       scrollThread.stop();
  340.       scrollThread = null;
  341.     }
  342.   }
  343.  
  344.   public boolean mouseEnter(Event e, int x, int y) {
  345.  
  346.     // Show address of current URL in status window.
  347.  
  348.     mouseIn = true;
  349.     if (status != null)
  350.       getAppletContext().showStatus(status);
  351.     else if (url != null)
  352.       getAppletContext().showStatus(url.toString());
  353.     else
  354.       getAppletContext().showStatus("");
  355.     return true;
  356.   }
  357.  
  358.   public boolean mouseExit(Event e, int x, int y) {
  359.  
  360.     // Clear status window.
  361.  
  362.     mouseIn = false;
  363.     getAppletContext().showStatus("");
  364.     return true;
  365.   }
  366.  
  367.   public boolean mouseDown(Event e, int x, int y) {
  368.  
  369.     int i;
  370.  
  371.     // Mark current link and any equivalent links as visited.
  372.  
  373.     hit = Boolean.TRUE;
  374.     hitList.setElementAt(hit, currentLink);
  375.     for (i = 0; i < hitList.size(); i++)
  376.       if (url.sameFile((URL) urlList.elementAt(i)))
  377.         hitList.setElementAt(hit, i);
  378.  
  379.     // Link to the current URL.
  380.  
  381.     if (url != null)
  382.       if (target != null)
  383.         getAppletContext().showDocument(url, target);
  384.       else
  385.         getAppletContext().showDocument(url);
  386.     return true;
  387.   }
  388.  
  389.   public void run() {
  390.  
  391.     // Lower this thread's priority.
  392.  
  393.     Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  394.  
  395.     long startTime;
  396.  
  397.     // Get the start time.
  398.  
  399.     startTime = System.currentTimeMillis();
  400.  
  401.     // This is the animation loop.
  402.  
  403.     while (Thread.currentThread() == scrollThread) {
  404.  
  405.       // Advance the animation if not paused.
  406.  
  407.       if (!paused) {
  408.  
  409.         // Go to the next link.
  410.  
  411.         if (--yOffset < yOffsetMin ) {
  412.           yOffset = yOffsetMax;
  413.           if (++currentLink >= linkList.size())
  414.             currentLink = 0;
  415.  
  416.           // Set the current link data.
  417.  
  418.           link = (String) linkList.elementAt(currentLink);
  419.           url = (URL) urlList.elementAt(currentLink);
  420.           target = (String) targetList.elementAt(currentLink);
  421.           status = (String) statusList.elementAt(currentLink);
  422.           hit = (Boolean) hitList.elementAt(currentLink);
  423.  
  424.           // Update the status window.
  425.  
  426.           if (mouseIn)
  427.             if (status != null)
  428.               getAppletContext().showStatus(status);
  429.             else if (url != null)
  430.               getAppletContext().showStatus(url.toString());
  431.             else
  432.               getAppletContext().showStatus("");
  433.         }
  434.  
  435.         if (yOffset == yOffsetBase)
  436.           paused = true;
  437.         repaint();
  438.         try {
  439.           startTime += delay;
  440.           Thread.sleep(Math.max(0, startTime - System.currentTimeMillis()));
  441.         }
  442.         catch (InterruptedException e) {
  443.           break;
  444.         }
  445.       }
  446.  
  447.       else {
  448.         try {
  449.           startTime += pause;
  450.           Thread.sleep(Math.max(0, startTime - System.currentTimeMillis()));
  451.         }
  452.         catch (InterruptedException e) {
  453.           break;
  454.         }
  455.         paused = false;
  456.       }
  457.     }
  458.   }
  459.  
  460.   public void paint(Graphics g) {
  461.  
  462.     update(g);
  463.   }
  464.  
  465.   public void update(Graphics g) {
  466.  
  467.     Dimension d = size();
  468.     int i;
  469.  
  470.     // Create the offscreen graphics context, if no good one exists.
  471.  
  472.     if (offGraphics == null || d.width != offDimension.width || d.height != offDimension.height) {
  473.       offDimension = d;
  474.       offImage = createImage(d.width, d.height);
  475.       offGraphics = offImage.getGraphics();
  476.     }
  477.  
  478.     // Fill in background.
  479.  
  480.     offGraphics.setColor(bgColor);
  481.     offGraphics.fillRect(0, 0, d.width, d.height);
  482.  
  483.     // Set the font.
  484.  
  485.     offGraphics.setFont(font);
  486.  
  487.     // Draw shadow.
  488.  
  489.     if (shxOffset != 0 || shyOffset != 0) {
  490.       offGraphics.setColor(shColor);
  491.       offGraphics.drawString(link, xOffset + shxOffset, yOffset + shyOffset);
  492.     }
  493.  
  494.     // Draw text.
  495.  
  496.     if (hit == Boolean.TRUE)
  497.       offGraphics.setColor(vlColor);
  498.     else
  499.       offGraphics.setColor(fgColor);
  500.     offGraphics.drawString(link, xOffset, yOffset);
  501.  
  502.     // Draw border.
  503.  
  504.     offGraphics.setColor(bdColor);
  505.     for (i = 0; i < bdSize; i++)
  506.       offGraphics.drawRect(i, i, d.width - 2 * i - 1, d.height - 2 * i - 1);
  507.  
  508.     // Paint the image onto the screen.
  509.  
  510.     g.drawImage(offImage, 0, 0, this);
  511.   }
  512. }
  513.