import java.awt.*;
import java.awt.image.*;
import java.util.*;
import java.net.*;
import java.io.*;
import java.applet.Applet;

//***************************************************************************

public class Door extends Applet implements Runnable{

  Thread thread;
  int timeout = 500;
  int timeoutdef = 1000;

  String name[][][];
  Image image[][][];
  int picture[][];

  int maxidef = 15;
  int maxi = 15;
  int maxx = 8;
  int maxy = 6;

  int ci;
  int cx;
  int cy;
  
  URL dochome;

  Random random;

//---------------------------------------------------------------------------

  public void init(){

    String at = getParameter( "speed" );
    timeout = ((at == null) ? timeoutdef : Integer.valueOf(at).intValue());
    at = getParameter( "number" );
    maxi = ((at == null) ? maxidef : Integer.valueOf(at).intValue());
    System.out.println( maxi );

    dochome = getDocumentBase();

    name = new String[maxi+1][maxx+1][maxy+1];
    for( ci=1; ci<=maxi; ci++ ){
      for( cx=1; cx<=maxx; cx++ ){
        for( cy=1; cy<=maxy; cy++ ){
          name[ci][cx][cy] = ci + "-" + cx + "-" + cy + ".gif";
        }
      }
    }
  
    image = new Image[maxi+1][maxx+1][maxy+1];
    for( ci=1; ci<=maxi; ci++ ){
      for( cx=1; cx<=maxx; cx++ ){
        for( cy=1; cy<=maxy; cy++ ){
          image[ci][cx][cy] = getImage( dochome, name[ci][cx][cy] );
        }
      }
    }

    random = new Random();
    picture = new int[maxx+1][maxy+1];
    for( cx=1; cx<=maxx; cx++ ){
      for( cy=1; cy<=maxy; cy++ ){
        picture[cx][cy] = rand( maxi );
      }
    }

  }

//---------------------------------------------------------------------------

  public void start(){
    if (thread == null) {
      thread = new Thread(this);
      thread.start();
    }
  }

//---------------------------------------------------------------------------

  public void stop() {
    if (thread != null) {
      thread.stop();
      thread = null;
    }
  }

//---------------------------------------------------------------------------

  public void run() {
    int newx;
    int newy;
    int newi;

    while (true) {
      try {
        Thread.currentThread().sleep(timeout);
      } catch (InterruptedException e){
      }
      newx = rand( maxx );
      newy = rand( maxy );
      newi = rand( maxi );
      picture[newx][newy] = newi;
      repaint();
    }
  }

//---------------------------------------------------------------------------

  public void update(Graphics g) {

    ImageObserver observer = null;

    int x = 0;
    int y = 0;
    int cx;
    int cy;

    for( cx=1; cx<=maxx; cx++ ){
      y = 0;
      for( cy=1; cy<=maxy; cy++ ){
        int pic = picture[cx][cy];
        g.drawImage( image[pic][cx][cy], x, y, observer );
        y = y + image[1][1][1].getHeight( observer );
      }
      x = x + image[1][1][1].getWidth( observer );
    }

  }

//---------------------------------------------------------------------------

  public int rand( int max ){
    return (Math.abs(random.nextInt()) % max) + 1;
  }

//---------------------------------------------------------------------------

}

