home *** CD-ROM | disk | FTP | other *** search
/ The Best of Windows 95.com 1996 December / WIN95_DEC_1996_2.ISO / java / JDRAW4.EXE / data.z / Blink.Bsp < prev    next >
Text File  |  1996-08-09  |  2KB  |  87 lines

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