import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;

/**
  色付きの升目の領域 ColorCell クラス
*/
class ColorCell {

     /** 親のオブジェクト */
       public Applet parent;
     /** セルのインデックス */
       public int i,j;
     /** セルのサイズ */
       public int width, height;
     /** セルの色 */
       public Color color;

    /**
      コンストラクタ（クラスの新しいデータの生成を行う）
    */
       ColorCell( Applet parent, int i, int j,
                  int width, int height ) {

              this.parent = parent;
              this.i = i;   this.j = j;
              this.width = width;  this.height = height;
              this.color = Color.lightGray;
       }
       ColorCell( Applet parent, int i, int j,
                  int width, int height, Color color ) {

              this.parent = parent;
              this.i = i;   this.j = j;
              this.width = width;  this.height = height;
              this.color = color;
       }

    /**
      セル内部に描画を行う関数
    */
       public void paint( Graphics g ){

              g.setColor( color );
              g.fill3DRect( i*width+1, j*height+1,
                            width-2, height-2, true );
       }

   /**
     セルをクリアする関数
   */
       public void clear( Graphics g ){

              g.clearRect( i*width, j*height, width, height );
       }

   /**
     セルに色を再設定する関数
   */
       public void setColor( Color color ){

              this.color = color;
       }
}
// クラス ColorCell の定義の終わり

/**
  ランダムに絵を表示する RandomBoard クラス
 */
public class RandomBoard extends Applet {

     /** 色を塗るためのセル */

       ColorCell cell[][];

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

       public int iMax,jMax;

    /**
     初期設定の関数。セルを生成。アプレットのサイズを調整。
     */
       public void init() {

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

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

             try {
                  jMax = getIntValue( "row" );   // 行の数
             }
             catch( NoValueException 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 );

         // セルの生成

             cell = new ColorCell[iMax][jMax];
             for( int i=0; i<iMax; i++ )
               for( int j=0; j<jMax; j++ )
                 cell[i][j] = new ColorCell( this, i, j,
                              size().width/iMax, size().height/jMax );
       }

    /** 
     アプレットの表示（初期状態）を行う関数。
     */
       public void paint( Graphics g ) {

             setCellsColor();
             paintCells( g );
       }

    /**
      HTMLのデータを受け取り、Strig型からint型に変換する
     */
       public int getIntValue( String keyword ) throws NoValueException {
 
             String valueString = getParameter( keyword );

             if( valueString == null )
                throw  new NoValueException();  // 値が不明という例外

             Integer I = new Integer( valueString ); 
             return I.intValue();
       }
 
    /**
     セルの色を再設定する
     */
       protected void setCellsColor() {
 
             Color color;
             int r, g, b;

             for( int i=0; i<iMax; i++ )
               for( int j=0; j<jMax; j++ ) {
                   r = (int)( Math.random()*255.0f );
                   g = (int)( Math.random()*255.0f );
                   b = (int)( Math.random()*255.0f );
                   color = new Color ( r, g, b );
                   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 );
               }
       }
}
// クラス RandomBoard の定義の終わり

/**
  該当する数値データがないという例外のクラス
 */
class NoValueException extends Exception {
 
      NoValueException() {
             super();      // このクラスは特別な機能は追加していない
      }  
}

