1 /*
   2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. 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 Oracle 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  * This source code is provided to illustrate the usage of a given feature
  34  * or technique and has been deliberately simplified. Additional steps
  35  * required for a production-quality application, such as security checks,
  36  * input validation and proper error handling, might not be present in
  37  * this sample code.
  38  */
  39 
  40 
  41 
  42 import java.awt.Graphics;
  43 import java.awt.Font;
  44 import java.applet.Applet;
  45 import java.awt.event.MouseEvent;
  46 import java.awt.event.MouseListener;
  47 
  48 
  49 /**
  50  * An applet that displays jittering text on the screen.
  51  *
  52  * @author Daniel Wyszynski 04/12/95
  53  * @author 05/09/95 kwalrath Changed string; added thread suspension
  54  * @author 02/06/98 madbot removed use of suspend and resume and cleaned up
  55  */
  56 @SuppressWarnings("serial")
  57 public class NervousText extends Applet implements Runnable, MouseListener {
  58 
  59     String banner;              // The text to be displayed
  60     char bannerChars[];         // The same text as an array of characters
  61     char attributes[];          // Character attributes ('^' for superscript)
  62     Thread runner = null;       // The thread that is displaying the text
  63     boolean threadSuspended;    // True when thread suspended (via mouse click)
  64     static final int REGULAR_WD = 15;
  65     static final int REGULAR_HT = 36;
  66     static final int SMALL_WD = 12;
  67     static final int SMALL_HT = 24;
  68     Font regularFont = new Font("Serif", Font.BOLD, REGULAR_HT);
  69     Font smallFont = new Font("Serif", Font.BOLD, SMALL_HT);
  70 
  71     @Override
  72     public void init() {
  73         banner = getParameter("text");
  74         if (banner == null) {
  75             banner = "HotJava";
  76         }
  77 
  78         int bannerLength = banner.length();
  79         StringBuilder bc = new StringBuilder(bannerLength);
  80         StringBuilder attrs = new StringBuilder(bannerLength);
  81         int wd = 0;
  82         for (int i = 0; i < bannerLength; i++) {
  83             char c = banner.charAt(i);
  84             char a = 0;
  85             if (c == '^') {
  86                 i++;
  87                 if (i < bannerLength) {
  88                     c = banner.charAt(i);
  89                     a = '^';
  90                     wd += SMALL_WD - REGULAR_WD;
  91                 } else {
  92                     break;
  93                 }
  94             }
  95             bc.append(c);
  96             attrs.append(a);
  97             wd += REGULAR_WD;
  98         }
  99 
 100         bannerLength = bc.length();
 101         bannerChars = new char[bannerLength];
 102         attributes = new char[bannerLength];
 103         bc.getChars(0, bannerLength, bannerChars, 0);
 104         attrs.getChars(0, bannerLength, attributes, 0);
 105 
 106         threadSuspended = false;
 107         resize(wd + 10, 50);
 108         addMouseListener(this);
 109     }
 110 
 111     @Override
 112     public void destroy() {
 113         removeMouseListener(this);
 114     }
 115 
 116     @Override
 117     public void start() {
 118         runner = new Thread(this);
 119         runner.start();
 120     }
 121 
 122     @Override
 123     public synchronized void stop() {
 124         runner = null;
 125         if (threadSuspended) {
 126             threadSuspended = false;
 127             notify();
 128         }
 129     }
 130 
 131     @Override
 132     public void run() {
 133         Thread me = Thread.currentThread();
 134         while (runner == me) {
 135             try {
 136                 Thread.sleep(100);
 137                 synchronized (this) {
 138                     while (threadSuspended) {
 139                         wait();
 140                     }
 141                 }
 142             } catch (InterruptedException e) {
 143             }
 144             repaint();
 145         }
 146     }
 147 
 148     @Override
 149     public void paint(Graphics g) {
 150         int length = bannerChars.length;
 151         for (int i = 0, x = 0; i < length; i++) {
 152             int wd, ht;
 153             if (attributes[i] == '^') {
 154                 wd = SMALL_WD;
 155                 ht = SMALL_HT;
 156                 g.setFont(smallFont);
 157             } else {
 158                 wd = REGULAR_WD;
 159                 ht = REGULAR_HT;
 160                 g.setFont(regularFont);
 161             }
 162             int px = (int) (10 * Math.random() + x);
 163             int py = (int) (10 * Math.random() + ht);
 164             g.drawChars(bannerChars, i, 1, px, py);
 165             x += wd;
 166         }
 167     }
 168 
 169     @Override
 170     public synchronized void mousePressed(MouseEvent e) {
 171         e.consume();
 172         threadSuspended = !threadSuspended;
 173         if (!threadSuspended) {
 174             notify();
 175         }
 176     }
 177 
 178     @Override
 179     public void mouseReleased(MouseEvent e) {
 180     }
 181 
 182     @Override
 183     public void mouseEntered(MouseEvent e) {
 184     }
 185 
 186     @Override
 187     public void mouseExited(MouseEvent e) {
 188     }
 189 
 190     @Override
 191     public void mouseClicked(MouseEvent e) {
 192     }
 193 
 194     @Override
 195     public String getAppletInfo() {
 196         return "Title: NervousText\nAuthor: Daniel Wyszynski\n"
 197                 + "Displays a text banner that jitters.";
 198     }
 199 
 200     @Override
 201     public String[][] getParameterInfo() {
 202         String pinfo[][] = {
 203             { "text", "string", "Text to display" }, };
 204         return pinfo;
 205     }
 206 }