//
//    CHAPTER 10
//    Applet Source Name [StringYield.java]
//                                    1996.2
//                 Author [ Junzo hagimoto ]

import java.applet.*;
import java.awt.*;


public  class  StringYield  extends Applet implements Runnable {
    Dimension d;                                         
    String s[];                                          
    Thread kicker = null;                                
    int    xScroll;                                      
    int    y_Point;
    Font   font;                                         
    int    strHeight,strWidth;                           
    int    maxText;                                      
    int    curTextCount=0 ;                              
    int    red,blue,green;                               
    Color  color;                                        
    int    speed;                                        
    Image  offs;                                         
    Graphics grf;                                        
    char separated[];                                    
    int    charWidth;                              //      
    float  f[];                                      // ADD
    int    maxFloatArray= (int)(3.1415 * 10 * 2) ;   // ADD 
    

    public void init() {
        d = size();
       xScroll = d.width;                    

       offs = createImage( d.width, d.height);                     
       grf  = offs.getGraphics();                                  
                                                       // 
       f  = new float[maxFloatArray];                  // ADD
       double  x = 3.1;                                // ADD
       for(int i=0; i < maxFloatArray; i++ ){          // ADD
           f[i]  = (float)Math.sin(x) ;                // ADD
           x -= 0.1;                                   // ADD
       }                                               // ADD 



        String param = getParameter( "speed" );                    
        speed = (param != null) ? Integer.parseInt(param) : 1;     
        if(speed < 1 || speed > 5 ){                               
          speed = 1;                                            
        }                                                          
       
        param = getParameter( "maxText" );                   
        maxText = (param != null) ? Integer.parseInt(param) : 1;    
        s = new String[maxText];                                    
        int i=0;                                                    
        do{                                                         
            param = getParameter( "text"+ (i+1) );                  
            if(param != null ){                                     
                s[i] = new String(param);                           
             }else{                                                  
                if( i==0 ){                                         
                    s[i] = "Java";                                    
                }                                                   
            maxText = i+1;                                      
            }                                                       
        }while( param != null && ++i != maxText );                  

        font  = new Font("TimesRoman",Font.BOLD,20);           
        strHeight  =(getFontMetrics(font)).getLeading() ;      
        strHeight -=(getFontMetrics(font)).getDescent() ;      
        strHeight +=(getFontMetrics(font)).getAscent() ;       
        y_Point    = (d.height + strHeight
                   - (getFontMetrics(font)).getDescent() ) / 2;
    }


    public void run() {
        Thread.currentThread().setPriority(Thread.NORM_PRIORITY-3);
        while (kicker != null) {
            repaint();
            try {
                Thread.sleep( 20 );                         
            } catch (InterruptedException e) {}
       }
          kicker=null;
    }

    public void update(Graphics g) {                        
           paint(g);                                        
    }                                                       
    public void paint(Graphics g) {
        grf.setFont( font );                                  
        grf.setColor(Color.black);                                   
        grf.fillRect(0, 0, d.width, d.height);
        if(xScroll == d.width){                                       
            red   = (int)(Math.random() * 256);                       
            blue  = (int)(Math.random() * 256);                       
            green = (int)(Math.random() * 256);                       
            color = new     Color( red, green, blue);                 
            curTextCount++;                                           
            if(curTextCount > maxText){                               
                curTextCount=1;                                     
            }                                                         
    
            strWidth = (getFontMetrics(font)).stringWidth(s[curTextCount-1]);
    
            separated =  new char [ s[curTextCount-1].length() ];      
            s[curTextCount-1].getChars(0,                              
                      s[curTextCount-1].length(),separated,0);     

     }                                                             
        grf.setColor(color);
       xScroll -= speed;

        int _xOffset=0;                                       //           
        int _width = d.height -  y_Point;                     // ADD 
        for(int i=0;i < s[curTextCount-1].length();i++){      // ADD
             int _floatIdx=0;                                 // ADD 
             int _y;                                          // UPDATE 

             // DEL  int _x= (int) (Math.random()* 5);   
             //                                                  

             _xOffset += (i != 0)?                                      
             (getFontMetrics(font)).charWidth(separated[i-1]) : 0;  
                                                                        // 
             if( xScroll + _xOffset  < d.width ){                       // ADD
                 if( d.width - xScroll - i >= 0 ){                      // ADD
                   _floatIdx = (d.width - xScroll - i) % maxFloatArray; // ADD
             }else{                                                     // ADD
                   _floatIdx = 0;                                       // ADD
                 }                                                      // ADD
             }                                                          // ADD
             _y =  (int)(f[ _floatIdx ] * _width * 0.9);                // ADD
             grf.drawChars(separated, i,1 ,xScroll +  _xOffset,         // UPDATE
                 y_Point + _y );                                        // UPDATE 
     }
//  
//  DEL      grf.drawString(s[curTextCount-1],
//  DEL              xScroll,d.height/2 +strHeight/2 );                   

     if(xScroll < -strWidth ){                           
         xScroll = d.width;                 
     }
                                      
        g.drawImage(offs, 0, 0, this);                               
    }

    public void start() {
        if (kicker == null) {
           kicker = new Thread(this);
           kicker.start();
        }
    }

    public void stop() {
        if (kicker != null) {
            kicker.stop();
            kicker = null;
        }
    }
}