CS2- Lab 2b, Due 2/6 & 2/9

Introduction

This lab assumes you have a working version of the Life lab to modify. If yours is not working by the end of lab today, please start with the one from class. It has been/will be compressed into xxx.zip, and is on the examplesFromClass directory. You should copy it to your own directory structure and unzip it (in OS-X double-click to expand where it is).

New features for the life simulation (1-5 for Friday, the rest for next Mon)

  1. Add a Reset control
  2. Add a run/stop control; it should run until either:
    1. the state of every Cell is identical with its previous state (i.e. the board has not changed), or,
    2. the current board is identical with what it was 2 time steps ago (i.e. it is oscillating).
  3. Make the board variable sized (under the user's control)
  4. Implement graphical output.
    1. Display the state of the life board by coloring squares of the Frame different colors for live and dead
    2. Make the speed variable, under the user's control
  5. Read the initial state of all the Cells from a file (See Chapter 10, in this PDF for how to use MyReader and StringTokenizer).
  6. Add a save function that saves the current state of the world to a file (so you can read it back in later!).
  7. Allow the user to alter the current state with the mouse.

How to Proceed:

Here's one way to proceed incrementally; whatever you do, don't try to write it all at once!
  1. Multiple steps
    1. Animation in Java must be implemented in a separate Thread
    2. As shown in class, add an infinite loop at the end of the Frame constructor.
    3. Making your program pause:
      final int DELAY_IN_MSEC = 100; // All caps means CONSTANT -- final enforces it!
      ...
      try {
          Thread.sleep(DELAY_IN_MSEC);
      } catch (Exception e) {}
                          
  2. The Reset button should reinitialize the Board and display
  3. Continuous run: complexify the expression controlling the infinite loop (Arrays.equals(array1, array2) does not work!
  4. Use a Slider to control the size of the board; use sizeSlider.getValue() in reset
  5. Implement graphical output.
    1. As shown in class, add a JPanel to your JFrame (having set its layout to BorderLayout!)
    2. In the JPanel, override public void paintComponent(Graphics g); then when repaint is sent to the Frame, paintComponent will be invoked in the panel.
    3. See page 2 onward for basics of Graphics
    4. First (to make sure it's executing) simply fill one square in red, like
      
          public void paintComponent(Graphics g) {
      	g.setColor(Color.red);
      	g.fillRect(100,100,100,100);
          }
      
    5. As in the magic square example, define variables for the upper left corner of the display area, and its width, then modify paint to fill the entire thing with red and then draw a border around it in black (g.drawLine(x1, y1, x2, y2) is the method to draw a line).
    6. Define a variable for the width of an individual Cell and add code to draw the upper left Cell in black (on top of the red so you can see it easily)
    7. Write paint(Graphics) for the Board class, if you fill the display area with, say, white, in the Frame, you will only need to color in the live cells

Extra Credit:

  1. Allow the board to be unlimitedly large (or at least 1,000,000 x 1,000,000).