
/* 
   This file defines a program that shows a little red square
   wandering around in a black window.  It is based on the
   MosaicFrame class, which can actually display a grid of
   rectangle in which each rectangle has its own specified
   color.  If the red sqaure wanders off one edge of the
   window, it appears at the opposit edge.
*/

public class MosaicApplication {

   public static void main(String[] args) {
      
      MosaicFrame mosaic = new MosaicFrame(30,30);
          // Open a window with 30 rows and 30 columns of rectangles,
          // all initially black.  NOTE that the rows and columns
          // are numbered from 0 to 29!!

      int row = 15;  // The row in which the red square is located.
      int col = 15;  // The column in which the red square is located.
                     // (Initial values of 15 put the square in the middle
                     //          of the window.)

      while (mosaic.stillOpen()) { // repeat as long as the window stays open

         // executing this while loop will move the red square
         // one space either left, right, up, or down.
         
         int newRow = row;  // row to which square will move
         int newCol = col;  // column to which square will move
                            // (Initial values say that the sqaure won't
                            //    move, but the values are changed before
                            //    they are used to actually move the square.)

         int rand = (int)(4*Math.random());  // a random number from 0 to 3,
                                             //   used to decide which direction to move

         if (rand == 0) {  // move left
            if (row > 0)
               newRow = row - 1;
            else  // square was already at left edge; move to right edge
               newRow = 29;
         }
         else if (rand == 1) {  // move right
            if (row < 29)
               newRow = row + 1;
            else  // square was already at right edge; move to left edge
               newRow = 0;
         }
         else if (rand == 2) {  // move up
            if (col > 0)
               newCol = col - 1;
            else  // square was already at top edge; move to bottom
               newCol = 29;
         }
         else {  // move down
            if (col < 29)
               newCol = col + 1;
            else  // square was already at bottom edge; move to top
               newCol = 0;
         }

         mosaic.setColor(row,col,0,0,0);         // set old square location to black
         mosaic.setColor(newRow,newCol,255,0,0); // set new square location to maximum red

         row = newRow;  // record the new location of the square in row,col
         col = newCol;

         mosaic.delay(100);  // Insert a delay of 100 milliseconds. This sets
                             // the speed of the square; it moves about 10
                             // times per second.

      }  // end of while loop

   }  // end of main()

}  // end of class MosaicApplication