/* * Paul Garrett, 29 Sept 98 * * Conway's "life" game * * For now, wrap-around geometry... * */ import java.awt.*; import java.util.*; public class Life extends java.applet.Applet implements Runnable { boolean initial_state = true; // to remember initial config boolean running = false; Stack newly_drawn = new Stack(); boolean drawing = false; final int cell_size = 8; // in pixels: should be divisor of width and height final int cell_padding = 1; // interstices thickness... int nwx = 0; // current nw corner of box to paint int ix = 0; // current i-index in array int nwy = 0; // current nw corner of box to paint int iy = 0; // current j-index in array // all 4 of the following must be non-zero: final int big_i_inc = 1; // horizontal increment in outer loop final int big_j_inc = 1; // vertical increment in outer loop final int small_i_inc = 1; // horizontal increment in outer loop final int small_j_inc = 1; // vertical increment in outer loop final int big_x_inc = big_i_inc * cell_size; final int big_y_inc = big_j_inc * cell_size; final int small_x_inc = small_i_inc * cell_size; // within paint() inner loop final int small_y_inc = small_j_inc * cell_size; // within paint() inner loop final static int width = 704; // of whole picture final static int height = 520; // of whole picture final int horiz = width/cell_size; // number of cells horizontally final int vert = height/cell_size; // number of cells vertically final int number_of_cells = horiz * vert; // number of cells to color in a single paint() int tick_length = vert * horiz; // should be multiple of "horiz" Thread th; // to run the animation int time = 0; // counts generations int[][] states = new int[horiz][vert]; // states of cells int[][] orig_states = new int[horiz][vert]; // states of cells int[][] next_states = new int[horiz][vert]; // states of cells // tells which color/population it will draw in int mouseState = 0; // clicking should change it... Color[] colors = { Color.gray, Color.black }; /**********************************************************************/ public static void main(String[] argv) { Frame fr = new Frame(); fr.addNotify(); fr.setLayout(null); // Insets insets = fr.getInsets(); Insets insets = fr.insets(); // 1.0 api // fr.setSize(insets.left+insets.right+width, insets.top+insets.bottom+height); fr.resize(insets.left+insets.right+width, insets.top+insets.bottom+height); fr.show(); fr.setBackground(new Color(0x999999)); Life se = new Life(); fr.add(se); se.init(); // have to initialize some stuff before drawing it... // se.setLocation(insets.left, insets.top); // 1.1 api se.move(insets.left, insets.top); // 1.0 api se.resize(width, height); // 1.0 api } /**********************************************************************/ final public void generation() { int neighbors; // from 0 to 8 int iless, imore, jless, jmore; for (int i=0; i 0 && x < width && y > 0 && y < height ) { int i = x/cell_size; int j = y/cell_size; states[i][j] = mouseState; if ( initial_state == true ) { orig_states[i][j] = mouseState; } int[] indices = {i, j}; newly_drawn.push( indices ); drawing = true; repaint(); } return true; } public boolean mouseDown(Event e, int x, int y) { mouseState = 1 - mouseState; // just 0 or 1 int i = x/cell_size; int j = y/cell_size; states[i][j] = 1 - states[i][j]; if ( initial_state == true ) { orig_states[i][j] = 1 - orig_states[i][j]; } int[] indices = {i, j}; newly_drawn.push( indices ); drawing = true; repaint(); return true; } public boolean keyDown(Event e, int k) { if ( k == 103 && running == false ) { // == 'g' for 'go' initial_state = false; repaint(); running = true; _start(); return true; } else if ( k == 115 && running == true ) { // == 's' for 'stop' running = false; _stop(); repaint(); return true; } else if ( k == 114 && running == false ) { // == 'r' for 'reset' for (int i=0; i