home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / JavaNow / Code / AppB / NervousText / NervousText.java < prev    next >
Encoding:
Java Source  |  1996-07-10  |  2.9 KB  |  110 lines

  1. /*  Daniel Wyszynski 
  2.     Center for Applied Large-Scale Computing (CALC) 
  3.     04-12-95 
  4.  
  5.     Test of text animation.
  6.  
  7.     kwalrath: Changed string; added thread suspension. 5-9-95
  8. */
  9. import java.awt.Graphics;
  10. import java.awt.Font;
  11.  
  12. public class NervousText extends java.applet.Applet
  13.                                           implements Runnable {
  14.  
  15.     // used to hold the string s broken out into an array of
  16.     // characters (this is to allow each character to be
  17.     // displayed separately)
  18.     char separated[];
  19.  
  20.     // the string to display
  21.     String s = null;
  22.  
  23.     // the current thread (curious name though)
  24.     Thread killme = null;
  25.  
  26.     // miscellaneous controls
  27.     int i;
  28.     int x_coord = 0, y_coord = 0;
  29.     String num;
  30.     int speed=35;
  31.     int counter =0;
  32.  
  33.     // keep track of whether we're suspended or not
  34.     // (clicking the mouse suspends the applet)
  35.     boolean threadSuspended = false; //added by kwalrath
  36.  
  37. public void init() {
  38.     // read the text to display from the HTML file;
  39.     // default is "HotJava"
  40.     s = getParameter("text");
  41.     if (s == null) {
  42.         s = "HotJava";
  43.     }
  44.  
  45.     // now pull the string apart into its individual characters
  46.     separated =  new char [s.length()];
  47.     s.getChars(0,s.length(),separated,0);
  48.  
  49.     // set up the window size and font
  50.     resize(150,50);
  51.     setFont(new Font("TimesRoman",Font.BOLD,36));
  52.  }
  53.  
  54. // start - normal stuff here
  55. public void start() {
  56.     if(killme == null) 
  57.     {
  58.         killme = new Thread(this);
  59.         killme.start();
  60.     }
  61.  }
  62.  
  63. // stop - normal here too
  64. public void stop() {
  65.     killme = null;
  66.  }
  67.  
  68. // run - all the work is in paint
  69. public void run() {
  70.     while (killme != null) {
  71.     try {Thread.sleep(100);} catch (InterruptedException e){}
  72.     repaint();
  73.     }
  74.     killme = null;
  75.  }
  76.  
  77. // paint - repaint the string with each character adjusted
  78. //           slightly by some random amount
  79. public void paint(Graphics g) {
  80.     // loop through the characters in the string
  81.     for(i=0;i<s.length();i++)
  82.     {
  83.  
  84.     // allocate 15 pixels horizontally for each character (this 
  85.     // is based on the size of the font selected in init) -
  86.     // BUT jiggle each character by anywhere from 0 to 10 pixels
  87.     // both horizontally and vertically
  88.     x_coord = (int) (Math.random()*10+15*i);
  89.     y_coord = (int) (Math.random()*10+36);
  90.  
  91.     // now draw the one character from the i'th position of the
  92.     // "separated" array at the calculated coordinates
  93.     g.drawChars(separated, i,1,x_coord,y_coord);
  94.     }
  95.  }
  96.  
  97. // mouseDown - clicking the mouse stops the nervous animation
  98. /* Added by kwalrath. */
  99. public boolean mouseDown(java.awt.Event evt, int x, int y) {
  100.         if (threadSuspended) {
  101.             killme.resume();
  102.         }
  103.         else {
  104.             killme.suspend();
  105.         }
  106.         threadSuspended = !threadSuspended;
  107.     return true;
  108.     }
  109. }
  110.