KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Blink


1 /*
2  * @(#)Blink.java 1.16 06/02/22
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)Blink.java 1.16 06/02/22
39  */

40
41 /**
42  * I love blinking things.
43  *
44  * @author Arthur van Hoff
45  * @modified 04/24/96 Jim Hagen use getBackground
46  * @modified 02/05/98 Mike McCloskey removed use of deprecated methods
47  * @modified 04/23/99 Josh Bloch, use timer instead of explicit multithreading.
48  * @modified 07/10/00 Daniel Peek brought to code conventions, minor changes
49  */

50
51 import java.awt.*;
52 import java.util.*;
53
54 public class Blink extends java.applet.Applet JavaDoc {
55     private Timer timer; // Schedules the blinking
56
private String JavaDoc labelString; // The label for the window
57
private int delay; // the delay time between blinks
58

59     public void init() {
60         String JavaDoc blinkFrequency = getParameter("speed");
61         delay = (blinkFrequency == null) ? 400 :
62             (1000 / Integer.parseInt(blinkFrequency));
63         labelString = getParameter("lbl");
64         if (labelString == null)
65             labelString = "Blink";
66         Font font = new java.awt.Font JavaDoc("Serif", Font.PLAIN, 24);
67         setFont(font);
68     }
69
70     public void start() {
71         timer = new Timer(); //creates a new timer to schedule the blinking
72
timer.schedule(new TimerTask() { //creates a timertask to schedule
73
// overrides the run method to provide functionality
74
public void run() {
75                 repaint();
76             }
77         }
78             , delay, delay);
79     }
80
81     public void paint(Graphics g) {
82         int fontSize = g.getFont().getSize();
83         int x = 0, y = fontSize, space;
84         int red = (int) ( 50 * Math.random());
85         int green = (int) ( 50 * Math.random());
86         int blue = (int) (256 * Math.random());
87         Dimension d = getSize();
88         g.setColor(Color.black);
89         FontMetrics fm = g.getFontMetrics();
90         space = fm.stringWidth(" ");
91         for (StringTokenizer t = new StringTokenizer(labelString);
92              t.hasMoreTokens();) {
93             String JavaDoc word = t.nextToken();
94             int w = fm.stringWidth(word) + space;
95             if (x + w > d.width) {
96                 x = 0;
97                 y += fontSize; //move word to next line if it doesn't fit
98
}
99             if (Math.random() < 0.5)
100                 g.setColor(new java.awt.Color JavaDoc((red + y*30) % 256,
101                                               (green + x/3) % 256, blue));
102             else
103                 g.setColor(getBackground());
104             g.drawString(word, x, y);
105             x += w; //shift to the right to draw the next word
106
}
107     }
108     
109     public void stop() {
110         timer.cancel(); //stops the timer
111
}
112
113     public String JavaDoc getAppletInfo() {
114         return "Title: Blinker\n"
115             + "Author: Arthur van Hoff\n"
116             + "Displays multicolored blinking text.";
117     }
118     
119     public String JavaDoc[][] getParameterInfo() {
120         String JavaDoc pinfo[][] = {
121             {"speed", "string", "The blink frequency"},
122             {"lbl", "string", "The text to blink."},
123         };
124         return pinfo;
125     }
126 }
127
Popular Tags