以下是康威生命游戏的代码,尽管该程序运行良好
GUI以黑白方式打印块。如何更改它的代码以打印出红色(死)和绿色(活着)
public class GameofLife {
private static final long serialVersionUID = 1L;
public static int frameSize = 360;
public static String title = "Game Of Life";
public Random r = new Random();
public int gridSize = 100;
public double generationSpeed = 20.0;
public BufferedImage image;
public int[] pixels;
public boolean[] cGrid;
public boolean[] pGrid;
public GameofLife() {
Dimension d = new Dimension(frameSize, frameSize);
setMinimumSize(d);
setMaximumSize(d);
setPreferredSize(d);
image = new BufferedImage(gridSize, gridSize, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
}
public void start() {
cGrid = new boolean[pixels.length];
pGrid = new boolean[pixels.length];
for (int i = 0; i < cGrid.length; i++)
cGrid[i] = r.nextInt(100) / 100.0 > 0.8 ? true: false;
new Thread(this).start();
}
public void run() {
double frameCut = 1000000000.0 / generationSpeed;
long currentTime = System.nanoTime();;
long previouseTime = currentTime;
long passedTime = 0;
double unprocessedTime = 0.0;
long frameCounter = System.currentTimeMillis();
int generations = 1;
while (true) {
previouseTime = currentTime;
currentTime = System.nanoTime();
passedTime = currentTime - previouseTime;
unprocessedTime += passedTime;
if (unprocessedTime > frameCut) {
unprocessedTime = 0;
update();
generations++;
}
if (System.currentTimeMillis() - frameCounter >= 1000) {
frameCounter = System.currentTimeMillis();
}
render();
}
}
public void update() {
for (int i = 0; i < pixels.length; i++)
pGrid[i] = cGrid[i];
for (int y = 0; y < gridSize; y++) {
for (int x = 0; x < gridSize; x++) {
int res = 0;
int xx0 = x - 1;
int yy0 = y - 1;
int xx1 = x + 1;
int yy1 = y + 1;
if (x != 0) res += pGrid[xx0 + gridSize * y] ? 1 : 0;
if (y != 0) res += pGrid[x + gridSize * yy0] ? 1 : 0;
if (x != gridSize - 1) res += pGrid[xx1 + gridSize * y] ? 1 : 0;
if (y != gridSize - 1) res += pGrid[x + gridSize * yy1] ? 1 : 0;
if (x != 0 && y != 0) res += pGrid[xx0 + gridSize * yy0] ? 1 : 0;
if (x != 0 && y != gridSize - 1) res += pGrid[xx0 + gridSize * yy1] ? 1 : 0;
if (x != gridSize - 1 && y != 0) res += pGrid[xx1 + gridSize * yy0] ? 1 : 0;
if (x != gridSize - 1 && y != gridSize - 1) res += pGrid[xx1 + gridSize * yy1] ? 1 : 0;
if (! (pGrid[x + gridSize * y] && (res == 3 || res == 2))) cGrid[x + gridSize * y] = false;
if (!pGrid[x + gridSize * y] && res == 3) cGrid[x + gridSize * y] = true;
}
}
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
for (int i = 0; i < pixels.length; i++)
pixels[i] = 0;
for (int i = 0; i < pixels.length; i++)
pixels[i] = cGrid[i] ? 0xffffff: 0;
g.drawImage(image, 0, 0, frameSize, frameSize, null);
g.dispose();
bs.show();
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setAlwaysOnTop(true);
GameofLife gol = new GameofLife();
frame.add(gol);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
gol.start();
}
}
答案 0 :(得分:0)
颜色在此代码部分
中设置for (int i = 0; i < pixels.length; i++)
pixels[i] = cGrid[i] ? 0xffffff : 0;
如果要设置不同的开/关颜色,可以进行简单的调整
int off = Color.red.getRGB();
int on = Color.green.getRGB();
for (int i = 0; i < pixels.length; i++)
pixels[i] = cGrid[i] ? on : off;