/**********************************************************************************
 *                                                                                *
 *                               Check Box Applet                                 *
 *                              ( CheckBox.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      : CheckBox
//    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  CheckBox  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                    */
    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;

        setLayout(new BorderLayout());          /* Set Border Layout              */
        Panel p = new Panel();                  /* Set Panel instance             */
        p.setBackground(Color.yellow);          /* Set Background color of panel  */
        p.setLayout(new GridLayout(1,8));       /* Arrange checkbox to grid       */
        CheckboxGroup cg = new CheckboxGroup(); /* Set Group of the checkbox      */
        p.add(new Checkbox("red",cg,true));     /* Red color checkbox (initial)   */
        p.add(new Checkbox("pink",cg,false));   /* Pink color checkbox            */
        p.add(new Checkbox("orange",cg,false)); /* Orange color checkbox          */
        p.add(new Checkbox("yellow",cg,false)); /* Yellow color checkbox          */
        p.add(new Checkbox("green",cg,false));  /* Green color checkbox           */
        p.add(new Checkbox("cyan",cg,false));   /* Cyan color checkbox            */
        p.add(new Checkbox("blue",cg,false));   /* Blue color checkbox            */
        p.add(new Checkbox("gray",cg,false));   /* Gray color checkbox            */
        add("South",p);                         /* Set Panel to the lower part    */
        grf.setColor( Color.red );              /* Set initial color(red)         */

    }                                           /* End of initial method          */

/*************** Action method *****************/
    public boolean action(Event e,Object o) {
        Checkbox c = (Checkbox)e.target;        /* Get object that was clicked    */
        if ("red".equals(c.getLabel()))         /* Red color box ?                */
            grf.setColor( Color.red );          /*   Set red color                */
        else if ("pink".equals(c.getLabel()))   /* Pink color box ?               */
            grf.setColor( Color.pink );         /*   Set pink color               */
        else if ("orange".equals(c.getLabel())) /* Orange color box ?             */
            grf.setColor( Color.orange );       /*   Set orange color             */
        else if ("yellow".equals(c.getLabel())) /* Yellow color box ?             */
            grf.setColor( Color.yellow );       /*   Set yellow color             */
        else if ("green".equals(c.getLabel()))  /* Green color box ?              */
            grf.setColor( Color.green );        /*   Set green color              */
        else if ("cyan".equals(c.getLabel()))   /* Cyan color box ?               */
            grf.setColor( Color.cyan );         /*   Set cyan color               */
        else if ("blue".equals(c.getLabel()))   /* Blue color box ?               */
            grf.setColor( Color.blue );         /*   Set blue color               */
        else if ("gray".equals(c.getLabel()))   /* Gray color box ?               */
            grf.setColor( Color.gray );         /*   Set grau color               */
        else                                    /* Neither which color?           */
            grf.setColor( Color.black );        /*   Set black color              */
        return true;

    }                                           /* End of action method           */

/************ 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      */
                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         */
                    }
                }
            } 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.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 Check Box Applet                              *
 **********************************************************************************/