home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 12,000 to 12,999 / 12000.zip / AOLDLs / Online-Tools / Java-Applets / JAVAAPPS.lzh / JAVAAPPS / TICKER / TICKER.EXE / ticker.java < prev   
Encoding:
Java Source  |  1996-04-05  |  10.9 KB  |  369 lines

  1. /*
  2.  * Permission to use, copy, modify and distribute this software and its
  3.  * documentation without fee for NON-COMMERCIAL purposes is hereby granted
  4.  * provided that this notice with a reference to the original source and 
  5.  * the author appears in all copies or derivatives of this software.
  6.  *
  7.  * THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  8.  * THIS SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  9.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  10.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR
  11.  * ANY DAMAGES SUFFERED BY ANYBODY AS A RESULT OF USING, MODIFYING OR
  12.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  13.  */
  14.  
  15. import java.awt.*;
  16. import java.lang.*;
  17. import java.net.URL;
  18. import java.util.*;
  19.  
  20. /**
  21.  * ticker - Scrolling Text with colors, speed, url, timedependant starting and expiring<br>
  22.  * Some Ideas from Sven Heinicke, Author of TickerTape<br>
  23.  * 
  24.  * <p>V b1.00, 10/06/95: Creation by Thomas Wendt (thw)<br>
  25.  * V b1.01, 11/18/95: Cleaning up the code by thw<br>
  26.  * V b1.10, 11/28/95: Fixed mem leakage, synchronized run and update by thw<br>
  27.  * V b1.20, 12/31/95: Added new features by thw<br>
  28.  * V b1.21, 01/03/96: bugfix: stop thread by thw<br>
  29.  * V b1.30, 02/07/96: Added support for multilingual use by thw; netscapes bug in doing this
  30.  * and a workaround found by <a href="mailto:Peter.Sylvester@edelweb.fr">Peter Sylvester</a><br>
  31.  *
  32.  * <p><a href="http://www.uni-kassel.de/fb16/ipm/mt/java/javae.html"><b>origin</b></a> of ticker.<br>
  33.  *
  34.  * @author     <A HREF="http://www.uni-kassel.de/fb16/ipm/mt/staff/thwendte.html">Thomas Wendt</A>
  35.  * @version     b1.30, 02/07/96
  36.  */
  37. public class ticker extends java.applet.Applet implements Runnable {
  38.   /** The offscreen image */
  39.   public Image im = null;
  40.  
  41.   /** The offscreen drawing context */
  42.   public Graphics gr = null;
  43.  
  44.   /** The message to be displayed. */
  45.   public String message;
  46.  
  47.   /** The Font to be displayed. */
  48.   public Font messageF;
  49.  
  50.   /** x-position of message. */
  51.   public int messageX;
  52.  
  53.   /** y-position of message. */
  54.   public int messageY;
  55.  
  56.   /** Length of the message. */
  57.   public int messageW = 0;
  58.  
  59.   /** URL to switch to */
  60.   public URL url_ = null;
  61.  
  62.   /** How far to skip across the screen. */
  63.   int speed;
  64.  
  65.   /** The animating thread. */
  66.   public Thread t = null;
  67.  
  68.   /** The Color of the Text. */
  69.   public Color txtCo;
  70.  
  71.   /** The textstyle. */
  72.   public int txtStyle;
  73.  
  74.   /** The Color of the first textshadow. */
  75.   public Color shCo1;
  76.  
  77.   /** The Color of the second textshadow. */
  78.   public Color shCo2;
  79.  
  80.   /** The Color of the frame. */
  81.   public Color hrefCo;
  82.  
  83.   /** The backgroundcolor. */
  84.   public Color bgCo;
  85.  
  86.   /** True, if to be filled with bgCo after expiration */
  87.   public boolean ExFill;
  88.  
  89.   /**
  90.    * The time-dependant state of the applet.
  91.    * True, if not started or expired
  92.    */
  93.   public boolean expired = false;
  94.  
  95.   /** Flag for using shadow */
  96.   public boolean useShadow;
  97.  
  98.   /** Flag for using frame if url is given */
  99.   public boolean useFrame;
  100.  
  101.   /** The Size used to calc the Font. */
  102.   public Dimension lastS = new Dimension(1,1);
  103.  
  104.   /**
  105.    * Fix a netscape bug: We get 0xffcc, if we should
  106.    * get 0x00cc. To make it possible to display non-ascii
  107.    * characters, we need a workaround and forget uni-code.<br>
  108.    * Author of the following method is
  109.    * <a href="mailto:Peter.Sylvester@edelweb.fr">Peter Sylvester</a>
  110.    */
  111.   public String fixedgetParameter(String s) {
  112.     char ec[] = s.toCharArray();
  113.     for (int i=0; i < ec.length; i++)
  114.       ec[i] &= 0x00ff ;
  115.     return(new String(ec)) ;
  116.   }
  117.  
  118.   /**
  119.    * Initialize: Read Attributes
  120.    * Resize to (2,2) and do nothing if expired;
  121.    */
  122.   public void init () {
  123.     Date today = new Date();
  124.     Date anyDay;
  125.     String at = getParameter("msg");
  126.     message = (at == null) ? "ticker for beta" : fixedgetParameter(at);
  127.  
  128.     // use readColor to read the Date; date and color both have 3 components.
  129.     bgCo = readColor(getParameter("exp"), Color.white);
  130.     if (!bgCo.equals(Color.white)) {
  131.       anyDay = new Date(bgCo.getRed(),bgCo.getGreen()-1,bgCo.getBlue());
  132.       expired = today.after(anyDay);
  133.     }
  134.  
  135.     if (!expired) {
  136.       bgCo = readColor(getParameter("start"), Color.black);
  137.       if (!bgCo.equals(Color.black)) {
  138.         anyDay = new Date(bgCo.getRed(),bgCo.getGreen()-1,bgCo.getBlue());
  139.         expired = anyDay.after(today);
  140.       }
  141.     }
  142.     // don't show, if expired
  143.     ExFill = (getParameter("exfill") != null);
  144.     if (expired && !ExFill) {
  145.       resize(2,2);
  146.       return;
  147.     }
  148.     
  149.     speed = ((at = getParameter("speed")) == null) ? 10 : (Integer.valueOf(at).intValue());
  150.  
  151.     if ((at = getParameter("href")) != null) { 
  152.       try { url_ = new URL(getDocumentBase(), at); }
  153.       catch (Exception e) { url_ = null; }
  154.     }
  155.     // use default txtco = blue, if url is given; black otherwise
  156.     if (url_ == null) { bgCo = Color.black; }
  157.     else { bgCo = Color.blue; }
  158.     
  159.     // get the colors
  160.     txtCo = readColor(getParameter("txtco"), bgCo);
  161.     bgCo = readColor(getParameter("bgco"), getBackground());
  162.     shCo2 = readColor(getParameter("shco"), bgCo);
  163.     useShadow = !(shCo2.equals(bgCo));
  164.     hrefCo = readColor(getParameter("hrefco"), Color.blue);
  165.     useFrame = !(hrefCo.equals(bgCo));
  166.  
  167.     txtStyle = useShadow ? Font.PLAIN : Font.BOLD;
  168.     if (useShadow) {
  169.       int r = (shCo2.getRed()+txtCo.getRed()) >> 1;
  170.       int g = (shCo2.getGreen()+txtCo.getGreen()) >> 1;
  171.       int b = (shCo2.getBlue()+txtCo.getBlue()) >> 1;
  172.       shCo1 = new Color(r,g,b);
  173.     }
  174.   }
  175.  
  176.   /** Parameter Info. */
  177.   public String[][] getParameterInfo() {
  178.     String[][] info = {
  179.       {"msg",     "String",  "Message to display"},
  180.       {"href",    "String",  "url to switch to"},
  181.       {"speed",   "int",     "animation speed in pixels (10)"},
  182.       {"txtco",   "int[3]",  "RGB-Color of Message (black/blue)"},
  183.       {"hrefco",  "int[3]",  "RGB-Color of Frame (blue)"},
  184.       {"bgco",    "int[3]",  "RGB-Color of background (getBackground)"},
  185.       {"shco",    "int[3]",  "RGB-Color of Message (black/blue)"},
  186.       {"start",   "int[3]",  "Date to start: Y, M, D; if not set, show"},
  187.       {"exp",     "int[3]",  "Date to expire: Y, M, D; if not set, no expiration"},
  188.       {"exfill",  "",        "If exist, fill with bgco, if expired"},
  189.     };
  190.     return info;
  191.   }
  192.  
  193.   /** Applet Info. */
  194.   public String getAppletInfo() {
  195.     return "ticker.java, V b1.30, 02/07/96 by Thomas Wendt, http://www.uni-kassel.de/fb16/ipm/mt/staff/thwendte.html";
  196.   }    
  197.  
  198.   /**
  199.    * Convert a ","-delimited String with RGB-Values to Color
  200.    * Uses aDefault, if no or not enough RGB-Values
  201.    */
  202.   public Color readColor(String aColor, Color aDefault) {
  203.     if (aColor == null) { return aDefault; }
  204.  
  205.     int r, g, b;
  206.     StringTokenizer st = new StringTokenizer(aColor, ",");
  207.     
  208.     try {
  209.       r = Integer.valueOf(st.nextToken()).intValue();
  210.       g = Integer.valueOf(st.nextToken()).intValue();
  211.       b = Integer.valueOf(st.nextToken()).intValue();
  212.       return new Color(r,g,b);
  213.     }
  214.     catch (Exception e) { return aDefault; }
  215.   }
  216.  
  217.   /**
  218.    * Create the image Parameters.
  219.    * Called, if just created or size has changed
  220.    */
  221.   public void createParams() {
  222.     // Init some constants
  223.     int w = size().width;
  224.     int h = size().height;
  225.     lastS.width = w;
  226.     lastS.height = h;
  227.  
  228.     // Calc the font and positions. Message must fit applets area.
  229.     int refH = 14;
  230.     Font tf = new Font("TimesRoman", txtStyle, refH);
  231.     setFont(tf);
  232.     FontMetrics tfm = getFontMetrics(tf);
  233.     int fh = tfm.getHeight();
  234.     fh = refH*(h-10)/fh;
  235.     messageF = new Font("TimesRoman", txtStyle, fh);
  236.     FontMetrics fm = getFontMetrics(messageF);
  237.     fh = fm.getHeight();
  238.     messageX = w;
  239.     messageY = ((h-fh) >> 1)+fm.getAscent();
  240.     messageW = fm.stringWidth(message);
  241.  
  242.     // Use double buffering to avoid flicker.
  243.     if (gr != null)
  244.       gr.dispose();
  245.     im = createImage(lastS.width, lastS.height);
  246.     gr = im.getGraphics();
  247.   }
  248.  
  249.   /** Show the stuff, call update */
  250.   public void paint(Graphics g) { update(g); }
  251.  
  252.   /** Show the stuff */
  253.   public synchronized void update(Graphics g) {
  254.     int w = size().width;
  255.     int h = size().height;
  256.     // Exit, if expired
  257.     if (expired) {
  258.       if (ExFill) {
  259.         g.setColor(bgCo);
  260.         g.fillRect(0,0,w,h);
  261.       }
  262.       return;
  263.     }
  264.  
  265.     // Recalc params, if something has changed
  266.     if ((h != lastS.height) || (w != lastS.width))
  267.       createParams();
  268.  
  269.     // fill area with bgcolor
  270.     gr.setColor(bgCo);
  271.     gr.fillRect(0,0,w,h);
  272.  
  273.     // if url is given, let it look like a link
  274.     if (url_ != null && useFrame) {
  275.       gr.setColor(hrefCo);
  276.       gr.clipRect(0,0,w,h);
  277.       gr.drawRect(0,0,w,h);
  278.       gr.drawRect(1,1,w-2,h-2);
  279.       gr.setColor(bgCo);
  280.       gr.draw3DRect(2,2,w-4, h-4, true);
  281.       gr.draw3DRect(3,3,w-6, h-6, true);
  282.       gr.clipRect(4,4,w-8, h-8);
  283.     }
  284.  
  285.     // draw the text
  286.     gr.setFont(messageF);
  287.     if (useShadow) {
  288.       gr.setColor(shCo2);
  289.       gr.drawString(message, messageX+2, messageY+1);
  290.       gr.setColor(shCo1);
  291.       gr.drawString(message, messageX+1, messageY);
  292.     }
  293.     gr.setColor(txtCo);
  294.     gr.drawString(message, messageX, messageY);
  295.  
  296.     // finally show all together on the screen
  297.     g.drawImage(im,0,0,this);
  298.   }
  299.  
  300.   public void calcPos() {  
  301.     // decrement position
  302.     messageX -= speed;
  303.     // and stay in the bounds
  304.     if ((messageX + messageW) < 0)
  305.       messageX = size().width;
  306.   }
  307.  
  308.   /** Run the loop. This method is called by class Thread. */
  309.   public void run() {
  310.     // do nothing, if expired
  311.     if (expired)
  312.       return;
  313.  
  314.     // others might be more important
  315.     Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  316.     while (t != null) {
  317.       // indirectly call update
  318.       repaint();
  319.       calcPos();
  320.       // pause
  321.       try {Thread.sleep(100);}
  322.       catch (InterruptedException e){}
  323.     }
  324.   }
  325.  
  326.   /** Start the applet by forking an animation thread. */
  327.   public void start() {
  328.     if (t == null) {
  329.       t = new Thread(this);
  330.       t.start();
  331.     }
  332.   }
  333.  
  334.   /** Stop the applet. The thread will exit because run() exits. */
  335.   public void stop() {
  336.     if (t != null) {
  337.       t.stop();
  338.       t = null;
  339.     }
  340.     im = null;
  341.     if (gr != null) {
  342.       gr.dispose();
  343.       gr = null;
  344.     }
  345.     lastS = new Dimension(1,1);
  346.   }
  347.  
  348.   /** Switch to url, if url is given. */
  349.   public boolean mouseUp(Event evt, int x, int Y) {
  350.     if (url_  != null)
  351.       getAppletContext().showDocument(url_); // might not work with some early browsers
  352.     return true;
  353.   }
  354.  
  355.   /** Status: show URL */
  356.   public boolean mouseEnter(Event evt, int x, int y) {
  357.     if (url_ != null) { showStatus(url_.toExternalForm()); }
  358.     return true;
  359.   }
  360.  
  361.   /** clear status */
  362.   public boolean mouseExit(Event evt, int x, int y) {
  363.     if (url_ != null) { showStatus("  "); }
  364.     return true;
  365.   }
  366.  
  367. }
  368.     
  369.