/** * We are using the class DrawingSurface to create objects * that will interact with each other. We are simulating a * simple version of "the game of life". * * @author John Sloan * @version CS 161 Lab 7 */ public class Cell { private int diameter; private int x, y; private Cell C0, C1, C2, C3; private String color; private boolean on; private DrawingSurface surface; // all these fields are instance and member variables /** * Constructor for a Cell object. * All fields are initialized in the constuctor * @param x The horizontal position in pixels * @param y The vertical position in pixels * */ public Cell(int x, int y, Cell C0, String color, DrawingSurface surface ) { // Initialize Instance variables // Besides diameter and on, the remaining member variables use the this keyword to give the method a // first option reference. The definition from the closest encloing block! diameter = 10; on = true; this.C0 = C0; this.x = x; this.y = y; this.color = color; this.surface = surface; //The following are coditional statements followed by initializing member variables. if(x < 250) { this.C1 = new Cell ( x + 40, y, this, "blue", surface ); } else {this.C1 = null;} if(y < 250) { this.C2 = new Cell ( x, y + 40, this, "green", surface ); } else{this.C2 = null;} if ((x < 250) && (y < 250)) { this.C3 = new Cell ( x + 15, y + 25, this, "red", surface ); } else{this.C3 = null;} } /** * true false simple return accessor. * */ public boolean isOn() { return on; } /** * The method fire */ public void fire() { int value = 0; //local if( (C0 != null) && (C0.isOn()) ) { value++; } if( (C1 != null) && (C1.isOn()) ) { value++; } if( (C2 != null) && (C2.isOn()) ) { value++; } if( (C3 != null) && (C3.isOn()) ) { value++; } if( C0 == null ) { value++; } if( C1 == null ) { value++; } if( C2 == null ) { value++; } if( C3 == null ) { value++; } if((value==2)||(value==3)) { on = true; surface.draw(x, y, color); } else { on = false; surface.erase( x, y ); } if(C1 != null ) { C1.fire(); } if(C2 != null ) { C2.fire(); } if(C3 != null ) { C3.fire(); } } }