PictureCA.java


Below is the syntax highlighted version of PictureCA.java.


/***********************************************************************
  * Name:    Donna Gabai
  * NetID:   dgabai 
  * Precept: P01
  * 
  * Description: visualizes operation of a 1D cellular automaton
  * Dependencies: CA, Picture
  **********************************************************************/

import java.awt.Color;

public class PictureCA {
    public static void main(String[] args) {
        // Constants
        int STEPS = 255;        // fixed number of steps per instructions
        char BLANK = ' ';       // space character
        
        //input rules from command-line
        String rules = args[0];
        
        // create a CA and a blank Picture (default is all black)
        CA cellAuto =  new CA(STEPS, rules);
        Picture pic = new Picture(STEPS*2 + 1, STEPS + 1);
        
        // generate a row for each step including initial state
        for (int row = 0; row <= STEPS; row++) {
            String state = cellAuto.toString();
            int width = state.length();
            for (int col = 0; col < width; col++) {
                // spaces become white pixels, 1s stay black
                if (state.charAt(col) == BLANK) pic.set(col, row, Color.WHITE);
            }
            // next step 
            cellAuto.step();
        }
        
        // display the picture
        pic.show();
    }
}