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 = "Java Development Kit ";
  76         }
  77         banner += System.getProperty("java.version", "");
  78 
  79         int bannerLength = banner.length();
  80         StringBuilder bc = new StringBuilder(bannerLength);
  81         StringBuilder attrs = new StringBuilder(bannerLength);
  82         int wd = 0;
  83         for (int i = 0; i < bannerLength; i++) {
  84             char c = banner.charAt(i);
  85             char a = 0;
  86             if (c == '^') {
  87                 i++;
  88                 if (i < bannerLength) {
  89                     c = banner.charAt(i);
  90                     a = '^';
  91                     wd += SMALL_WD - REGULAR_WD;
  92                 } else {
  93                     break;
  94                 }
  95             }
  96             bc.append(c);
  97             attrs.append(a);
  98             wd += REGULAR_WD;
  99         }
 100 
 101         bannerLength = bc.length();
 102         bannerChars = new char[bannerLength];
 103         attributes = new char[bannerLength];
 104         bc.getChars(0, bannerLength, bannerChars, 0);
 105         attrs.getChars(0, bannerLength, attributes, 0);
 106 
 107         threadSuspended = false;
 108         resize(wd + 10, 50);
 109         addMouseListener(this);
 110     }
 111 
 112     @Override
 113     public void destroy() {
 114         removeMouseListener(this);
 115     }
 116 
 117     @Override
 118     public void start() {
 119         runner = new Thread(this);
 120         runner.start();
 121     }
 122 
 123     @Override
 124     public synchronized void stop() {
 125         runner = null;
 126         if (threadSuspended) {
 127             threadSuspended = false;
 128             notify();
 129         }
 130     }
 131 
 132     @Override
 133     public void run() {
 134         Thread me = Thread.currentThread();
 135         while (runner == me) {
 136             try {
 137                 Thread.sleep(100);
 138                 synchronized (this) {
 139                     while (threadSuspended) {
 140                         wait();
 141                     }
 142                 }
 143             } catch (InterruptedException e) {
 144             }
 145             repaint();
 146         }
 147     }
 148 
 149     @Override
 150     public void paint(Graphics g) {
 151         int length = bannerChars.length;
 152         for (int i = 0, x = 0; i < length; i++) {
 153             int wd, ht;
 154             if (attributes[i] == '^') {
 155                 wd = SMALL_WD;
 156                 ht = SMALL_HT;
 157                 g.setFont(smallFont);
 158             } else {
 159                 wd = REGULAR_WD;
 160                 ht = REGULAR_HT;
 161                 g.setFont(regularFont);
 162             }
 163             int px = (int) (10 * Math.random() + x);
 164             int py = (int) (10 * Math.random() + ht);
 165             g.drawChars(bannerChars, i, 1, px, py);
 166             x += wd;
 167         }
 168     }
 169 
 170     @Override
 171     public synchronized void mousePressed(MouseEvent e) {
 172         e.consume();
 173         threadSuspended = !threadSuspended;
 174         if (!threadSuspended) {
 175             notify();
 176         }
 177     }
 178 
 179     @Override
 180     public void mouseReleased(MouseEvent e) {
 181     }
 182 
 183     @Override
 184     public void mouseEntered(MouseEvent e) {
 185     }
 186 
 187     @Override
 188     public void mouseExited(MouseEvent e) {
 189     }
 190 
 191     @Override
 192     public void mouseClicked(MouseEvent e) {
 193     }
 194 
 195     @Override
 196     public String getAppletInfo() {
 197         return "Title: NervousText\nAuthor: Daniel Wyszynski\n"
 198                 + "Displays a text banner that jitters.";
 199     }
 200 
 201     @Override
 202     public String[][] getParameterInfo() {
 203         String pinfo[][] = {
 204             { "text", "string", "Text to display" }, };
 205         return pinfo;
 206     }
 207 }