home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / plugin / applets / NervousText / NervousText.java < prev   
Encoding:
Java Source  |  2002-09-06  |  5.6 KB  |  191 lines

  1. /*
  2.  * Copyright (c) 2002 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.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)NervousText.java    1.9 02/06/13
  38.  */
  39.  
  40. import java.awt.event.*;
  41. import java.awt.Graphics;
  42. import java.awt.Font;
  43. import java.applet.Applet;
  44.  
  45. /**
  46.  * An applet that displays jittering text on the screen.
  47.  *
  48.  * @author Daniel Wyszynski 04/12/95
  49.  * @version 1.10, 02/05/97
  50.  * @modified 05/09/95 kwalrath Changed string; added thread suspension
  51.  * @modified 02/06/98 madbot removed use of suspend and resume and cleaned up
  52.  */
  53.  
  54. public class NervousText extends Applet implements Runnable, MouseListener {
  55.     String banner;        // The text to be displayed
  56.     char bannerChars[];        // The same text as an array of characters
  57.     char attributes[];        // Character attributes ('^' for superscript)
  58.     Thread runner = null;    // The thread that is displaying the text
  59.     boolean threadSuspended;    // True when thread suspended (via mouse click)
  60.  
  61.     static final int REGULAR_WD = 15;
  62.     static final int REGULAR_HT = 36;
  63.     static final int SMALL_WD = 12;
  64.     static final int SMALL_HT = 24;
  65.  
  66.     Font regularFont = new Font("Serif", Font.BOLD, REGULAR_HT);
  67.     Font smallFont = new Font("Serif", Font.BOLD, SMALL_HT);
  68.  
  69.     public void init() {
  70.     banner = getParameter("text");
  71.     if (banner == null) {
  72.         banner = "HotJava";
  73.     }
  74.  
  75.         int bannerLength = banner.length();
  76.     StringBuffer bc = new StringBuffer(bannerLength);
  77.     StringBuffer attrs = new StringBuffer(bannerLength);
  78.     int wd = 0;
  79.     for (int i = 0; i < bannerLength; i++) {
  80.         char c = banner.charAt(i);
  81.         char a = 0;
  82.         if (c == '^') {
  83.         i++;
  84.         if (i < bannerLength) {
  85.             c = banner.charAt(i);
  86.             a = '^';
  87.             wd += SMALL_WD - REGULAR_WD;
  88.         } else {
  89.             break;
  90.         }
  91.         }
  92.         bc.append(c);
  93.         attrs.append(a);
  94.         wd += REGULAR_WD;
  95.     }
  96.  
  97.     bannerLength = bc.length();
  98.     bannerChars =  new char[bannerLength];
  99.     attributes = new char[bannerLength];
  100.     bc.getChars(0, bannerLength, bannerChars, 0);
  101.     attrs.getChars(0, bannerLength, attributes, 0);
  102.  
  103.         threadSuspended = false;
  104.     resize(wd + 10, 50);
  105.     addMouseListener(this);
  106.     }
  107.  
  108.     public void destroy() {
  109.         removeMouseListener(this);
  110.     }
  111.  
  112.     public void start() {
  113.         runner = new Thread(this);
  114.         runner.start();
  115.     }
  116.  
  117.     public synchronized void stop() {
  118.     runner = null;
  119.         if (threadSuspended) {
  120.             threadSuspended = false;
  121.             notify();
  122.         }
  123.     }
  124.  
  125.     public void run() {
  126.         Thread me = Thread.currentThread();
  127.         while (runner == me) {
  128.             try {
  129.                 Thread.sleep(100);
  130.                 synchronized(this) {
  131.                     while (threadSuspended) {
  132.                         wait();
  133.                     }
  134.                 }
  135.             } catch (InterruptedException e){
  136.             }
  137.             repaint();
  138.         }
  139.     }
  140.  
  141.     public void paint(Graphics g) {
  142.     int length = bannerChars.length;
  143.         for (int i = 0, x = 0; i < length; i++) {
  144.         int wd, ht;
  145.         if (attributes[i] == '^') {
  146.         wd = SMALL_WD;
  147.         ht = SMALL_HT;
  148.         g.setFont(smallFont);
  149.         } else {
  150.         wd = REGULAR_WD;
  151.         ht = REGULAR_HT;
  152.         g.setFont(regularFont);
  153.         }
  154.             int px = (int) (10 * Math.random() + x);
  155.             int py = (int) (10 * Math.random() + ht);
  156.             g.drawChars(bannerChars, i, 1, px, py);
  157.         x += wd;
  158.     }
  159.     }
  160.  
  161.     public synchronized void mousePressed(MouseEvent e) {
  162.         e.consume();
  163.         threadSuspended = !threadSuspended;
  164.         if (!threadSuspended)
  165.             notify();
  166.     }
  167.  
  168.     public void mouseReleased(MouseEvent e) {
  169.     }
  170.  
  171.     public void mouseEntered(MouseEvent e) {
  172.     }
  173.  
  174.     public void mouseExited(MouseEvent e) {
  175.     }
  176.  
  177.     public void mouseClicked(MouseEvent e) {
  178.     }
  179.  
  180.     public String getAppletInfo() {
  181.         return "Title: NervousText\nAuthor: Daniel Wyszynski\nDisplays a text banner that jitters.";
  182.     }
  183.  
  184.     public String[][] getParameterInfo() {
  185.         String pinfo[][] = {
  186.             {"text", "string", "Text to display"},
  187.         };
  188.         return pinfo;
  189.     }
  190. }
  191.