home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-05-21 | 1.1 KB | 52 lines |
- // DynaLabel.java - Label with dynamically changing color
- //
- // Copyright (C) 1996 by Dale Gass
- // Non-exclusive license granted to MKS, Inc.
- //
-
- import java.lang.*;
- import java.util.*;
- import java.awt.*;
- import java.net.*;
- import java.applet.*;
-
- // DynaLabel - a label with dynamically changing colour
-
- public class DynaLabel extends Canvas implements Runnable {
- Thread me;
- Font font;
- String s;
- Random r;
-
- // Constructor
-
- public DynaLabel(String text) {
- s = text;
- font = new Font("TimesRoman", Font.BOLD | Font.ITALIC, 20);
- r = new Random();
- resize(120, 30);
-
- (me = new Thread(this)).start();
- }
-
- // Draw the object with a random colour
-
- public void paint(Graphics g) {
- g.setFont(font);
- g.setColor(new Color(r.nextInt() % 256,
- r.nextInt() % 256,
- r.nextInt() % 256));
- g.drawString(s, 5, getFontMetrics(font).getHeight());
- }
-
- // Dynamically update the label
-
- public void run() {
- while (true) {
- try { me.sleep(300); } catch (Exception e) { }
- repaint();
- }
- }
- }
-
-