Zebra Stripes

This version behaves very similarly to the original zebra stripes applet, but I used some image processing classes from the standard library to do the work. I also loaded an image as the initial state of the array of cells.

The key observation is that this idea of multiplying neighbors states by weights and then summing them up to determine a cell's next state is really the same thing as the "convolution" operation used in image processing. The Java library developers anticipated people would want to manipulate images so they provided some of the basic tools necessary including the BufferedImage class I've used in previous examples. The new one used here is ConvolveOp along with Kernel.

A Kernel object contains the weights for each neighbor in a rectangular neighborhood and can be thought of as a two dimensional array. You create a Kernel by passing an array of weights to its constructor. It takes a one-dimensional array which is just the weights read off of the 2D kernel left to right and top to bottom. The resulting Kernel object is passed to the constructor of ConvolveOp to create a convolution operator. This object has a filter() method which takes a source image and destination image as parameters. The source image is the one you want to perform the convolution operation on and the destination is where the result will be placed. In my code I continually apply the convolution so I swap the source and destination images after each one so the destination becomes the source for the next operation.

Most of the complexity in this code is my algorithm for mathematically assigning the weights to the convolution kernel. It took lots of trial and error, but the basic idea is that I was trying to overlay a pattern of positive weights representing the activator in reaction-diffusion system over a pattern of negative weights from an inhibitor. The activator pattern has a longer reach vertically causing activated regions to join vertically into stripes. The inhibitor pattern has a longer reach horizontally to make the stripes stable by making regions of cells want to be a different color from their neighbors to the left and right.

Source Code: