home *** CD-ROM | disk | FTP | other *** search
/ MACD 7 / MACD7.iso / www / weirdscience / babylon5 / applets / textscrollerapplet.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-10-26  |  8.8 KB  |  329 lines

  1. import java.applet.Applet;
  2. import java.applet.AudioClip;
  3. import java.awt.Color;
  4. import java.awt.Component;
  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.io.IOException;
  11. import java.io.InputStream;
  12. import java.net.MalformedURLException;
  13. import java.net.URL;
  14. import java.util.Hashtable;
  15. import java.util.NoSuchElementException;
  16. import java.util.StringTokenizer;
  17.  
  18. public class TextScrollerApplet extends Applet implements Runnable {
  19.    private static final int RUNNING = 1;
  20.    private static final int SUSPENDED = 2;
  21.    private static int defaultTimeout = 4;
  22.    private static int defaultScrollDistance = 5;
  23.    private static String defaultScrollingText = "Scrolling Text brought to you by Gregory S. Pogue (http://www.inforamp.net/~pogue/gregory/)";
  24.    private static String defaultFontName = "TimesRoman";
  25.    private static int defaultFontStyle = 1;
  26.    private static int defaultFontSize = 36;
  27.    private static String defaultVerticleAlignment = "CENTER";
  28.    private static Color defaultFontColor;
  29.    private static Color defaultBackgroundColor;
  30.    private static Hashtable myColorCache;
  31.    private static String appletInfoString;
  32.    private static String[][] appletParamList;
  33.    private Thread _kicker;
  34.    private int _kickerState = 1;
  35.    private String _scrollingText;
  36.    private AudioClip _audioClip;
  37.    private boolean _audioLoop = false;
  38.    private Image _frame;
  39.    private Graphics _offscreen;
  40.    private int _waitTimeout;
  41.    private int _scrollDistance;
  42.    private int _anchorX;
  43.    private int _anchorY;
  44.    private Color _fontColor;
  45.    private Color _backgroundColor;
  46.  
  47.    public void init() {
  48.       Object var2 = null;
  49.       int var4 = 0;
  50.       String var1 = ((Applet)this).getParameter("speed");
  51.  
  52.       try {
  53.          this._waitTimeout = 1000 / (var1 == null ? defaultTimeout : Integer.valueOf(var1));
  54.       } catch (Exception var13) {
  55.          this._waitTimeout = defaultTimeout;
  56.       }
  57.  
  58.       var1 = ((Applet)this).getParameter("distance");
  59.  
  60.       try {
  61.          this._scrollDistance = var1 == null ? defaultScrollDistance : Integer.valueOf(var1);
  62.       } catch (NumberFormatException var12) {
  63.          this._scrollDistance = defaultScrollDistance;
  64.       }
  65.  
  66.       var1 = ((Applet)this).getParameter("remoteText");
  67.       if (var1 != null) {
  68.          try {
  69.             var25 = new URL(((Applet)this).getDocumentBase(), var1);
  70.          } catch (MalformedURLException var11) {
  71.             var25 = null;
  72.          }
  73.  
  74.          if (var25 != null) {
  75.             this._scrollingText = this.getRemoteString(var25);
  76.          }
  77.       }
  78.  
  79.       if (this._scrollingText == null) {
  80.          var1 = ((Applet)this).getParameter("text");
  81.          this._scrollingText = var1 == null ? defaultScrollingText : var1;
  82.       }
  83.  
  84.       var1 = ((Applet)this).getParameter("font");
  85.       String var3 = var1 == null ? defaultFontName : var1;
  86.       var1 = ((Applet)this).getParameter("size");
  87.  
  88.       int var5;
  89.       try {
  90.          var5 = var1 == null ? defaultFontSize : Integer.valueOf(var1);
  91.       } catch (NumberFormatException var10) {
  92.          var5 = defaultFontSize;
  93.       }
  94.  
  95.       var1 = ((Applet)this).getParameter("style");
  96.       if (var1 != null) {
  97.          var1 = var1.toUpperCase();
  98.          var4 += var1.indexOf("BOLD") < 0 ? 0 : 1;
  99.          var4 += var1.indexOf("ITALIC") < 0 ? 0 : 2;
  100.          var4 += var1.indexOf("PLAIN") < 0 ? 0 : 0;
  101.       }
  102.  
  103.       if (var1 == null || var4 == 0) {
  104.          var4 = defaultFontStyle;
  105.       }
  106.  
  107.       Font var6 = new Font(var3, var4, var5);
  108.       FontMetrics var7 = ((Component)this).getFontMetrics(var6);
  109.       var1 = ((Applet)this).getParameter("textColor");
  110.       if (var1 != null) {
  111.          this._fontColor = this.colorFromString(var1);
  112.       }
  113.  
  114.       if (this._fontColor == null) {
  115.          this._fontColor = defaultFontColor;
  116.       }
  117.  
  118.       var1 = ((Applet)this).getParameter("backgroundColor");
  119.       if (var1 != null) {
  120.          this._backgroundColor = this.colorFromString(var1);
  121.       }
  122.  
  123.       if (this._backgroundColor == null) {
  124.          this._backgroundColor = defaultBackgroundColor;
  125.       }
  126.  
  127.       ((Component)this).setBackground(this._backgroundColor);
  128.       var1 = ((Applet)this).getParameter("alignment");
  129.       String var8 = (var1 == null ? defaultVerticleAlignment : var1).toUpperCase();
  130.       if (var8.equals("TOP")) {
  131.          this._anchorY = var7.getHeight() - var7.getDescent();
  132.       } else if (var8.equals("BOTTOM")) {
  133.          this._anchorY = ((Component)this).size().height - var7.getDescent();
  134.       } else {
  135.          this._anchorY = (((Component)this).size().height - var7.getHeight()) / 2 + var7.getLeading() + var7.getAscent();
  136.       }
  137.  
  138.       var1 = ((Applet)this).getParameter("audioClip");
  139.       if (var1 != null) {
  140.          try {
  141.             this._audioClip = ((Applet)this).getAudioClip(new URL(((Applet)this).getDocumentBase(), var1));
  142.          } catch (MalformedURLException var9) {
  143.             this._audioClip = null;
  144.          }
  145.  
  146.          if (this._audioClip != null) {
  147.             var1 = ((Applet)this).getParameter("audioLoop");
  148.             if (var1 != null && var1.equalsIgnoreCase("ON")) {
  149.                this._audioLoop = true;
  150.                this._audioClip.loop();
  151.             } else {
  152.                this._audioLoop = false;
  153.                this._audioClip.play();
  154.             }
  155.          }
  156.       }
  157.  
  158.       this._frame = ((Component)this).createImage(var7.stringWidth(this._scrollingText) + this._scrollDistance * 30, ((Component)this).size().height);
  159.       this._offscreen = this._frame.getGraphics();
  160.       this._offscreen.setFont(var6);
  161.       this._offscreen.setColor(this._backgroundColor);
  162.       this._offscreen.fillRect(0, 0, this._frame.getWidth(this), this._frame.getHeight(this));
  163.       this._offscreen.setColor(this._fontColor);
  164.       this._offscreen.drawString(this._scrollingText, 0, this._anchorY);
  165.    }
  166.  
  167.    public void start() {
  168.       if (this._kicker == null && this._scrollingText != null) {
  169.          this._kicker = new Thread(this);
  170.          this._anchorX = ((Component)this).size().width;
  171.          if (this._audioClip != null) {
  172.             if (this._audioLoop) {
  173.                this._audioClip.loop();
  174.             } else {
  175.                this._audioClip.play();
  176.             }
  177.          }
  178.  
  179.          this._kicker.start();
  180.       }
  181.  
  182.    }
  183.  
  184.    public void stop() {
  185.       if (this._kicker != null) {
  186.          this._kicker.stop();
  187.          this._kicker = null;
  188.       }
  189.  
  190.       if (this._audioClip != null) {
  191.          this._audioClip.stop();
  192.       }
  193.  
  194.    }
  195.  
  196.    public synchronized boolean mouseDown(Event var1, int var2, int var3) {
  197.       if (this._kicker != null) {
  198.          if (this._kickerState == 1) {
  199.             this._kicker.suspend();
  200.             this._kickerState = 2;
  201.             if (this._audioClip != null) {
  202.                this._audioClip.stop();
  203.             }
  204.          } else {
  205.             this._kicker.resume();
  206.             this._kickerState = 1;
  207.             if (this._audioClip != null) {
  208.                if (this._audioLoop) {
  209.                   this._audioClip.loop();
  210.                } else {
  211.                   this._audioClip.play();
  212.                }
  213.             }
  214.          }
  215.       }
  216.  
  217.       return true;
  218.    }
  219.  
  220.    public void run() {
  221.       while(true) {
  222.          try {
  223.             Thread.currentThread();
  224.             Thread.sleep((long)this._waitTimeout);
  225.          } catch (InterruptedException var1) {
  226.          }
  227.  
  228.          this.scrollText(this._scrollDistance);
  229.       }
  230.    }
  231.  
  232.    private synchronized void scrollText(int var1) {
  233.       this._anchorX = this._anchorX < (this._frame.getWidth(this) + this._scrollDistance) * -1 ? ((Component)this).size().width : this._anchorX - this._scrollDistance;
  234.       ((Component)this).repaint();
  235.    }
  236.  
  237.    public void update(Graphics var1) {
  238.       this.paint(var1);
  239.    }
  240.  
  241.    public void paint(Graphics var1) {
  242.       var1.drawImage(this._frame, this._anchorX, 0, this);
  243.    }
  244.  
  245.    protected String getRemoteString(URL var1) {
  246.       StringBuffer var3 = new StringBuffer();
  247.       int var4 = 0;
  248.       String var5 = null;
  249.  
  250.       InputStream var2;
  251.       try {
  252.          var2 = var1.openStream();
  253.       } catch (Exception var7) {
  254.          var2 = null;
  255.       }
  256.  
  257.       if (var2 != null) {
  258.          try {
  259.             while(var4 >= 0) {
  260.                var4 = var2.read();
  261.                if (var4 >= 0) {
  262.                   var3.append((char)var4);
  263.                }
  264.             }
  265.  
  266.             var5 = var3.toString();
  267.             var2.close();
  268.          } catch (IOException var8) {
  269.             var5 = null;
  270.  
  271.             try {
  272.                var2.close();
  273.             } catch (IOException var6) {
  274.             }
  275.          }
  276.       }
  277.  
  278.       return var5;
  279.    }
  280.  
  281.    protected Color colorFromString(String var1) {
  282.       String var3 = var1.toLowerCase();
  283.       Color var2 = (Color)myColorCache.get(var3);
  284.       if (var2 == null) {
  285.          StringTokenizer var4 = new StringTokenizer(var3, ",./;:-");
  286.  
  287.          try {
  288.             int var5 = Integer.valueOf(var4.nextToken());
  289.             int var6 = Integer.valueOf(var4.nextToken());
  290.             int var7 = Integer.valueOf(var4.nextToken());
  291.             var2 = new Color(var5, var6, var7);
  292.          } catch (NoSuchElementException var8) {
  293.          } catch (NumberFormatException var9) {
  294.          }
  295.       }
  296.  
  297.       return var2;
  298.    }
  299.  
  300.    public String getAppletInfo() {
  301.       return appletInfoString;
  302.    }
  303.  
  304.    public String[][] getParameterInfo() {
  305.       return appletParamList;
  306.    }
  307.  
  308.    static {
  309.       defaultFontColor = Color.blue;
  310.       defaultBackgroundColor = Color.white;
  311.       myColorCache = new Hashtable();
  312.       appletInfoString = "TextScrollerApplet, Version 3.00, 18/02/1996, Gregory S. Pogue [pogue@inforamp.net]";
  313.       appletParamList = new String[][]{{"speed", "1-1000", "a relative speed adjustment (the larger the number, the faster the text)"}, {"distance", "1-100", "the number of pixels to move the text between each frame"}, {"remoteText", "url", "a url of a file containing the text to scroll (relative or absolute)"}, {"text", "string", "the text to be scrolled (remoteText will override this, unless there is an error reading from the url)"}, {"font", "font", "the name of the font to use for the text"}, {"size", "6-100", "the point size of the font"}, {"style", "BOLD ITALIC PLAIN", "the style of the font (can be a combination)"}, {"textColor", "name or r,g,b", "color of the text (can be specified as Red or 255,0,0)"}, {"backgroundColor", "name or r,g,b", "color of the background (can be specified as Red or 255,0,0)"}, {"alignment", "TOP BOTTOM CENTER", "alignment of the text vertically within the applet"}, {"audioClip", "url", "a url of a file containing audio to play"}, {"audioLoop", "YES NO", "an indicator which allows an audio file to be played continuously"}};
  314.       myColorCache.put("red", Color.red);
  315.       myColorCache.put("green", Color.green);
  316.       myColorCache.put("blue", Color.blue);
  317.       myColorCache.put("cyan", Color.cyan);
  318.       myColorCache.put("magenta", Color.magenta);
  319.       myColorCache.put("yellow", Color.yellow);
  320.       myColorCache.put("orange", Color.orange);
  321.       myColorCache.put("pink", Color.pink);
  322.       myColorCache.put("white", Color.white);
  323.       myColorCache.put("lightgray", Color.lightGray);
  324.       myColorCache.put("gray", Color.gray);
  325.       myColorCache.put("darkgray", Color.darkGray);
  326.       myColorCache.put("black", Color.black);
  327.    }
  328. }
  329.