上次写的生命游戏有问题,这次补个好的
先定义常量类
package com.LifeGame2;
public class Constant {
static final int LENGTH = 30;//每格生命所占屏幕像素的长度
static final long FREQUENCY = 400;//页面刷新频率400ms/次
}
在定义一个生命类
package com.LifeGame2;
import java.awt.*;
import static com.LifeGame2.Constant.*;
public class Life {
//生命的坐标
int x;
int y;
//定义两个live
boolean live1 = false;
boolean live2 = false;
public Life(int x, int y) {
this.x = x;
this.y = y;
}
//在图像上画的方法
public void paint(Graphics g) {
g.fillRect(x*LENGTH+2, y*LENGTH+2, LENGTH-3, LENGTH-3);
}
}
在定义一个窗口类
package com.LifeGame2;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import static com.LifeGame2.Constant.LENGTH;
public class MyFrame {
public static void main(String[] args) {
new Run().launch();
}
}
class Run extends Frame {
public void launch() {
this.setSize(1980, 1080);
this.setLocation(0, 0);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
for (int i = 0; i < f.length; i++) {
for (int j = 0; j < f[i].length; j++) {
f[i][j] = new Life(i, j);
}
}
{
f[20][20].live1 = true;
f[20][21].live1 = true;
f[21][20].live1 = true;
f[19][20].live1 = true;
f[20][22].live1 = true;
f[18][20].live1 = true;
f[22][21].live1 = true;
f[21][22].live1 = true;
f[18][19].live1 = true;
f[21][23].live1 = true;
f[21][18].live1 = true;
f[30][30].live1 = true;
f[31][29].live1 = true;
f[32][29].live1 = true;
f[32][30].live1 = true;
f[32][31].live1 = true;
for (int i=0;i<8;i++){
f[i+10][10].live1 = true;
}
}
new PaintThread().start();
}
int width = 1920 / LENGTH;
int length = 1080 / LENGTH;
Life[][] f = new Life[width][length];
long time = 0;
long time0 = System.currentTimeMillis();
@Override
public void paint(Graphics g) {
time = (System.currentTimeMillis() - time0) / 1000;
g.setColor(new Color(42, 17, 21));
g.fillRect(0, 0, 1920, 1080);
super.paint(g);
g.setColor(new Color(147, 147, 147));
paintLine(g);
g.setColor(new Color(28, 159, 32));
paintLife(g);
}
//画出分隔线
private void paintLine(Graphics g) {
for (int i = 0; i < width; i++) {
g.drawLine(LENGTH * i, 0, LENGTH * i, 1080);
}
for (int i = 0; i < length; i++) {
g.drawLine(0, LENGTH * i, 1920, LENGTH * i);
}
}
boolean b = true;
//画出生命
private void paintLife(Graphics g) {
for (int i = 1; i < f.length - 1; i++) {
for (int j = 1; j < f[i].length - 1; j++) {
int number = judge(i, j, b);
if (b) {
if (f[i][j].live1) {
f[i][j].paint(g);
number--;
if (number < 2 || number > 3) {
f[i][j].live2 = false;
}else {
f[i][j].live2 = f[i][j].live1;
}
} else {
if (number == 3) {
f[i][j].live2 = true;
}else {
f[i][j].live2 = f[i][j].live1;
}
}
} else {
if (f[i][j].live2) {
f[i][j].paint(g);
number--;
if (number < 2 || number > 3) {
f[i][j].live1 = false;
}else {
f[i][j].live1 = f[i][j].live2;
}
} else {
if (number == 3) {
f[i][j].live1 = true;
}else {
f[i][j].live1 = f[i][j].live2;
}
}
}
}
}
b = !b;
}
//判断每个生命身边存活数
private int judge(int i, int j, boolean status) {
int number = 0;
for (int ii = 0; ii <= 2; ii++) {
for (int jj = 0; jj <= 2; jj++) {
if (status) {
if (this.f[i + 1 - ii][j + 1 - jj].live1) {
number++;
}
} else {
if (this.f[i + 1 - ii][j + 1 - jj].live2) {
number++;
}
}
}
}
return number;
}
//线程
class PaintThread extends Thread {
@Override
public void run() {
while (true) {
repaint();
try {
Thread.sleep(com.LifeGame2.Constant.FREQUENCY);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//双缓冲
private Image offScreenImage = null;
public void update(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(1920, 1080);
}
Graphics gOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage, 0, 0, null);
}
}
这次就没有问题了