/** * The Cell class represents one "light" in a grid of lights. There * are four static class fields which controll overall behavior and * four instance veraibles which control the positioning and status * of each particular cell. The cells are controlled through a * two-phase protocol where each is first marked as going to * light up or go out (via either lightsUp() or goesOut()), then * the change is effected with the update() method. * * @author Phil Nico, Kevin O'Gorman * @version 27 February 2003 */ import java.awt.*; public class Cell{ // Static fields which control the drawing process private static Graphics dsply; // where to draw... private static Color litcolor; // foreground color private static Color dimcolor; // background color private static int dotsize; // how big is each dot /** * Initialize parameters. * * @param scr Where things are to be drawn. * @param fg The foreground color. * @param bg The background color. * @param dsize The dot size. */ public static void setUpCellParams(Graphics scr, Color fg, Color bg, int dsize){ // because this is static, it'll apply to // all instances of cell dsply = scr; litcolor = fg; dimcolor = bg; dotsize = dsize; } /** * Set the display object. * * @param scr Where things are to be drawn. */ public static void setDisplay(Graphics scr){ dsply = scr; } private boolean lit; // is this cell lit private boolean will_light; // will it be in the next generation private int row; // where is this cell located private int col; /** * Constructor for a Cell. * * @param x The row. * @param y The column. * @param lit True if lit. * @param will_light True if this Cell will light on the next update. */ public Cell(int x, int y){ row = x; col = y; lit = false; will_light = false; } /** * Return the state of the Cell. * * @return True if the Cell is currently lit. */ public boolean isLit(){ return lit; } /** * Return the next state of the Cell. * * @return True if the Cell will be lit on the next update. */ public void lightsUp(){ will_light = true; } /** * Return the next state of the Cell. * * @return True if the Cell will be dim on the next update. */ public void goesOut(){ will_light = false; } /** * Update to the next state. Lights or goes dim according * to the internal state. */ public void updateCell(){ // Move the will-light (future) value to to lit, the // current state of the light. lit = will_light; // Update generational info will_light = false; } /** * Display the cell according to the state of Cell. */ public void showCell(){ // write either a lit or dim circle on top of the current // location. Color old; old = dsply.getColor(); // preserve the old color if ( lit ) { dsply.setColor(litcolor); dsply.fillOval( row * dotsize, col * dotsize, dotsize-1, dotsize-1); } else { dsply.setColor(dimcolor); dsply.fillOval( row * dotsize, col * dotsize, dotsize-1, dotsize-1); } dsply.setColor(old); // Restore the old color } }