// SmooshedLetters2.java
// Smooshes up my name for web page
// Smooshes up DR. STEPHEN G. BUELL
// Returns with JAVA AND either FINANCE or FIN 125
import java.awt.*;
import java.applet.Applet;

public class SmooshedLetters2 extends Applet implements Runnable {
        private int xPos[ ];
        private int x[ ];
        private int k;
        private int cXLine, cYLine, xSpace;
        private boolean flip;
        private int rn;  // random number for color
        private Font f;

        private Color colors[ ] = new Color[ 5 ];

        private Thread threadL;

        private Graphics gOff;  // off-secreen graphics buffer
        private Image buffer;  // off-screen buffer

        public void init( ) {
                cXLine = 210;  // measured from left of the applet window
                cYLine = 48;  // measured from top of applet window
                xSpace = 18;
                k = 9;
                xPos = new int[ 18 ];
                x = new int[ 18 ];
        
                for ( int i = 0; i < xPos.length; i++ ) {
                        xPos[ i ] = cXLine - k * xSpace;
                        // Speed of each letter's movement in spaces per repaint
                        x[ i ] = k--;
                }

                flip = true;

                // initialize colors array
                colors[ 0 ] = Color.red;
                colors[ 1 ] = Color.blue;
                colors[ 2 ] = Color.green;
                colors[ 3 ] = Color.black;
                colors[ 4 ] = Color.magenta;

                // create off-screen buffer
                buffer = createImage( 420, 50 );  // creates image buffer
                gOff = buffer.getGraphics( );  // gets graphics context

                threadL = new Thread( this, "Letter Thread" );
                threadL.start( );

                f = new Font( "TimesRoman", Font.BOLD, 42 );   // FONT SIZE +3 = 36??
                gOff.setFont( f );
        }

        public void stop( ) {
                threadL.stop( );
        }

        public void run( ) {
                
                while ( true ) {

                        // sleep for .13 seconds
                        try {
                                threadL.sleep( 130 );
                        }
                        catch ( InterruptedException e ) {
                                e.printStackTrace( );
                        }

                        // set background color to white
                        gOff.setColor( Color.white );
                        gOff.fillRect( 0, 0, size( ).width, size( ).height );

                        // draw letters
                        gOff.setColor( colors[ rn ] );
                        k = 0;
                        gOff.drawString( flip ? "D" : " ", xPos[ k ], cYLine );
                        gOff.drawString( flip ? "R." : "J", xPos[ ++k ], cYLine );
                        gOff.drawString( flip ? "  " : "A", xPos[ ++k ], cYLine );
                        gOff.drawString( flip ? "S" : "V", xPos[ ++k ], cYLine );
                        gOff.drawString( flip ? "T" : "A", xPos[ ++k ], cYLine );
                        gOff.drawString( flip ? "E" : " ", xPos[ ++k ], cYLine );
                        gOff.drawString( flip ? "P" : "A", xPos[ ++k ], cYLine );
                        gOff.drawString( flip ? "H" : "N", xPos[ ++k ], cYLine );
                        gOff.drawString( flip ? "E" : "D", xPos[ ++k ], cYLine );
                        gOff.drawString( flip ? "N" : " ", xPos[ ++k ], cYLine );
                        gOff.drawString( flip ? "  " : "F", xPos[ ++k ], cYLine );
                        gOff.drawString( flip ? "G." : "I", xPos[ ++k ], cYLine );
                        gOff.drawString( flip ? "  " : "N", xPos[ ++k ], cYLine );
                        gOff.drawString( flip ? "B" : " ", xPos[ ++k ], cYLine );
                        gOff.drawString( flip ? "U" : "1", xPos[ ++k ], cYLine );
                        gOff.drawString( flip ? "E" : "2", xPos[ ++k ], cYLine );
                        gOff.drawString( flip ? "L" : "5", xPos[ ++k ], cYLine );
                        gOff.drawString( flip ? "L" : " ", xPos[ ++k ], cYLine );

                        //xPos1 += x1;
                        if ( xPos[ 0 ] >= cXLine - 5 ) {
                                for ( int i = 0; i < x.length; i++ ) {
                                        x[ i ] *= -1;
                                }
                                flip = !flip;
                                rn = ( int ) ( 5 * Math.random( ) );  // get new random color
                        }
                        
                        if ( xPos[ 0 ] <= 10 ) {
                                for ( int i = 0; i < x.length; i++ ) {
                                        x[ i ] *= -1;
                                }
                        }

                        for ( int i = 0; i < xPos.length; i++ ) {
                                xPos[ i ] += x[ i ];
                        }

                        repaint( );
                        }
                }

        public void paint( Graphics g ) {
                g.drawImage( buffer, 0, 0, this );
        }

        //  override update to eliminate flicker
        //  do not clear background
        public void update( Graphics g ) {
                paint( g );
        }
}