home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1998 February / VPR9802A.ISO / APP_DEMO / VC / SAMPLES.BIN / Blink.java < prev    next >
Text File  |  1997-10-27  |  3KB  |  96 lines

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