New to Java? We'll help you get started with our revised beginner's tutorial, or our free online textbook.


Get the latest Java books
h t t p : / /w w w . j a v a c o f f e e b r e a k . c o m /

Java Coffee Break

Solution for
Programming Exercise 4.1


THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook.

Exercise 5: The sample program RandomMosaicWalk.java from Section 4.6 shows a "disturbance" that wanders around a grid of colored squares. When the disturbance visits a square, the color of that square is changed. The applet at the bottom of Section 4.7 shows a variation on this idea. In this applet, all the squares start out with the default color, black. Every time the disturbance visits a square, a small amount is added to the red component of the color of that square. Write a subroutine that will add 25 to the red component of one of the squares in the mosaic. The row and column numbers of the square should be passed as parameters to the subroutine. Recall that you can discover the current red component of the square in row r and column c with the function call Mosaic.getRed(r,c). Use your subroutine as a substitute for the changeToRandomColor() subroutine in the program RandomMosaicWalk2.java. (This is the improved version of the program from Section 4.7 that uses named constants for the number of rows, number of columns, and square size.) Set the number of rows and the number of columns to 80. Set the square size to 5.


Discussion

This is an exercise in making a rather small modification to a relatively complicated existing program.

The only real problem is to write a new subroutine, which I will call brightenSquare. Much of the program comes directly from RandomMosaicWalk2.java. The randomMove() routine is unchanged. The only change in the main() routine is to substitute a call to brightenSquare for the call to changeToRandomColor. The subroutines fillWithRandomColors and changeToRandomColor in the RandomMosaicWalk2 program are not needed and should be removed. In the three lines that define the constants, the values are changed according the instructions in the exercise:

        final static int ROWS = 80;        // Number of rows in the mosaic.
        final static int COLUMNS = 80;     // Number of columns in the mosaic.
        final static int SQUARE_SIZE = 5;  // Size of each square in the mosaic.

With these values, the program is interesting to watch for a while. You might want to try using shades of green, blue, or gray, instead of red.

An outline for the brightenSquare routine is clear:

           Let r be the current red component of the square
           Add 25 to r
           Set the color components of the square to  r, 0, 0

The green and blue components of the color will always be zero. However, they must be specified in the Mosaic.setColor() routine. Written in Java, the body of the routine is just three lines long:

       static void brightenSquare(int row, int col) {
           int r = Mosaic.getRed(row,col);
           r += 25;
           Mosaic.setColor(row,col,r,0,0);
       }

In fact, you could even write the body of the routine using just one line:

         Mosaic.setColor(row, col, Mosaic.getColor(row,col) + 25, 0, 0);

One thing here might bother you: It looks like the value of the red component of a given square might get bigger than 255 if the disturbance visits it often enough. But the largest legal value for a color component is 255. What I haven't told you is that when a value greater than 255 is used for a color component, Mosaic.setColor will silently change the value to 255. If this were not the case, it would be necessary to rewrite brightenSquare to avoid illegal values of r:

       static void brightenSquare(int row, int col) {
           int r = Mosaic.getRed(row,col);
           r += 25;
           if ( r > 255 )
               r = 255;
           Mosaic.setColor(row,col,r,0,0);
       }

The Solution

    public class Brighten {
    
             /*
                 This program shows a mosaic of little colored
                 squares.  Initially, all the squares are black.
                 A "disturbance" moves randomly around
                 in the window.  Each time it visits a square,
                 the square gets a little more red.  Depends on the 
                 non-standard classes Mosaic and MosaicCanvas.
             */
       
       final static int ROWS = 80;
       final static int COLUMNS = 80;
       final static int SQUARE_SIZE = 5;
           
       static int currentRow;    // Row currently containing the disturbance.
       static int currentColumn; // Column currently containing disturbance.
               
       public static void main(String[] args) {
           Mosaic.open(ROWS, COLUMNS, SQUARE_SIZE, SQUARE_SIZE);
           currentRow = ROWS / 2;   // start at center of window
           currentColumn = COLUMNS / 2;
           while (Mosaic.isOpen()) {
               brightenSquare(currentRow, currentColumn);
               randomMove();
               Mosaic.delay(20);
           }
       }  // end of main()
       
       static void brightenSquare(int row, int col) {
              // Increases the level of red in the square at
              // the specified row and column.
              // (setting grenn and blue levels to zero).
           int r = Mosaic.getRed(row,col);
           r += 25;
           Mosaic.setColor(row,col,r,0,0);
       }
        
       static void randomMove() {
              // Randomly move the disturbance in one of
              // four possible directions: up, down, left, or right;
              // if this moves the disturbance outside the window,
              // then move it to the opposite edge of the window.
           int directionNum;  // Randomly set to 0, 1, 2, or 3, to choose direction.
           directionNum = (int)(4*Math.random());
           switch (directionNum) {
              case 0:  // move up 
                 currentRow--;
                 if (currentRow < 0)
                    currentRow = ROWS - 1;
                 break;
              case 1:  // move right
                 currentColumn++;
                 if (currentColumn >= COLUMNS)
                    currentColumn = 0;
                 break; 
              case 2:  // move down
                 currentRow ++;
                 if (currentRow >= ROWS)
                    currentRow = 0;
                 break;
              case 3:  
                 currentColumn--;
                 if (currentColumn < 0)
                    currentColumn = ROWS - 1;
                 break; 
            }
       }  // end of randomMove()
       
    } // end of class Brighten

[ Exercises | Chapter Index | Main Index ]