KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > NervousText


1 /*
2  * @(#)NervousText.java 1.14 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  * @(#)NervousText.java 1.14 06/02/22
39  */

40
41 import java.awt.event.*;
42 import java.awt.Graphics JavaDoc;
43 import java.awt.Font JavaDoc;
44 import java.applet.Applet JavaDoc;
45
46 /**
47  * An applet that displays jittering text on the screen.
48  *
49  * @author Daniel Wyszynski 04/12/95
50  * @version 1.10, 02/05/97
51  * @modified 05/09/95 kwalrath Changed string; added thread suspension
52  * @modified 02/06/98 madbot removed use of suspend and resume and cleaned up
53  */

54
55 public class NervousText extends Applet JavaDoc implements Runnable JavaDoc, MouseListener {
56     String JavaDoc banner; // The text to be displayed
57
char bannerChars[]; // The same text as an array of characters
58
char attributes[]; // Character attributes ('^' for superscript)
59
Thread JavaDoc runner = null; // The thread that is displaying the text
60
boolean threadSuspended; // True when thread suspended (via mouse click)
61

62     static final int REGULAR_WD = 15;
63     static final int REGULAR_HT = 36;
64     static final int SMALL_WD = 12;
65     static final int SMALL_HT = 24;
66
67     Font JavaDoc regularFont = new Font JavaDoc("Serif", Font.BOLD, REGULAR_HT);
68     Font JavaDoc smallFont = new Font JavaDoc("Serif", Font.BOLD, SMALL_HT);
69
70     public void init() {
71     banner = getParameter("text");
72     if (banner == null) {
73         banner = "HotJava";
74     }
75
76         int bannerLength = banner.length();
77     StringBuffer JavaDoc bc = new StringBuffer JavaDoc(bannerLength);
78     StringBuffer JavaDoc attrs = new StringBuffer JavaDoc(bannerLength);
79     int wd = 0;
80     for (int i = 0; i < bannerLength; i++) {
81         char c = banner.charAt(i);
82         char a = 0;
83         if (c == '^') {
84         i++;
85         if (i < bannerLength) {
86             c = banner.charAt(i);
87             a = '^';
88             wd += SMALL_WD - REGULAR_WD;
89         } else {
90             break;
91         }
92         }
93         bc.append(c);
94         attrs.append(a);
95         wd += REGULAR_WD;
96     }
97
98     bannerLength = bc.length();
99     bannerChars = new char[bannerLength];
100     attributes = new char[bannerLength];
101     bc.getChars(0, bannerLength, bannerChars, 0);
102     attrs.getChars(0, bannerLength, attributes, 0);
103
104         threadSuspended = false;
105     resize(wd + 10, 50);
106     addMouseListener(this);
107     }
108
109     public void destroy() {
110         removeMouseListener(this);
111     }
112
113     public void start() {
114         runner = new Thread JavaDoc(this);
115         runner.start();
116     }
117
118     public synchronized void stop() {
119     runner = null;
120         if (threadSuspended) {
121             threadSuspended = false;
122             notify();
123         }
124     }
125
126     public void run() {
127         Thread JavaDoc me = Thread.currentThread();
128         while (runner == me) {
129             try {
130                 Thread.sleep(100);
131                 synchronized(this) {
132                     while (threadSuspended) {
133                         wait();
134                     }
135                 }
136             } catch (InterruptedException JavaDoc e){
137             }
138             repaint();
139         }
140     }
141
142     public void paint(Graphics JavaDoc g) {
143     int length = bannerChars.length;
144         for (int i = 0, x = 0; i < length; i++) {
145         int wd, ht;
146         if (attributes[i] == '^') {
147         wd = SMALL_WD;
148         ht = SMALL_HT;
149         g.setFont(smallFont);
150         } else {
151         wd = REGULAR_WD;
152         ht = REGULAR_HT;
153         g.setFont(regularFont);
154         }
155             int px = (int) (10 * Math.random() + x);
156             int py = (int) (10 * Math.random() + ht);
157             g.drawChars(bannerChars, i, 1, px, py);
158         x += wd;
159     }
160     }
161
162     public synchronized void mousePressed(MouseEvent e) {
163         e.consume();
164         threadSuspended = !threadSuspended;
165         if (!threadSuspended)
166             notify();
167     }
168
169     public void mouseReleased(MouseEvent e) {
170     }
171
172     public void mouseEntered(MouseEvent e) {
173     }
174
175     public void mouseExited(MouseEvent e) {
176     }
177
178     public void mouseClicked(MouseEvent e) {
179     }
180
181     public String JavaDoc getAppletInfo() {
182         return "Title: NervousText\nAuthor: Daniel Wyszynski\nDisplays a text banner that jitters.";
183     }
184
185     public String JavaDoc[][] getParameterInfo() {
186         String JavaDoc pinfo[][] = {
187             {"text", "string", "Text to display"},
188         };
189         return pinfo;
190     }
191 }
192
Popular Tags