package celluar;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/**
* Title: Game of Life
* Description:
* Copyright: Copyright (c) 2003
* Company: Http://agents.yeah.net
* @author Keats
* @version 1.0
*/
public class celluar extends Applet implements Runnable {//加implements Runnable是使这个类能作为线程独立运行
Thread runner;//为类定义一个线程,在start中启用
int size=50;//世界的大小
double per=0.1;//起始时生存者占整个世界的比率
int refreshSteps=1;//每演化几步刷新屏幕一次
//参考系数:size=100,per=0.08;网页中:size=50,per=0.1;
boolean running;//运行停启标志
boolean started;//开始标志
int stepLeft=0;//单步运行时使用
int steps=0;//演化的步数
int cyclemax=2000;//cyclemax为记载历史数据的最大限度
int HistoryData[];//历史数据,主要记载每次生存格子的比例
int cCount=0;//每一步生存格子的比例
int delay=50;//cpu每delay10计算一次
int width=300,heightall=350;//定义长宽
int height=300;//作图区域的高
Button pausebutton;
int grid[][][];//计算时确定格子的坐标
int rule[][][][][][][][][];//判断规则,每次预先生成
int cd=0;//用于grid[cd][][],值为0,1交换,实现实际数组和暂存数组的交换
int square,fringe;//作图时方格及空隙大小
boolean isStandalone = false;//系统参数,是否独立运行
/**Get a parameter value*/
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
/**Construct the applet*/
public celluar() {
HistoryData=new int[cyclemax];
}
/**Initialize the applet*/
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
resize(width,heightall);//重置窗口大小
pausebutton = new Button("Start");
Button btn=new Button("set");
Button btn1=new Button("+");
Button btn2=new Button("-");
setLayout(new BorderLayout());//定义对齐方式
Panel p = new Panel();//定义一个面板
this.setBackground(Color.black);
p.setBackground(Color.lightGray);
btn1.setLabel("faster");
btn2.setLabel("slower");
p.add(pausebutton);//加控件之后要添加进去
p.add(new Button("reset"));
p.add(btn);
p.add(new Button("show"));
p.add(btn1);
p.add(btn2);
add("South",p);
reinit();//initial一次
}
public void reinit(){
steps=0;//演化步数置零
started=true;//置一下开始,而后129清空,并置为false等待运行
square = width/size-1;
fringe = 1;
grid = new int[2][size][size];
rule = new int [2][2][2][2][2][2][2][2][2];
//随机赋值
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
double ran=Math.random();
if (ran<per){
grid[cd][i][j]=1;
}
else{
grid[cd][i][j]=0;
}
}
}
//自动生成规则
for(int i1=0;i1<2;i1++){
for(int i2=0;i2<2;i2++){
for(int i3=0;i3<2;i3++){
for(int i4=0;i4<2;i4++){
for(int i5=0;i5<2;i5++){
for(int i6=0;i6<2;i6++){
for(int i7=0;i7<2;i7++){
for(int i8=0;i8<2;i8++){
rule[0][i1][i2][i3][i4][i5][i6][i7][i8]=0;
rule[1][i1][i2][i3][i4][i5][i6][i7][i8]=0;
int addition=i1+i2+i3+i4+i5+i6+i7+i8;
if(addition==2){
rule[1][i1][i2][i3][i4][i5][i6][i7][i8]=1;
}
else if(addition==3){
rule[0][i1][i2][i3][i4][i5][i6][i7][i8]=1;
rule[1][i1][i2][i3][i4][i5][i6][i7][i8]=1;
}
}
}
}
}
}
}
}
}
for(int i=0;i<cyclemax;i++){
HistoryData[i]=-1;//给历史纪录赋值
}
repaint();//画出图
update(this.getGraphics());//用于网页中
}
/**Start the applet*/
//必须用到的函数,如果用到implements Runnable
public void run() {
repaint();
while (true) {
if (running || stepLeft > 0) {//stepleft为单步运行做的判断;而running为连续运行服务
decision();//确定值
if( (steps % refreshSteps == 0)|| (stepLeft > 0)) repaint();//每个几步画一次
if (stepLeft > 0) stepLeft--;//单步时运行完一步就使stepleft小于0,然后就不再做decision
showStatus("Steps:"+(steps++));//状态条
try{Thread.sleep(delay);}catch(InterruptedException e){};//每隔delay秒cpu运算一次,也就是做一次decision
}
else{try{Thread.sleep(500);}catch(InterruptedException e){};}
}
}
public void decision(){
int cCount=0;//每一步的生存者赋初值零
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
//边界可循环
int x1=((i-1)+size)%size,x3=((i+1)+size)%size,y1=((j-1)+size)%size,y3=((j+1)+size)%size;
//先把值给暂存数组
grid[1-cd][i][j]=rule[grid[cd][i][j]][grid[cd][x1][y1]][grid[cd][i][y1]][grid[cd][x3][y1]][grid[cd][x1][j]][grid[cd][x3][j]][grid[cd][x1][y3]][grid[cd][i][y3]][grid[cd][x3][y3]];
if (grid[1-cd][i][j]==1) cCount++;//记录生存者
}
}
cd=1-cd;//转换成现实数组
int index=steps%cyclemax;//最大记录为cyclemax个
HistoryData[index]=cCount;//记录每次生存者数目
}
public void start() {
if(runner==null){runner=new Thread(this);runner.start();}//启用线程
}
/**Stop the applet*/
public void stop() {
if(runner!=null){
runner.stop();
runner = null;
}
}
/**Destroy the applet*/
public void destroy() {
if(runner!=null){
runner.stop();
runner = null;
}
}
/**Get Applet information*/
public String getAppletInfo() {
return "Applet Information";
}
/**Get parameter info*/
public String[][] getParameterInfo() {
return null;
}
/**Main method*/
public static void main(String[] args) {
celluar applet = new celluar();
applet.isStandalone = true;
Frame frame;
frame = new Frame() {
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public synchronized void setTitle(String title) {
super.setTitle(title);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
};
frame.setTitle("Applet Frame");
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(300,320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
public void update(Graphics g) { paint(g);}//网页中画图
public void paint (Graphics g) {
if(started) {g.clearRect(0,0,width,height);started=false;}//初始时清空面板
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
Color cl;
if(grid[cd][i][j]==1){
cl=Color.orange;
}else{
cl=Color.black;
}
g.setColor(cl);
g.fillRect(i*(square+fringe),j*(square+fringe),square,square);
}
}
}
//按钮事件
public boolean action(Event ev, Object arg){
if ( ev.target instanceof Button) {//instanceof实例
String button = (String) arg;
if (button.equals("Stop")) {
pausebutton.setLabel("Start");
running = false;
}
else if (button.equals("Start")) {
pausebutton.setLabel("Stop");
running = true;
}
else if (button.equals("reset")) {//单步运行
/*stepLeft = 1;
if (running) {
pausebutton.setLabel("Start");
running = false;
}*/
reinit();
running=true;
pausebutton.setLabel("Stop");
}
else if (button.equals("set")){//设置参数
running=false;
CelluarControl ctl=new CelluarControl(this);
ctl.setSize(300,300);
ctl.show();
}
else if (button.equals("show")){//显示数据
ViewLog ctl=new View
评论0