home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / WebObjects / WebObjectsDoc_HTML / DynamicElements / AppletEx1.wo / Blink.java < prev   
Encoding:
Java Source  |  1995-12-12  |  1.6 KB  |  71 lines

  1.  
  2. /*
  3.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  4.  */
  5.  
  6. import java.awt.*;
  7. import java.util.StringTokenizer;
  8.  
  9. /**
  10.  * I love blinking things.
  11.  *
  12.  * @author Arthur van Hoff
  13.  */
  14. public class Blink extends java.applet.Applet implements Runnable {
  15.     Thread blinker;
  16.     String lbl;
  17.     Font font;
  18.     int speed;
  19.  
  20.     public void init() {
  21.     font = new java.awt.Font("TimesRoman", Font.PLAIN, 24);
  22.     String att = getParameter("speed");
  23.     speed = (att == null) ? 400 : (1000 / Integer.valueOf(att).intValue());
  24.     att = getParameter("lbl");
  25.     lbl = (att == null) ? "Blink" : att;
  26.     }
  27.     
  28.     public void paint(Graphics g) {
  29.     int x = 0, y = font.getSize(), space;
  30.     int red = (int)(Math.random() * 50);
  31.     int green = (int)(Math.random() * 50);
  32.     int blue = (int)(Math.random() * 256);
  33.     Dimension d = size();
  34.  
  35.     g.setColor(Color.black);
  36.     g.setFont(font);
  37.     FontMetrics fm = g.getFontMetrics();
  38.     space = fm.stringWidth(" ");
  39.     for (StringTokenizer t = new StringTokenizer(lbl) ; t.hasMoreTokens() ; ) {
  40.         String word = t.nextToken();
  41.         int w = fm.stringWidth(word) + space;
  42.         if (x + w > d.width) {
  43.         x = 0;
  44.         y += font.getSize();
  45.         }
  46.         if (Math.random() < 0.5) {
  47.         g.setColor(new java.awt.Color((red + y * 30) % 256, (green + x / 3) % 256, blue));
  48.         } else {
  49.         g.setColor(Color.lightGray);
  50.         }
  51.         g.drawString(word, x, y);
  52.         x += w;
  53.     }
  54.     }
  55.  
  56.     public void start() {
  57.     blinker = new Thread(this);
  58.     blinker.start();
  59.     }
  60.     public void stop() {
  61.     blinker.stop();
  62.     }
  63.     public void run() {
  64.     while (true) {
  65.     try {Thread.currentThread().sleep(speed);} catch (InterruptedException e){}
  66.         repaint();
  67.     }
  68.     }
  69. }
  70.  
  71.