home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / applets / ImageMap / TickerArea.java < prev   
Encoding:
Java Source  |  2002-09-06  |  3.9 KB  |  136 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)TickerArea.java    1.9 02/06/13
  38.  */
  39.  
  40. import java.awt.Graphics;
  41. import java.awt.Color;
  42. import java.awt.Font;
  43. import java.awt.FontMetrics;
  44. import java.util.StringTokenizer;
  45.  
  46. /**
  47.  * This ImageArea renders a string of text that constantly scrolls across
  48.  * the indicated area of the ImageMap in the specified color.
  49.  *
  50.  * @author    Jim Graham
  51.  * @version    1.9, 06/13/02
  52.  */
  53. class TickerArea extends ImageMapArea {
  54.  
  55.     String tickertext;
  56.     Color  tickercolor;
  57.     Font   tickerfont;
  58.     int    speed;        // In pixels per second for scrolling
  59.  
  60.     int tickerx;
  61.     int tickery;
  62.     int tickerlen;
  63.     long lasttick;
  64.  
  65.     public void handleArg(String s) {
  66.     StringTokenizer st = new StringTokenizer(s, ",");
  67.  
  68.     tickertext = st.nextToken();
  69.     tickercolor = Color.black;
  70.     speed = 100;
  71.     String fontname = "Serif";
  72.  
  73.     if (st.hasMoreTokens()) {
  74.         fontname = st.nextToken();
  75.         if (st.hasMoreTokens()) {
  76.         String str = st.nextToken();
  77.         if (str.startsWith("#")) {
  78.             str = str.substring(1);
  79.         }
  80.         try {
  81.             int colorval = Integer.parseInt(str, 16);
  82.             tickercolor = new Color((colorval >> 16) & 0xff,
  83.                         (colorval >> 8) & 0xff,
  84.                         (colorval >> 0) & 0xff);
  85.         } catch (Exception e) {
  86.             tickercolor = Color.black;
  87.         }
  88.         if (st.hasMoreTokens()) {
  89.             str = st.nextToken();
  90.             try {
  91.             speed = Integer.parseInt(str);
  92.             } catch (Exception e) {
  93.             speed = 100;
  94.             }
  95.         }
  96.         }
  97.     }
  98.  
  99.     FontMetrics fm;
  100.     int size;
  101.     int nextsize = H;
  102.     do {
  103.         size = nextsize;
  104.         tickerfont = new Font(fontname, Font.PLAIN, size);
  105.         fm = parent.getFontMetrics(tickerfont);
  106.         nextsize = (size * 9) / 10;
  107.     } while (fm.getHeight() > H && size > 0);
  108.     tickerlen = fm.stringWidth(tickertext);
  109.     tickery = fm.getAscent();
  110.     }
  111.  
  112.     public void getMedia() {
  113.     tickerx = 0;
  114.     repaint();
  115.     lasttick = System.currentTimeMillis();
  116.     }
  117.  
  118.     public boolean animate() {
  119.     long curtick = System.currentTimeMillis();
  120.     tickerx -= ((speed * (curtick - lasttick)) / 1000);
  121.     if (tickerx > W || tickerx + tickerlen < 0) {
  122.         tickerx = W;
  123.     }
  124.     repaint();
  125.     lasttick = curtick;
  126.     return true;
  127.     }
  128.  
  129.     public void highlight(Graphics g) {
  130.     g.setColor(tickercolor);
  131.     g.setFont(tickerfont);
  132.     g.drawString(tickertext, X+tickerx, Y+tickery);
  133.     }
  134. }
  135.  
  136.