RandomBoard.java



import java.awt.Graphics;
import java.awt.Color;

public class RandomBoard extends ParamApplet {

     /** カラー・セル */

       protected Cell cell[][];

     /** セルの縦横の個数 */

       int iMax, jMax;

     /** セルの縦横のサイズ */

       int cellWidth, cellHeight;

    /** 初期設定のメソッド */

       public void init() {

          // パラメータから升目の個数を得る。

             try {
                  iMax = getIntParameter( "column" );  // 列の数
             }
             catch( Exception e ) {
                  iMax = 4;                   // デフォルト値
             }
             if( iMax <= 0 ) iMax = 4;        // デフォルト値

             try {
                  jMax = getIntParameter( "row" );   // 行の数
             }
             catch( Exception e ) {
                  jMax = 4;                   // デフォルト値
             }
             if( jMax <= 0 ) jMax = 4;        // デフォルト値

         // サイズの端数を切り捨ててサイズを調整

             size().width = size().width - size().width%iMax;
             size().height = size().height - size().height%jMax;
             resize( size().width, size().height );
             cellWidth = size().width/iMax;
             cellHeight = size().height/jMax;

         // セルの生成

             cell = new Cell[iMax][jMax];
             for( int i=0; i<iMax; i++ )
               for( int j=0; j<jMax; j++ )
                 cell[i][j] = new Cell( cellWidth, cellHeight );
       }

    /** アプレットの表示(初期状態)を行うメソッド */

       public void paint( Graphics g ) {

             setCellsColor();
             paintCells( g );
       }

    /** セルの色を再設定する */

       protected void setCellsColor() {
 
             Color color;
             float r, g, b;

             for( int i=0; i<iMax; i++ )
               for( int j=0; j<jMax; j++ ) {
                   color = new Color ( (float)Math.random(),
                                       (float)Math.random(),
                                       (float)Math.random() );
                   cell[i][j].setColor( color );
               }
       }
 
    /** セルの再描画を行う */

       protected void paintCells( Graphics g ) {
 
             for( int i=0; i<iMax; i++ ) {
                  for( int j=0; j<jMax; j++ ) {
                       cell[i][j].paint( g );
                       g.translate( 0, cellHeight );
                  }
                  g.translate( cellWidth, cellHeight*(-jMax) );
             }
       }
}