home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jsamples.z / Blink.java < prev    next >
Encoding:
Java Source  |  1997-07-30  |  3.4 KB  |  97 lines

  1. // $Header: z:/admin/metro_examples/java/demo/Blink/rcs/Blink.java 1.1 1997/02/06 00:29:48 IPGIntel-2 Exp $ 
  2. /*
  3.  * @(#)Blink.java    1.3 96/12/06
  4.  *
  5.  * Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
  6.  *
  7.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  8.  * modify and redistribute this software in source and binary code form,
  9.  * provided that i) this copyright notice and license appear on all copies of
  10.  * the software; and ii) Licensee does not utilize the software in a manner
  11.  * which is disparaging to Sun.
  12.  *
  13.  * This software is provided "AS IS," without a warranty of any kind. ALL
  14.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  15.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  16.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  17.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  18.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  19.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  20.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  21.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  22.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  23.  * POSSIBILITY OF SUCH DAMAGES.
  24.  *
  25.  * This software is not designed or intended for use in on-line control of
  26.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  27.  * the design, construction, operation or maintenance of any nuclear
  28.  * facility. Licensee represents and warrants that it will not use or
  29.  * redistribute the Software for such purposes.
  30.  */
  31.  
  32. import java.awt.*;
  33. import java.util.StringTokenizer;
  34.  
  35. /**
  36.  * I love blinking things.
  37.  *
  38.  * @author Arthur van Hoff
  39.  * @modified 96/04/24 Jim Hagen : use getBackground
  40.  */
  41. public class Blink extends java.applet.Applet implements Runnable {
  42.     Thread blinker;
  43.     String lbl;
  44.     Font font;
  45.     int speed;
  46.  
  47.     public void init() {
  48.     font = new java.awt.Font("TimesRoman", Font.PLAIN, 24);
  49.     String att = getParameter("speed");
  50.     speed = (att == null) ? 400 : (1000 / Integer.valueOf(att).intValue());
  51.     att = getParameter("lbl");
  52.     lbl = (att == null) ? "Blink" : att;
  53.     }
  54.     
  55.     public void paint(Graphics g) {
  56.     int x = 0, y = font.getSize(), space;
  57.     int red = (int)(Math.random() * 50);
  58.     int green = (int)(Math.random() * 50);
  59.     int blue = (int)(Math.random() * 256);
  60.     Dimension d = size();
  61.  
  62.     g.setColor(Color.black);
  63.     g.setFont(font);
  64.     FontMetrics fm = g.getFontMetrics();
  65.     space = fm.stringWidth(" ");
  66.     for (StringTokenizer t = new StringTokenizer(lbl) ; t.hasMoreTokens() ; ) {
  67.         String word = t.nextToken();
  68.         int w = fm.stringWidth(word) + space;
  69.         if (x + w > d.width) {
  70.         x = 0;
  71.         y += font.getSize();
  72.         }
  73.         if (Math.random() < 0.5) {
  74.         g.setColor(new java.awt.Color((red + y * 30) % 256, (green + x / 3) % 256, blue));
  75.         } else {
  76.                 g.setColor(getBackground());
  77.         }
  78.         g.drawString(word, x, y);
  79.         x += w;
  80.     }
  81.     }
  82.  
  83.     public void start() {
  84.     blinker = new Thread(this);
  85.     blinker.start();
  86.     }
  87.     public void stop() {
  88.     blinker.stop();
  89.     }
  90.     public void run() {
  91.     while (true) {
  92.     try {Thread.currentThread().sleep(speed);} catch (InterruptedException e){}
  93.         repaint();
  94.     }
  95.     }
  96. }
  97.