/**********************************************************************************
 *                                                                                *
 *                            Diamond diagram Applet                              *
 *                               ( Diamond.java )                                 *
 *                                                         Author : Seiichi Inoue *
 **********************************************************************************/

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

/************************** <<Oneself of class definition>> ***********************/
//    Class name      : Diamond
//    Access control  : public( Access possibility even from which class )
//    Extends class   : Applet
//    Implements class: Runnable( Use of Thread is enabled )
//    Input parameter : NAME="r"    ( Radius )
//                      NAME="node" ( Node number )
public class  Diamond  extends Applet implements Runnable {

/************************* << Class attribute of definition >> ********************/
    Thread      kicker=null;                  /* Thread(Initial value:suspension) */
    Dimension   d;                            /* Display range                    */
    Image       offs;                         /* Off screen                       */
    Graphics    grf;                          /* Drawing range                    */
    Color       color;                        /* Color of line                    */
    int         red,green,blue;               /* Color of line parameter          */
    String      param;                        /* Parameter reading                */
    int         r;                            /* Drawing radius                   */
    int         n;                            /* Node number                      */
    int         x;                            /* Drawing offset (horizontal)      */
    int         y;                            /* Drawing offset (vertical)        */
    int         k;                            /* Starting position counter        */
    int         x1;                           /* Starting horizontal axis (x)     */
    int         y1;                           /* Starting vertical axis (y)       */
    int         j;                            /* Ending position counter          */
    int         x2;                           /* Ending horizontal axis (x)       */
    int         y2;                           /* Ending vertical axis (y)         */

/***************** << Class of method (implementation procedure) >> ***************/
//
/******** Initialization (init) method *********/
    public void init() {
        d = size();                             /* Set display screen size        */
        offs = createImage(d.width,d.height);   /* Off scr area preparation       */
        grf = offs.getGraphics();               /* Graphics object extraction     */
        grf.setColor(Color.white);              /* Set display back color         */
        grf.fillRect(0,0,d.width,d.height);     /* Display range is applied       */

        param = getParameter("r");              /* Radius parameter reading       */
        r = (param != null)?                    /* Input determination            */
            Integer.parseInt(param): 100;
        param = getParameter("node");           /* Node number parameter reading  */
        n = (param != null)?                    /* Input determination            */
            Integer.parseInt(param): 16;
    }

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

/*********** Repeatedly (run) method ***********/
    public void run() {
        Thread.currentThread().setPriority(Thread.NORM_PRIORITY-3);
        while( kicker != null) {              /* Repeat until kicker becomes null */
            try {                               /* Interruption confirmation      */
                red = (int) ( Math.random()*256 );  /* Red component generation   */
                green = (int) ( Math.random()*256); /* Green component generation */
                blue = (int) (Math.random()*256);   /* Blue component generation  */
                color = new Color( red,green,blue );/* Line color assembly        */

                double rd=3.14159/180;          /* Angle value is made 64 bit     */
                for ( k=0; k<=n-2; k++ ) {      /* Drawing start point calculation*/
                    x1 = (int) ( r*Math.cos( k*360/n*rd ) + r ); /* Start x axis  */
                    y1 = (int) ( r*Math.sin( k*360/n*rd ) + r ); /* Start y axis  */
                    x = ( d.width - r*2 )/2;    /* Offset calculation             */
                    if ( x <= 0 )               /* Offset determination(negative?)*/
                        x = 0;                  /* YES: Zero setting              */
                    x1 += x;                    /* Start offset addition          */
                    y = ( d.height - r*2 )/2;   /* Offset calculation             */
                    if ( y <= 0 )               /* Offset determination(negative?)*/
                        y = 0;                  /* YES: Zero setting              */
                    y1 += y;                    /* Start offset addition          */
                    for ( j=k+1; j<=n-1; j++ ) {/* Drawing end point calculation  */
                        x2 = (int) ( r*Math.cos( j*360/n*rd ) + r );/* End x axis */
                        y2 = (int) ( r*Math.sin( j*360/n*rd ) + r );/* End y axis */
                        x2 += x;                /* End offset addition            */
                        y2 += y;                /* End offset addition            */

                        kicker.sleep(20);       /* Line drawing waiting (20mSEC)  */
                        repaint();              /* Drawing method calling         */
                    }
                }
                kicker.sleep(2000);             /* Next drawing waiting time:2sec */
            } catch(InterruptedException e) {}  /* Interruption processing        */
        }
        kicker = null;                          /* Repeate process completion set */
    }                                           /* End of run method              */

/*********** Renewal (update) method ***********/
    public void update(Graphics g) {            /* Lost of screen flickering      */
        paint(g);                               /* Drawing                        */
    }                                           /* End of update method           */

/*********** Drawing (paint) method ************/
    public void paint(Graphics g) {
        grf.setColor( color );                  /* Line color setting             */
        grf.drawLine( x1,y1,x2,y2 );            /* Display information setting    */
        g.drawImage(offs,0,0,this);             /* Drawing setting                */
    }

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

}                                               /* End of class setting           */

/**********************************************************************************
 *                       End of Diamond diagram Applet                            *
 **********************************************************************************/