home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / NervousText.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  5.3 KB  |  210 lines

  1. package symantec.itools.multimedia;
  2.  
  3.  
  4. import java.awt.Canvas;
  5. import java.awt.Dimension;
  6.  
  7. //     01/29/97    TWB    Integrated changes from Macintosh
  8.  
  9. /**
  10.  * NervousText control.
  11.  * Creates animated text in which each letter moves independently of all other letters.
  12.  *
  13.  * @version 1.0, Nov 26, 1996
  14.  * @author Symantec
  15.  */
  16.  
  17. public class NervousText
  18.     extends Canvas
  19.     implements Runnable
  20. {
  21.     char separated[];
  22.     String text;
  23.     boolean paused;
  24.     Thread jitter = null;
  25.  
  26.  
  27.     /**
  28.      * Create defalut NervousText object. Default object contains the word "text".
  29.      */
  30.  
  31.     public NervousText()
  32.     {
  33.         paused = false;
  34.         setText("text");
  35.     }
  36.  
  37.     /**
  38.      * Tells this component that it has been added to a container.
  39.      * This is a standard Java AWT method which gets called by the AWT when
  40.      * this component is added to a container. Typically, it is used to
  41.      * create this component's peer.
  42.      *
  43.      * It has been overridden here to start the nervous text thread.
  44.      *
  45.      * @see #removeNotify
  46.      */
  47.  
  48.     public void addNotify()
  49.     {
  50.         super.addNotify();
  51.         jitter = new Thread(this);
  52.         jitter.start();
  53.     }
  54.  
  55.     /**
  56.      * Tells this component that it is being removed from a container.
  57.      * This is a standard Java AWT method which gets called by the AWT when
  58.      * this component is removed from a container. Typically, it is used to
  59.      * destroy the peers of this component and all its subcomponents.
  60.      *
  61.      * It has been overridden here to stop the nervous text thread.
  62.      *
  63.      * @see #addNotify
  64.      */
  65.     public synchronized void removeNotify() {
  66.         if (jitter != null){
  67.             jitter.stop();
  68.             jitter = null;
  69.         }
  70.         super.removeNotify();
  71.     }
  72.  
  73.     /**
  74.      * Makes this component visible.
  75.      * This is a standard Java AWT method which gets called to show this
  76.      * component. If this component was invisible due to a previous hide()
  77.      * call it make this component visible again.
  78.      *
  79.      * @see #hide
  80.      */
  81.     public synchronized void show() {
  82.         super.show();
  83.         if (isVisible()) {
  84.             if (jitter != null)
  85.                 jitter.resume();
  86.         }
  87.     }
  88.  
  89.     /**
  90.      * Makes this component invisible.
  91.      * This is a standard Java AWT method which gets called to hide
  92.      * this component. A hidden component cannot be seen by the user nor
  93.      * does it take up space in its container, but it does continue to
  94.      * exist.
  95.      *
  96.      * @see #show
  97.      */
  98.     public synchronized void hide() {
  99.         super.hide();
  100.         if (!isVisible()) {
  101.             if (jitter != null)
  102.                 jitter.suspend();
  103.         }
  104.     }
  105.  
  106.     /**
  107.      * Specify the text that will be displayed by NervousText.
  108.      *
  109.      * @param str text to be shown as nervous text
  110.      */
  111.  
  112.     public void setText(String str)
  113.     {
  114.         text = str;
  115.         separated =  new char[text.length()];
  116.         text.getChars(0, text.length(), separated, 0);
  117.     }
  118.  
  119.     /**
  120.      * Obtain the text that is currently being displayed.
  121.      *
  122.      * @return text that is being shown by this component
  123.      */
  124.     public String getText()
  125.     {
  126.         return text;
  127.     }
  128.  
  129.     /**
  130.      * NervousText thread body.  This method is called by the Java virtual
  131.      * machine to when this thread starts.
  132.      */
  133.     public void run()
  134.     {
  135.         while (true)
  136.         {
  137.  
  138.             try
  139.             {
  140.                 Thread.sleep(150);
  141.             }
  142.             catch(Exception e)
  143.             {
  144.             }
  145.             if (!paused)
  146.             {
  147.                 repaint();
  148.             }
  149.         }
  150.     }
  151.  
  152.     /**
  153.      * Temporarily suspend the animation of the nervous text.
  154.      * @param f pause the animation of true
  155.      *
  156.      */
  157.     public void pause(boolean f)
  158.     {
  159.         paused = f;
  160.     }
  161.  
  162.     /**
  163.      * Obtain animator's current state.
  164.      *
  165.      * @return true if the animator is paused, false if it is running
  166.      */
  167.  
  168.     public boolean isPaused()
  169.     {
  170.         return paused;
  171.     }
  172.     /**
  173.      * Temporarily suspend the animation of the nervous text. Identical
  174.      * to pause(boolean).
  175.      * @param f pause the animation of true
  176.      *
  177.      */
  178.     public void setPaused(boolean f)
  179.     {
  180.         paused = f;
  181.     }
  182.  
  183.     /**
  184.      * Paints this component using the given graphics context.
  185.      * This is a standard Java AWT method which typically gets called
  186.      * by the AWT to handle painting this component. It paints this component
  187.      * using the given graphics context. The graphics context clipping region
  188.      * is set to the bounding rectangle of this component and its <0,0>
  189.      * coordinate is this component's top-left corner.
  190.      *
  191.      * @param g the graphics context used for painting
  192.      * @see java.awt.Component#repaint
  193.      * @see java.awt.Component#update
  194.      */
  195.     public void paint(java.awt.Graphics g)
  196.     {
  197.         Dimension dim = size();
  198.         g.clipRect(0,0,dim.width,dim.height);
  199.  
  200.         for(int i = 0;i < text.length(); i++)
  201.         {
  202.             int x_coord;
  203.             int y_coord;
  204.             x_coord = (int) (Math.random() * 10 + 20 * i);
  205.             y_coord = (int) (Math.random() * 10 + 36);
  206.             g.drawChars(separated, i, 1, x_coord, y_coord);
  207.         }
  208.     }
  209. }
  210.