home *** CD-ROM | disk | FTP | other *** search
/ Chip 1999 September / Chip_1999-09_cd.bin / internet / JSLib / applets / 0133 / JTTicker.java < prev   
Text File  |  1999-06-05  |  10KB  |  339 lines

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