User:Simpsons contributor/WireWorld processor

package cellularautomaton;

import java.awt.Color; import java.io.IOException;

/** * Creates the next generation of a cellular automaton using the * WireWorld algorithm. Can return the next generation in the form * of an ImageHolder object or save it to disc. * @author George Miller */

public class Automaton_WireWorld implements CellularAutomaton {   private Color gnd = new Color(48, 48, 48), con = new Color(255, 128, 0), head = new Color(0, 128, 255), tail = new Color(255, 255, 255); /**    * Changes the color that represents the state "gnd" * from the default color to a new color. * @param colorIn New color that will represent gnd instead * of the default value. */   public void setGNDColor(Color colorIn) {       gnd = colorIn; }   /**     * Changes the color that represents the state "conductor" * from the default color to a new color. * @param colorIn New color that will represent conductor instead * of the default value. */   public void setConductorColor(Color colorIn) {       con = colorIn; }   /**     * Changes the color that represents the state "electron head" * from the default color to a new color. * @param colorIn New color that will represent electron head instead * of the default value. */   public void setElectronHeadColor(Color colorIn) {       head = colorIn; }   /**     * Changes the color that represents the state "electron tail" * from the default color to a new color. * @param colorIn New color that will represent electron tail instead * of the default value. */   public void setElectronTailColor(Color colorIn) {       tail = colorIn; }   public ImageHolder process(ImageHolder imageIn) {       //Create blank image holder int width = imageIn.getWidth; int height = imageIn.getHeight; ImageHolder imageOut = new ImageHolder(width, height); for(int x=1; x<width-1; x++) {           for(int y=1; y<height-1; y++) {               //Get color of the pixel Color oldColor = imageIn.getColor(x, y); Color newColor = gnd; /*Perform apropriate transformation based on rules: * Gnd → gnd * Electron head → Electron tail * Electron tail → Conductor * Conductor → Electron head if exactly one or two of                *  the neighbouring cells are electron heads and remains * Conductor otherwise. */                               if(isHead(oldColor) == true) newColor = tail; if(isTail(oldColor) == true) newColor = con; if(isCon(oldColor) == true) {                   /*       (x-1) (x)  (x+1) * (y-1) Z1    Z2   Z3                     * (y)    Z4         Z5                     * (y+1)  Z6    Z7   Z8 */ Color z1 = imageIn.getColor(x-1, y-1); Color z2 = imageIn.getColor(x, y-1); Color z3 = imageIn.getColor(x+1, y-1); Color z4 = imageIn.getColor(x-1, y); Color z5 = imageIn.getColor(x+1, y); Color z6 = imageIn.getColor(x-1, y+1); Color z7 = imageIn.getColor(x, y+1); Color z8 = imageIn.getColor(x+1, y+1); //Determine the number of neighbouring electron heads int headCounter = 0; if(isHead(z1) == true) headCounter++; if(isHead(z2) == true) headCounter++; if(isHead(z3) == true) headCounter++; if(isHead(z4) == true) headCounter++; if(isHead(z5) == true) headCounter++; if(isHead(z6) == true) headCounter++; if(isHead(z7) == true) headCounter++; if(isHead(z8) == true) headCounter++; /* Conductor → Electron head if exactly one or two of                    *  the neighbouring cells are electron heads and remains * Conductor otherwise. */                   if(headCounter == 1 || headCounter == 2) newColor = head; else newColor = con; }               imageOut.setColor(x, y, newColor); }       } //End of nested for loops return imageOut; }   public void saveNextGeneration(String generation, String nextGeneration) throws IOException {       ImageHolder image = new ImageHolder(generation); image = process(image); image.save(nextGeneration, "png"); }   public void saveNextGeneration(ImageHolder generation, String nextGeneration) throws IOException {       generation = process(generation); generation.save(nextGeneration, "png"); }   private boolean isCon(Color testColor) {       if(testColor.equals(con)) return true; else return false; }   private boolean isHead(Color testColor) {       if(testColor.equals(head)) return true; else return false; }   private boolean isTail(Color testColor) {       if(testColor.equals(tail)) return true; else return false; } }