/** Clear the current cells and replace with the indicated starting pattern. */ public void reset(String patternName) { stop = true; int temp = generationMax; setGenerationCount(0); setMaxGenerations(0); if (patternName.equals("Random")) { theModel.reset(0.5); } else if (patternName.equals("R-Pentamino")) { theModel.reset(1); } else if (patternName.equals("Box")) { theModel.reset(2); } else if (patternName.equals("X Box")) { theModel.reset(3); } canvas.repaint(); // runGenerations(); //* Replace this by appropriate use of a thread synchronized (canvas) { canvas.notifyAll(); stop = false; canvas.repaint(); } setMaxGenerations(temp); setGenerationCount(0); }
/** Set the number of cells in the width & height of the model. */ public void setModelSize(int newSize) { modelSize = newSize; theModel.setSize(modelSize); sizeDisplay.setText("" + modelSize); setGenerationCount(0); canvas.repaint(); }
/** The main function for displaying the Life game area. */ private void paintCanvas(Graphics g) { Dimension canvasSize = canvas.getSize(); int xdelta = canvasSize.width / modelSize; if (xdelta == 0) { xdelta = 1; } int ydelta = canvasSize.height / modelSize; if (ydelta == 0) { ydelta = 1; } for (int x = 0; x < modelSize; ++x) { for (int y = 0; y < modelSize; ++y) { int age = theModel.getCell(x, y); // System.out.print("("+x+","+y+"): " + age); if (generationCount >= 10 && age > 0) { age = 1 + 10 * age / generationCount; } if (age >= 10) { age = 9; } Color c = (age > 0) ? colorMap[age - 1] : Color.black; // System.out.println(" " + age + " color: " + c); g.setColor(c); g.fillRect(x * xdelta, y * ydelta, xdelta, ydelta); } } Thread.yield(); }
/** Compute the next generation. */ public void nextGeneration() { // System.err.println("Computing generation " + generationCount); theModel.nextGeneration(); setGenerationCount(generationCount + 1); canvas.repaint(); }