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

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