home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / JAVA / NOTES / SOURCE / rmrwalk.jav < prev    next >
Text File  |  1996-12-20  |  4KB  |  126 lines

  1.  
  2. import java.awt.*;
  3.  
  4. public class RandomMosaicRandomWalk extends java.applet.Applet implements Runnable {
  5.  
  6.          /*
  7.                    Applet shows a window full of randomly colored
  8.                    squares.  A "disturbance" moves randomly around
  9.                    in the window, randomly changing the color of
  10.                    each square that it visits.  The program runs
  11.                    in an infinite loop, and so will never end on its
  12.                    own.
  13.          */
  14.                 
  15.    int ROWS = 10;    // number of rows of squares; can be reset by applet param named "rows"
  16.    int COLUMNS = 20; // number of colums of squares; can be reset by applet param named "columns"
  17.  
  18.    int currentRow;       // row currently containing "disturbance"
  19.    int currentColumn;    // column currently containing "disturbance"
  20.    MosaicCanvas mosaic;  // the actual mosaic of colored squares
  21.  
  22.    Thread runner = null; // thread for running the moving disturbance animation
  23.    
  24.    public void init() {
  25.       setLayout(new BorderLayout());
  26.       String param;
  27.       param = getParameter("rows");
  28.       if (param != null) {
  29.          try {
  30.             ROWS = Integer.parseInt(param);
  31.          }
  32.          catch (NumberFormatException e) {
  33.          }
  34.       }
  35.       param = getParameter("columns");
  36.       if (param != null) {
  37.          try {
  38.             COLUMNS = Integer.parseInt(param);
  39.          }
  40.          catch (NumberFormatException e) {
  41.          }
  42.       }
  43.       mosaic = new MosaicCanvas(ROWS,COLUMNS);
  44.       add("Center",mosaic);
  45.    }
  46.    
  47.    public void start() {
  48.       if (runner == null) {
  49.          runner = new Thread(this);
  50.          runner.start();
  51.       }
  52.    }
  53.    
  54.    public void stop() {
  55.       if (runner != null) {
  56.          runner.stop();
  57.          runner = null;
  58.       }
  59.    }
  60.    
  61.    public void run() {
  62.       currentRow = ROWS / 2;
  63.       currentColumn = COLUMNS / 2;
  64.       fillWithRandomColors();
  65.       while (true) {
  66.          changeToRandomColor(currentRow,currentColumn);
  67.          randomMove();
  68.          try {
  69.             Thread.sleep(25);
  70.          }
  71.          catch (InterruptedException e) {
  72.          }
  73.       }
  74.    }
  75.    
  76.    void fillWithRandomColors() {
  77.         // fill every square, in each row and column,
  78.         // with a random color
  79.         for (int row=0; row < ROWS; row++) {
  80.            for (int column=0; column < COLUMNS; column++) {
  81.               changeToRandomColor(row, column);  
  82.            }
  83.         }
  84.    }  // end of fillWithRandomColors()
  85.    
  86.    void changeToRandomColor(int rowNum, int colNum) {
  87.         // change the square in row number rowNum and
  88.         // column number colNum to a random color.
  89.         double red = Math.random();    // choose random levels in range
  90.         double green = Math.random();  //     0.0 to 1.0 for red, green, 
  91.         double blue = Math.random();   //     and blue color components
  92.         mosaic.setColor(rowNum,colNum,red,green,blue);  
  93.     }  // end of changeToRandomColor()
  94.     
  95.     void randomMove() {
  96.         // randomly move the disturbance in one of
  97.         // four possible directions: up, down, left, or right;
  98.         // if this moves the disturbance outside the window,
  99.         // then move it to the opposit edge of the window.
  100.         int directionNum = (int)(4*Math.random());
  101.              // direction num is randomly set to 0, 1, 2, or 3
  102.         switch (directionNum) {
  103.            case 0:  // move up 
  104.               currentRow--;
  105.               if (currentRow < 0)
  106.                   currentRow = ROWS - 1;
  107.               break;
  108.            case 1:  // move right
  109.               currentColumn++;
  110.               if (currentColumn >= COLUMNS)
  111.                 currentColumn = 0;
  112.               break; 
  113.            case 2:  // move down
  114.               currentRow ++;
  115.               if (currentRow >= ROWS)
  116.                   currentRow = 0;
  117.               break;
  118.            case 3:  
  119.               currentColumn--;
  120.               if (currentColumn < 0)
  121.                   currentColumn = COLUMNS - 1;
  122.               break; 
  123.         }
  124.     }  // end of randomMove()
  125.     
  126. } // end of class