CPSC 124, Fall 2005
Lab 7: Red vs. Blue

For the lab this week, you will be working with the "Mosaic" class that is discussed in Section 4.6 of the textbook. In class, you saw demonstrations of three Java applications that use Mosaic, Convert.jar, ConvertCL.jar, and Diffusion.jar. (These applications can be found in the directory /classes/f05/cs124/mosaic_examples, in case you want to run them yourself.)

To begin the lab, you should start up Eclipse and begin a new project named "Lab 7". Import the files Mosaic.java and MosaicCanvas.java into your project. You can find them in the directory /classes/f05/cs124/lab7. Note that these versions of the files are slightly modified from the versions in the textbook: I have changed all the comments in Mosaic.java to Javadoc format, and I have added one new subroutine, Mosaic.setTitle().

Exercise: There is only one exercise for today's lab, but it has several parts. Only the first three parts are required. The last part can be done for extra credit.
           Part 1, which I hope you will be able to finish lab, is to write a "Convert" application that works with randomly colored squares. This part of the lab is defined in the section headed Random Convert below.
           Part 2 of the exercise is to modify the program so that instead of starting with random colors, it starts with a checkerboard pattern. This part of the lab is described in the section headed Red versus Blue below.
           Part 3, which you must complete to get full credit for the lab, is to use constants in your program to represent the number of rows and the number of columns in the mosaic. Section 4.7 has an extended example that shows how the program from Section 4.6 can be modified to used constants named ROWS, COLUMNS, and SQUARE_SIZE. You should do the same with your program (at least for ROWS and COLUMNS. Be sure that you use the constants consistently throughout the program. Test your work by changing the values of the constants and making sure that your program still works.
           Part 4, which is optional, can be done for extra credit. If you want to do it, you should keep track of the number of red squares and the number of blue squares as the program runs. (Actually, since you know the total number of squares, you only need to keep track of one of these explicitely.) When you "convert" a square, update the numbers. Note that the numbers only change if the conversion actually changes the color of a square. Every 100000 steps, change the title of the window to say something like "After 1200000 Steps: 2927 Red, 3437 Blue". You can change the title by calling Mosaic.setTitle(title), where title is the new title of the window.


Random Convert

In this lab, you will be writing a complete program from scratch, and that program will use the "toolbox" defined by the Mosaic class to do its work. For a description of all the subroutines in this toolbox, you can look at the source code or at the Javadoc documentation that was generated from the source code. Some of the subroutines are also discussed in Section 4.6. Some routines that you will definitely need to complete the lab include the following; others might also be useful:

For the first part of the lab, you should write a program that has essentially the same behavior as ConvertCL (except that it is not required to start out with a "slow" phase before ramping up to full speed). That is, when you run it, your program should do the following:

     Open a mosaic window with 40 rows and 40 columns.
     Fill the mosaic with randomly selected colors.  
                           (Do this by calling Mosaic.fillRandomly().)
     While the mosaic window is open:
         Randomly select a row.
         Randomly select a column.
         Randomly select one of the neighbors of position (row,column).
         Get the color components of the neighbor
         Set the square in position (row, colum) to have the same color 
                                                           as the neighbor.

You will need to create a new main program class in your project. To some limited extent, you can model it on the RandomMosaicWalk example in Section 4.6, but you are only required to write one subroutine -- one that will select a random neighbor of the randomly selected position (row,column). The subroutine will be somewhat similar to the randomMove() subroutine in RandomMosaicWalk.

There is some difficulty with the definition of this subroutine: What to do about a square that is along one of the borders of the mosaic? Most squares -- those that are not on the border -- have four neighbors. The four neighboring positions of position (row,column) are (row+1,column), (row-1,column), (row,column+1), and (row,column-1). However, for a square on the border, one or two of the neighboring positions don't exist as legal positions in the Mosaic. The neatest approach to handling this problem is to pretend that the left edge of the Mosaic is attached to the right edge and the top edge to the bottom edge. This is what is done in the randomMove() subroutine.

You will probably need some help in writing the program and getting it to work. You should try to finish this part of the exercise during the lab. If you get it working, give me a demonstration!


Red versus Blue

Once you have the basic Convert program working with random colors, I would like you to modify it to use a different starting point. Instead of just calling Mosaic.fillRandomly() to fill the mosaic with random colors, you should fill it with a checkerboard pattern of red and blue squares. (Actually, you could use any two colors that you like.) To do this, you only need to write a subroutine, named for example fillCheckerboard, and replace the call to Mosaic.fillRandomly() with a call to fillCheckerboard(). Writing fillCheckerboard() is the real programming exercise in this part of the lab.

There are several approaches that could be taken to filling the mosaic in a checkerboard pattern. All of them will end up using nested for loops to go through the rows and columns of the checkerboard. For example:

You can use any method you want, as long as the mosaic is filled with a checkerboard pattern.


David J. Eck, October 2005