//
//    CHAPTER 8
//    Applet Source Name [PingPong.java]
//                                    1996.2
//                 Author [ Junzo hagimoto ]
import java.applet.*;
import java.awt.*;

public  class  PingPong  extends Applet implements Runnable {
    Dimension d;
    String s[];                                                     
    Thread kicker = null;
    int    xScroll;
    Font   font;                                        
    int    strHeight,strWidth;                          
    int    maxText;                                                 
    int    curTextCount=0 ;                                         
    int    red,blue,green;                                          
    Color  color;                                                   
    int    speed;
    Image  offs;
    Graphics grf;                                 //
    int     pingpong_y=0;                         //ADD
    double    time = 0.1;                         //ADD
    int     start_H = 40;                         //ADD 
    int    f_Descent;                             //ADD 
    
    public void init() {
        d = size();
        xScroll = d.width;                    

       offs = createImage( d.width, d.height);  
       grf  = offs.getGraphics();               


        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,25);           
        strHeight  =(getFontMetrics(font)).getLeading() ;      
        strHeight -=(getFontMetrics(font)).getDescent() ;      
        strHeight +=(getFontMetrics(font)).getAscent() ;     // 
        f_Descent  = (getFontMetrics(font)).getDescent() ;   // UPDATE 
    }


    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]) ;
                                                       // 
           start_H = 40;                               // ADD
           time=0.1;                                   // ADD 
         
        }                                                             
        grf.setColor(color);
        xScroll   -= speed;
                                                       // 
        pingpong_y = (int)( start_H * time - 5.0 * time * time) ;// ADD
        time+=0.5;                                               // ADD 
                                                       // 
        if(pingpong_y < 0 ){                           // ADD
            pingpong_y= 0;                             // ADD
            time=0.1;                                  // ADD
            start_H -= 3;                              // ADD
            if(start_H < 0){                           // ADD
                xScroll = d.width;                     // ADD
            }                                          // ADD
        }                                              // ADD 
        grf.drawString(s[curTextCount-1], xScroll  ,
           d.height - f_Descent- pingpong_y);          //UPDATE

//                                             
//DEL        if(xScroll < -strWidth ){                           
//DEL          xScroll  = d.width;                 
//DEL        }                                 
                                      
        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;
        }
    }
}