home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 October / PCO1097.ISO / FilesBBS / FREI / LSCROLL.EXE / LinkScroll.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-09-02  |  8.3 KB  |  428 lines

  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.Event;
  6. import java.awt.Font;
  7. import java.awt.FontMetrics;
  8. import java.awt.Graphics;
  9. import java.awt.Image;
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  12. import java.util.StringTokenizer;
  13. import java.util.Vector;
  14.  
  15. public class LinkScroll extends Applet implements Runnable {
  16.    Thread scrollThread;
  17.    boolean paused = false;
  18.    int delay = 25;
  19.    int pause = 2000;
  20.    Color fgColor;
  21.    Color bgColor;
  22.    Color vlColor;
  23.    int shxOffset;
  24.    int shyOffset;
  25.    Color shColor;
  26.    int bdSize;
  27.    Color bdColor;
  28.    String fontName;
  29.    int fontStyle;
  30.    int fontSize;
  31.    Vector linkList;
  32.    Vector urlList;
  33.    Vector targetList;
  34.    Vector statusList;
  35.    Vector hitList;
  36.    String link;
  37.    URL url;
  38.    String target;
  39.    String status;
  40.    Boolean hit;
  41.    int currentLink;
  42.    int xOffset;
  43.    int yOffset;
  44.    int yOffsetBase;
  45.    int yOffsetMax;
  46.    int yOffsetMin;
  47.    Font font;
  48.    Dimension offDimension;
  49.    Image offImage;
  50.    Graphics offGraphics;
  51.    boolean mouseIn;
  52.  
  53.    public void init() {
  54.       try {
  55.          String var1 = ((Applet)this).getParameter("delay");
  56.          int var6;
  57.          if (var1 != null && (var6 = Integer.parseInt(var1)) > 0) {
  58.             this.delay = var6;
  59.          }
  60.       } catch (Exception var21) {
  61.       }
  62.  
  63.       try {
  64.          String var22 = ((Applet)this).getParameter("pause");
  65.          int var37;
  66.          if (var22 != null && (var37 = Integer.parseInt(var22)) > 0) {
  67.             this.pause = var37;
  68.          }
  69.       } catch (Exception var20) {
  70.       }
  71.  
  72.       try {
  73.          String var23 = ((Applet)this).getParameter("fgcolor");
  74.          if (var23 != null) {
  75.             this.fgColor = this.getColorParm(var23);
  76.          }
  77.       } catch (Exception var19) {
  78.       }
  79.  
  80.       this.bdColor = this.fgColor;
  81.  
  82.       try {
  83.          String var24 = ((Applet)this).getParameter("bgcolor");
  84.          if (var24 != null) {
  85.             this.bgColor = this.getColorParm(var24);
  86.          }
  87.       } catch (Exception var18) {
  88.       }
  89.  
  90.       try {
  91.          String var25 = ((Applet)this).getParameter("vlcolor");
  92.          if (var25 != null) {
  93.             this.vlColor = this.getColorParm(var25);
  94.          }
  95.       } catch (Exception var17) {
  96.       }
  97.  
  98.       try {
  99.          String var26 = ((Applet)this).getParameter("shadow");
  100.          if (var26 != null) {
  101.             StringTokenizer var4 = new StringTokenizer(var26, ",");
  102.             int var38;
  103.             if ((var38 = Integer.parseInt(var4.nextToken())) > 0) {
  104.                this.shxOffset = var38;
  105.             }
  106.  
  107.             if ((var38 = Integer.parseInt(var4.nextToken())) > 0) {
  108.                this.shyOffset = var38;
  109.             }
  110.  
  111.             this.shColor = this.getColorParm(var4.nextToken());
  112.          }
  113.       } catch (Exception var16) {
  114.       }
  115.  
  116.       try {
  117.          String var27 = ((Applet)this).getParameter("border");
  118.          if (var27 != null) {
  119.             StringTokenizer var34 = new StringTokenizer(var27, ",");
  120.             int var40;
  121.             if ((var40 = Integer.parseInt(var34.nextToken())) > 0) {
  122.                this.bdSize = var40;
  123.             }
  124.  
  125.             this.bdColor = this.getColorParm(var34.nextToken());
  126.          }
  127.       } catch (Exception var15) {
  128.       }
  129.  
  130.       try {
  131.          String var28 = ((Applet)this).getParameter("font");
  132.          StringTokenizer var35 = new StringTokenizer(var28, ",");
  133.          String var2 = var35.nextToken();
  134.          if (var2.equalsIgnoreCase("Courier")) {
  135.             this.fontName = "Courier";
  136.          } else if (var2.equalsIgnoreCase("Dialog")) {
  137.             this.fontName = "Dialog";
  138.          } else if (var2.equalsIgnoreCase("Helvetica")) {
  139.             this.fontName = "Helvetica";
  140.          } else if (var2.equalsIgnoreCase("Symbol")) {
  141.             this.fontName = "Symbol";
  142.          } else if (var2.equalsIgnoreCase("TimesRoman")) {
  143.             this.fontName = "TimesRoman";
  144.          }
  145.  
  146.          var2 = var35.nextToken();
  147.          if (var2.equalsIgnoreCase("plain")) {
  148.             this.fontStyle = 0;
  149.          } else if (var2.equalsIgnoreCase("bold")) {
  150.             this.fontStyle = 1;
  151.          } else if (var2.equalsIgnoreCase("italic")) {
  152.             this.fontStyle = 2;
  153.          } else if (var2.equalsIgnoreCase("boldItalic")) {
  154.             this.fontStyle = 3;
  155.          }
  156.  
  157.          var2 = var35.nextToken();
  158.          int var41;
  159.          if ((var41 = Integer.parseInt(var2)) > 0) {
  160.             this.fontSize = var41;
  161.          }
  162.       } catch (Exception var14) {
  163.       }
  164.  
  165.       String var29 = null;
  166.       int var5 = 1;
  167.       this.hit = Boolean.FALSE;
  168.  
  169.       do {
  170.          try {
  171.             var29 = ((Applet)this).getParameter("link-" + var5);
  172.             if (var29 != null) {
  173.                this.link = var29;
  174.                this.url = null;
  175.                this.target = null;
  176.  
  177.                try {
  178.                   String var32 = ((Applet)this).getParameter("url-" + var5);
  179.                   StringTokenizer var36 = new StringTokenizer(var32, ",");
  180.                   String var3 = var36.nextToken();
  181.  
  182.                   try {
  183.                      this.url = new URL(((Applet)this).getDocumentBase(), var3);
  184.                      if (var36.hasMoreTokens()) {
  185.                         this.target = var36.nextToken();
  186.                      }
  187.                   } catch (MalformedURLException var11) {
  188.                   }
  189.                } catch (Exception var12) {
  190.                }
  191.  
  192.                this.status = null;
  193.  
  194.                try {
  195.                   String var33 = ((Applet)this).getParameter("status-" + var5);
  196.                   if (var33 != null) {
  197.                      this.status = var33;
  198.                   }
  199.                } catch (Exception var10) {
  200.                }
  201.  
  202.                this.linkList.addElement(this.link);
  203.                this.urlList.addElement(this.url);
  204.                this.targetList.addElement(this.target);
  205.                this.statusList.addElement(this.status);
  206.                this.hitList.addElement(this.hit);
  207.                ++var5;
  208.             }
  209.          } catch (Exception var13) {
  210.          }
  211.       } while(var29 != null);
  212.  
  213.       Graphics var7 = ((Component)this).getGraphics();
  214.       Dimension var8 = ((Component)this).size();
  215.       this.font = var7.getFont();
  216.       var7.setFont(this.font = new Font(this.fontName, this.fontStyle, this.fontSize));
  217.       FontMetrics var9 = var7.getFontMetrics();
  218.       this.xOffset = var9.getMaxAdvance();
  219.       this.yOffsetBase = ((Component)this).size().height - (((Component)this).size().height - var9.getHeight()) / 2 - var9.getDescent();
  220.       this.yOffsetMin = -var9.getHeight() - var9.getDescent();
  221.       this.yOffsetMax = var8.height + var9.getHeight();
  222.       this.yOffset = this.yOffsetMax;
  223.       this.link = (String)this.linkList.firstElement();
  224.       this.url = (URL)this.urlList.firstElement();
  225.       this.status = (String)this.statusList.firstElement();
  226.       this.hit = (Boolean)this.hitList.firstElement();
  227.       this.currentLink = 0;
  228.    }
  229.  
  230.    private Color getColorParm(String var1) {
  231.       if (var1.equalsIgnoreCase("black")) {
  232.          return Color.black;
  233.       } else if (var1.equalsIgnoreCase("blue")) {
  234.          return Color.blue;
  235.       } else if (var1.equalsIgnoreCase("cyan")) {
  236.          return Color.cyan;
  237.       } else if (var1.equalsIgnoreCase("darkGray")) {
  238.          return Color.darkGray;
  239.       } else if (var1.equalsIgnoreCase("gray")) {
  240.          return Color.gray;
  241.       } else if (var1.equalsIgnoreCase("green")) {
  242.          return Color.green;
  243.       } else if (var1.equalsIgnoreCase("lightGray")) {
  244.          return Color.lightGray;
  245.       } else if (var1.equalsIgnoreCase("magenta")) {
  246.          return Color.magenta;
  247.       } else if (var1.equalsIgnoreCase("orange")) {
  248.          return Color.orange;
  249.       } else if (var1.equalsIgnoreCase("pink")) {
  250.          return Color.pink;
  251.       } else if (var1.equalsIgnoreCase("red")) {
  252.          return Color.red;
  253.       } else if (var1.equalsIgnoreCase("white")) {
  254.          return Color.white;
  255.       } else if (var1.equalsIgnoreCase("yellow")) {
  256.          return Color.yellow;
  257.       } else if (var1.length() == 7 && var1.charAt(0) == '#') {
  258.          int var2 = Integer.parseInt(var1.substring(1, 3), 16);
  259.          int var3 = Integer.parseInt(var1.substring(3, 5), 16);
  260.          int var4 = Integer.parseInt(var1.substring(5, 7), 16);
  261.          return new Color(var2, var3, var4);
  262.       } else {
  263.          return Color.black;
  264.       }
  265.    }
  266.  
  267.    public void start() {
  268.       if (this.scrollThread == null) {
  269.          this.scrollThread = new Thread(this);
  270.          this.scrollThread.start();
  271.       }
  272.  
  273.    }
  274.  
  275.    public void stop() {
  276.       if (this.scrollThread != null) {
  277.          this.scrollThread.stop();
  278.          this.scrollThread = null;
  279.       }
  280.  
  281.    }
  282.  
  283.    public boolean mouseEnter(Event var1, int var2, int var3) {
  284.       this.mouseIn = true;
  285.       if (this.status != null) {
  286.          ((Applet)this).getAppletContext().showStatus(this.status);
  287.       } else if (this.url != null) {
  288.          ((Applet)this).getAppletContext().showStatus(this.url.toString());
  289.       } else {
  290.          ((Applet)this).getAppletContext().showStatus("");
  291.       }
  292.  
  293.       return true;
  294.    }
  295.  
  296.    public boolean mouseExit(Event var1, int var2, int var3) {
  297.       this.mouseIn = false;
  298.       ((Applet)this).getAppletContext().showStatus("");
  299.       return true;
  300.    }
  301.  
  302.    public boolean mouseDown(Event var1, int var2, int var3) {
  303.       this.hit = Boolean.TRUE;
  304.       this.hitList.setElementAt(this.hit, this.currentLink);
  305.  
  306.       for(int var4 = 0; var4 < this.hitList.size(); ++var4) {
  307.          if (this.url.sameFile((URL)this.urlList.elementAt(var4))) {
  308.             this.hitList.setElementAt(this.hit, var4);
  309.          }
  310.       }
  311.  
  312.       if (this.url != null) {
  313.          if (this.target != null) {
  314.             ((Applet)this).getAppletContext().showDocument(this.url, this.target);
  315.          } else {
  316.             ((Applet)this).getAppletContext().showDocument(this.url);
  317.          }
  318.       }
  319.  
  320.       return true;
  321.    }
  322.  
  323.    public void run() {
  324.       Thread.currentThread().setPriority(1);
  325.       long var1 = System.currentTimeMillis();
  326.  
  327.       while(Thread.currentThread() == this.scrollThread) {
  328.          if (!this.paused) {
  329.             if (--this.yOffset < this.yOffsetMin) {
  330.                this.yOffset = this.yOffsetMax;
  331.                if (++this.currentLink >= this.linkList.size()) {
  332.                   this.currentLink = 0;
  333.                }
  334.  
  335.                this.link = (String)this.linkList.elementAt(this.currentLink);
  336.                this.url = (URL)this.urlList.elementAt(this.currentLink);
  337.                this.target = (String)this.targetList.elementAt(this.currentLink);
  338.                this.status = (String)this.statusList.elementAt(this.currentLink);
  339.                this.hit = (Boolean)this.hitList.elementAt(this.currentLink);
  340.                if (this.mouseIn) {
  341.                   if (this.status != null) {
  342.                      ((Applet)this).getAppletContext().showStatus(this.status);
  343.                   } else if (this.url != null) {
  344.                      ((Applet)this).getAppletContext().showStatus(this.url.toString());
  345.                   } else {
  346.                      ((Applet)this).getAppletContext().showStatus("");
  347.                   }
  348.                }
  349.             }
  350.  
  351.             if (this.yOffset == this.yOffsetBase) {
  352.                this.paused = true;
  353.             }
  354.  
  355.             ((Component)this).repaint();
  356.  
  357.             try {
  358.                var1 += (long)this.delay;
  359.                Thread.sleep(Math.max(0L, var1 - System.currentTimeMillis()));
  360.             } catch (InterruptedException var4) {
  361.                return;
  362.             }
  363.          } else {
  364.             try {
  365.                var1 += (long)this.pause;
  366.                Thread.sleep(Math.max(0L, var1 - System.currentTimeMillis()));
  367.             } catch (InterruptedException var3) {
  368.                return;
  369.             }
  370.  
  371.             this.paused = false;
  372.          }
  373.       }
  374.  
  375.    }
  376.  
  377.    public void paint(Graphics var1) {
  378.       this.update(var1);
  379.    }
  380.  
  381.    public void update(Graphics var1) {
  382.       Dimension var2 = ((Component)this).size();
  383.       if (this.offGraphics == null || var2.width != this.offDimension.width || var2.height != this.offDimension.height) {
  384.          this.offDimension = var2;
  385.          this.offImage = ((Component)this).createImage(var2.width, var2.height);
  386.          this.offGraphics = this.offImage.getGraphics();
  387.       }
  388.  
  389.       this.offGraphics.setColor(this.bgColor);
  390.       this.offGraphics.fillRect(0, 0, var2.width, var2.height);
  391.       this.offGraphics.setFont(this.font);
  392.       if (this.shxOffset != 0 || this.shyOffset != 0) {
  393.          this.offGraphics.setColor(this.shColor);
  394.          this.offGraphics.drawString(this.link, this.xOffset + this.shxOffset, this.yOffset + this.shyOffset);
  395.       }
  396.  
  397.       if (this.hit == Boolean.TRUE) {
  398.          this.offGraphics.setColor(this.vlColor);
  399.       } else {
  400.          this.offGraphics.setColor(this.fgColor);
  401.       }
  402.  
  403.       this.offGraphics.drawString(this.link, this.xOffset, this.yOffset);
  404.       this.offGraphics.setColor(this.bdColor);
  405.  
  406.       for(int var3 = 0; var3 < this.bdSize; ++var3) {
  407.          this.offGraphics.drawRect(var3, var3, var2.width - 2 * var3 - 1, var2.height - 2 * var3 - 1);
  408.       }
  409.  
  410.       var1.drawImage(this.offImage, 0, 0, this);
  411.    }
  412.  
  413.    public LinkScroll() {
  414.       this.fgColor = Color.blue;
  415.       this.bgColor = Color.white;
  416.       this.vlColor = new Color(102, 51, 153);
  417.       this.shColor = Color.lightGray;
  418.       this.fontName = "Dialog";
  419.       this.fontSize = 12;
  420.       this.linkList = new Vector();
  421.       this.urlList = new Vector();
  422.       this.targetList = new Vector();
  423.       this.statusList = new Vector();
  424.       this.hitList = new Vector();
  425.       this.mouseIn = false;
  426.    }
  427. }
  428.