| 1 36 37 40 41 import java.awt.event.*; 42 import java.awt.Graphics ; 43 import java.awt.Font ; 44 import java.applet.Applet ; 45 46 54 55 public class NervousText extends Applet implements Runnable , MouseListener { 56 String banner; char bannerChars[]; char attributes[]; Thread runner = null; boolean threadSuspended; 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 regularFont = new Font ("Serif", Font.BOLD, REGULAR_HT); 68 Font smallFont = new Font ("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 bc = new StringBuffer (bannerLength); 78 StringBuffer attrs = new StringBuffer (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 (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 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 e){ 137 } 138 repaint(); 139 } 140 } 141 142 public void paint(Graphics 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 getAppletInfo() { 182 return "Title: NervousText\nAuthor: Daniel Wyszynski\nDisplays a text banner that jitters."; 183 } 184 185 public String [][] getParameterInfo() { 186 String pinfo[][] = { 187 {"text", "string", "Text to display"}, 188 }; 189 return pinfo; 190 } 191 } 192 | Popular Tags |