J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * - Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * - Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * - Neither the name of Sun Microsystems nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived |
| 17 | * from this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | /* |
| 33 | */ |
| 34 | |
| 35 | import java.awt.event.*; |
| 36 | import java.awt.Graphics; |
| 37 | import java.awt.Font; |
| 38 | import java.applet.Applet; |
| 39 | |
| 40 | /** |
| 41 | * An applet that displays jittering text on the screen. |
| 42 | * |
| 43 | * @author Daniel Wyszynski 04/12/95 |
| 44 | * @modified 05/09/95 kwalrath Changed string; added thread suspension |
| 45 | * @modified 02/06/98 madbot removed use of suspend and resume and cleaned up |
| 46 | */ |
| 47 | |
| 48 | public class NervousText extends Applet implements Runnable, MouseListener { |
| 49 | String banner; // The text to be displayed |
| 50 | char bannerChars[]; // The same text as an array of characters |
| 51 | char attributes[]; // Character attributes ('^' for superscript) |
| 52 | Thread runner = null; // The thread that is displaying the text |
| 53 | boolean threadSuspended; // True when thread suspended (via mouse click) |
| 54 | |
| 55 | static final int REGULAR_WD = 15; |
| 56 | static final int REGULAR_HT = 36; |
| 57 | static final int SMALL_WD = 12; |
| 58 | static final int SMALL_HT = 24; |
| 59 | |
| 60 | Font regularFont = new Font("Serif", Font.BOLD, REGULAR_HT); |
| 61 | Font smallFont = new Font("Serif", Font.BOLD, SMALL_HT); |
| 62 | |
| 63 | public void init() { |
| 64 | banner = getParameter("text"); |
| 65 | if (banner == null) { |
| 66 | banner = "HotJava"; |
| 67 | } |
| 68 | |
| 69 | int bannerLength = banner.length(); |
| 70 | StringBuffer bc = new StringBuffer(bannerLength); |
| 71 | StringBuffer attrs = new StringBuffer(bannerLength); |
| 72 | int wd = 0; |
| 73 | for (int i = 0; i < bannerLength; i++) { |
| 74 | char c = banner.charAt(i); |
| 75 | char a = 0; |
| 76 | if (c == '^') { |
| 77 | i++; |
| 78 | if (i < bannerLength) { |
| 79 | c = banner.charAt(i); |
| 80 | a = '^'; |
| 81 | wd += SMALL_WD - REGULAR_WD; |
| 82 | } else { |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | bc.append(c); |
| 87 | attrs.append(a); |
| 88 | wd += REGULAR_WD; |
| 89 | } |
| 90 | |
| 91 | bannerLength = bc.length(); |
| 92 | bannerChars = new char[bannerLength]; |
| 93 | attributes = new char[bannerLength]; |
| 94 | bc.getChars(0, bannerLength, bannerChars, 0); |
| 95 | attrs.getChars(0, bannerLength, attributes, 0); |
| 96 | |
| 97 | threadSuspended = false; |
| 98 | resize(wd + 10, 50); |
| 99 | addMouseListener(this); |
| 100 | } |
| 101 | |
| 102 | public void destroy() { |
| 103 | removeMouseListener(this); |
| 104 | } |
| 105 | |
| 106 | public void start() { |
| 107 | runner = new Thread(this); |
| 108 | runner.start(); |
| 109 | } |
| 110 | |
| 111 | public synchronized void stop() { |
| 112 | runner = null; |
| 113 | if (threadSuspended) { |
| 114 | threadSuspended = false; |
| 115 | notify(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | public void run() { |
| 120 | Thread me = Thread.currentThread(); |
| 121 | while (runner == me) { |
| 122 | try { |
| 123 | Thread.sleep(100); |
| 124 | synchronized(this) { |
| 125 | while (threadSuspended) { |
| 126 | wait(); |
| 127 | } |
| 128 | } |
| 129 | } catch (InterruptedException e){ |
| 130 | } |
| 131 | repaint(); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | public void paint(Graphics g) { |
| 136 | int length = bannerChars.length; |
| 137 | for (int i = 0, x = 0; i < length; i++) { |
| 138 | int wd, ht; |
| 139 | if (attributes[i] == '^') { |
| 140 | wd = SMALL_WD; |
| 141 | ht = SMALL_HT; |
| 142 | g.setFont(smallFont); |
| 143 | } else { |
| 144 | wd = REGULAR_WD; |
| 145 | ht = REGULAR_HT; |
| 146 | g.setFont(regularFont); |
| 147 | } |
| 148 | int px = (int) (10 * Math.random() + x); |
| 149 | int py = (int) (10 * Math.random() + ht); |
| 150 | g.drawChars(bannerChars, i, 1, px, py); |
| 151 | x += wd; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | public synchronized void mousePressed(MouseEvent e) { |
| 156 | e.consume(); |
| 157 | threadSuspended = !threadSuspended; |
| 158 | if (!threadSuspended) |
| 159 | notify(); |
| 160 | } |
| 161 | |
| 162 | public void mouseReleased(MouseEvent e) { |
| 163 | } |
| 164 | |
| 165 | public void mouseEntered(MouseEvent e) { |
| 166 | } |
| 167 | |
| 168 | public void mouseExited(MouseEvent e) { |
| 169 | } |
| 170 | |
| 171 | public void mouseClicked(MouseEvent e) { |
| 172 | } |
| 173 | |
| 174 | public String getAppletInfo() { |
| 175 | return "Title: NervousText\nAuthor: Daniel Wyszynski\nDisplays a text banner that jitters."; |
| 176 | } |
| 177 | |
| 178 | public String[][] getParameterInfo() { |
| 179 | String pinfo[][] = { |
| 180 | {"text", "string", "Text to display"}, |
| 181 | }; |
| 182 | return pinfo; |
| 183 | } |
| 184 | } |