daily pastebin goal
32%
SHARE
TWEET

Blink.java

a guest Jun 19th, 2017 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     Thread blinker;
  15.     String lbl;
  16.     Font font;
  17.     int speed;
  18.  
  19.     public void init() {
  20.     font = new java.awt.Font("TimesRoman", Font.PLAIN, 24);
  21.     String att = getParameter("speed");
  22.     speed = (att == null) ? 400 : (1000 / Integer.valueOf(att).intValue());
  23.     att = getParameter("lbl");
  24.     lbl = (att == null) ? "Blink" : att;
  25.     }
  26.    
  27.     public void paint(Graphics g) {
  28.     int x = 0, y = font.getSize(), space;
  29.     int red = (int)(Math.random() * 50);
  30.     int green = (int)(Math.random() * 50);
  31.     int blue = (int)(Math.random() * 256);
  32.     Dimension d = size();
  33.  
  34.     g.setColor(Color.black);
  35.     g.setFont(font);
  36.     FontMetrics fm = g.getFontMetrics();
  37.     space = fm.stringWidth(" ");
  38.     for (StringTokenizer t = new StringTokenizer(lbl) ;
  39.             t.hasMoreTokens() ; ) {
  40.  
  41.         String word = t.nextToken();
  42.         int w = fm.stringWidth(word) + space;
  43.         if (x + w > d.width) {
  44.         x = 0;
  45.         y += font.getSize();
  46.         }
  47.         if (Math.random() < 0.5) {
  48.         g.setColor(new java.awt.Color(
  49.                     (red + y * 30) % 256, (green + x / 3) % 256, blue));
  50.         } else {
  51.         g.setColor(Color.lightGray);
  52.         }
  53.         g.drawString(word, x, y);
  54.         x += w;
  55.     }
  56.     }
  57.  
  58.     public void start() {
  59.     blinker = new Thread(this);
  60.     blinker.start();
  61.     }
  62.     public void stop() {
  63.     blinker.stop();
  64.     }
  65.     public void run() {
  66.     while (true) {
  67.         try {
  68.         Thread.currentThread().sleep(speed);
  69.         } catch (InterruptedException e) {
  70.         }
  71.     repaint();
  72.     }
  73.     }
  74. }
RAW Paste Data
Top