home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-07-10 | 2.9 KB | 110 lines |
- /* Daniel Wyszynski
- Center for Applied Large-Scale Computing (CALC)
- 04-12-95
-
- Test of text animation.
-
- kwalrath: Changed string; added thread suspension. 5-9-95
- */
- import java.awt.Graphics;
- import java.awt.Font;
-
- public class NervousText extends java.applet.Applet
- implements Runnable {
-
- // used to hold the string s broken out into an array of
- // characters (this is to allow each character to be
- // displayed separately)
- char separated[];
-
- // the string to display
- String s = null;
-
- // the current thread (curious name though)
- Thread killme = null;
-
- // miscellaneous controls
- int i;
- int x_coord = 0, y_coord = 0;
- String num;
- int speed=35;
- int counter =0;
-
- // keep track of whether we're suspended or not
- // (clicking the mouse suspends the applet)
- boolean threadSuspended = false; //added by kwalrath
-
- public void init() {
- // read the text to display from the HTML file;
- // default is "HotJava"
- s = getParameter("text");
- if (s == null) {
- s = "HotJava";
- }
-
- // now pull the string apart into its individual characters
- separated = new char [s.length()];
- s.getChars(0,s.length(),separated,0);
-
- // set up the window size and font
- resize(150,50);
- setFont(new Font("TimesRoman",Font.BOLD,36));
- }
-
- // start - normal stuff here
- public void start() {
- if(killme == null)
- {
- killme = new Thread(this);
- killme.start();
- }
- }
-
- // stop - normal here too
- public void stop() {
- killme = null;
- }
-
- // run - all the work is in paint
- public void run() {
- while (killme != null) {
- try {Thread.sleep(100);} catch (InterruptedException e){}
- repaint();
- }
- killme = null;
- }
-
- // paint - repaint the string with each character adjusted
- // slightly by some random amount
- public void paint(Graphics g) {
- // loop through the characters in the string
- for(i=0;i<s.length();i++)
- {
-
- // allocate 15 pixels horizontally for each character (this
- // is based on the size of the font selected in init) -
- // BUT jiggle each character by anywhere from 0 to 10 pixels
- // both horizontally and vertically
- x_coord = (int) (Math.random()*10+15*i);
- y_coord = (int) (Math.random()*10+36);
-
- // now draw the one character from the i'th position of the
- // "separated" array at the calculated coordinates
- g.drawChars(separated, i,1,x_coord,y_coord);
- }
- }
-
- // mouseDown - clicking the mouse stops the nervous animation
- /* Added by kwalrath. */
- public boolean mouseDown(java.awt.Event evt, int x, int y) {
- if (threadSuspended) {
- killme.resume();
- }
- else {
- killme.suspend();
- }
- threadSuspended = !threadSuspended;
- return true;
- }
- }
-