/**********************************************************************************
 *                                                                                *
 *                            Scroll letters Applet                               *
 *                             ( Scroller.java )                                  *
 *                                                         Author : Seiichi Inoue *
 **********************************************************************************/

/********************** << Imported package class definition >> *******************/
import java.applet.*;           /* All of the Applet packages                     */
import java.awt.*;              /* All of the Abstract Windowing Toolkit packages */

/************************** <<Oneself of class definition>> ***********************/
//    Class name      : Scroller
//    Access control  : public( Access possibility even from which class )
//    Extends class   : Applet
//    Implements class: Runnable
public  class  Scroller  extends Applet implements Runnable {

/************************** << Class attribute definition >> **********************/
    Dimension d;                                    /* Display range              */
    String    s[];                                  /* Letter arrangement range   */
    Thread    kicker = null;                        /* Thread                     */
    int       xScroll;                              /* Scroll position            */
    int       y_Point;                              /* Vertical position of letter*/
    Font      font;                                 /* Font                       */
    int       strHeight,strWidth;                   /* Letter height, width       */
    int       maxText;                              /* Letter block number        */
    int       curTextCount = 0;                     /* Present letter block       */
    int       red,blue,green;                       /* Letter color               */
    Color     color;                                /* Letter color               */
    int       speed;                                /* Scroll speed               */
    Image     offs;                                 /* Off screen                 */
    Graphics  grf;                                  /* Drawing range              */

/***************** << Class of method (implementation procedure) >> ***************/
//
/*********** Initialization (init) method ***********/
    public void init() {
        d = size();                                  /* Set display screen size   */
        xScroll = d.width;                           /* Set display start position*/

        offs = createImage(d.width,d.height);        /* Off scr area preparation  */
        grf  = offs.getGraphics();                   /* Graphics object extraction*/
        String param = getParameter("speed");        /* Input "speed" reading     */
        speed = (param != null)?                     /* Input determination       */
            Integer.parseInt(param): 1;
        if(speed < 1 && speed > 5) {                 /* Input abnormal determ     */
            speed = 1;                               /* Set 1 at abnormality      */
        }
        param = getParameter("maxText");             /* Text block number reading */
        maxText = (param != null)?                   /* Input determination       */
            Integer.parseInt(param): 1;
        s = new String[maxText];                     /* Preparation letter arrange*/

        int i=0;                                     /* Loop counting initial     */
        do {                                         /* DO loop                   */
            param = getParameter("text" + (i+1));    /* i+1st text reading        */
            if(param != null) {                      /* Text presence determ      */
                s[i] = new String(param);            /* Yes( Text setting )       */
            } else {                                 /* No                        */
                if( i==0 ) {                         /* No text ?                 */
                    s[i] = "Text Parameter Erorr";   /* Set text abnormal         */
                }
                    maxText = i + 1;                 /* Set text block number     */
                }
        } while(param != null && ++i != maxText);    /* DO loop completion determ */

        font = new Font("Dialog",Font.BOLD,20);      /* Display font setting      */
        strHeight  = (getFontMetrics(font)).getLeading(); /* Display position set */
        strHeight -= (getFontMetrics(font)).getDescent();
        strHeight += (getFontMetrics(font)).getAscent();
        y_Point    = (d.height + strHeight
                      - (getFontMetrics(font)).getDescent())/2;

    }                                                /* End of initial method     */


/*********** Implementation (run) method ************/
    public void run() {
        Thread.currentThread().setPriority(Thread.NORM_PRIORITY-3);
        while (kicker != null) {              /* Repeat until kicker becomes null */
            repaint();                               /* Drawing                   */
            try {                                    /* Interruption confirmation */
                Thread.sleep( 20 );                  /* Wait 20 milli seconds set */
            } catch (InterruptedException e) {}      /* Interruption processing   */
        }
        kicker = null;
    }                                                /* End of run method         */


/************* Renewal (update) method **************/
    public void update(Graphics g)  {
        paint(g);
    }                                                /* End of renewal method     */


/************** Drawing (paint) method **************/
    public void paint(Graphics g) {
        grf.setFont(font);                           /* Set display font          */
        grf.setColor(Color.black);                   /* Set display back color    */
        grf.fillRect(0, 0, d.width, d.height);       /* Display range is applied  */
        if (xScroll == d.width) {                    /* Display position is top?  */
            String param = getParameter("red");      /* Read red designation      */
            red = (param != null)?                   /* Undesignated maximum value*/
               Integer.parseInt(param): 255;
            param = getParameter("blue");            /* Read blue designation     */
            blue = (param != null)?                  /* Undesignated maximum value*/
               Integer.parseInt(param): 255;
            param = getParameter("green");           /* Read green designation    */
            green = (param != null)?                 /* Undesignated maximum value*/
               Integer.parseInt(param): 255;
            color = new Color(red,green,blue);       /* Set display color         */
            curTextCount ++ ;
            if (curTextCount > maxText) {            /* Letter block over ?       */
                curTextCount = 1;                    /* YES: the first block set  */
            }
            strWidth = (getFontMetrics(font)).       /* Read width of letter line */
                        stringWidth(s[curTextCount - 1]);
        }
        grf.setColor(color);                         /* Display color setting     */
        xScroll -= speed;                            /* Reduction of position     */
        grf.drawString(s[curTextCount-1],            /* Display shifted letter    */
                               xScroll,y_Point);
        if(xScroll < -strWidth)  {                   /* Display completion determ */
           xScroll = d.width;                        /* Initial display position  */
        }
        g.drawImage(offs, 0, 0, this);               /* Image display             */
    }                                                /* End of drawing method     */

/*************** Start (start) method ***************/
    public void start() {
        if (kicker == null) {                        /* Kicker is null ?          */
            kicker = new Thread(this);               /* YES: kicker setting       */
            kicker.start();
        }
    }                                                /* End of start method       */

/**************** Stop (stop) method ****************/
    public void stop() {
        if (kicker != null) {                        /* Kicker is not null ?      */
            kicker.stop();
            kicker = null;                           /* YES: suspension of kicker */
        }
    }                                                /* End of stop method        */

}                                                    /* End of class setting      */

/**********************************************************************************
 *                            End of Scroll letters Applet                        *
 **********************************************************************************/