import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class CA extends JFrame implements ActionListener,MouseListener,MouseMotionListener
{
private static final String name = null;
private JMenu file, edit, set, run, view,windows, help;
private JLabel jla;
private JTextField jte;
private JButton but1,but2,but3,but4;
/* private int size = 400;
private int bao1[] = new int[size];
private int bao2[] = new int[size]; */
private final int xnum =40,ynum = 40;
private final int cellsize = 10;
private int now[][];
private Container con,con2;
private JPanel showpan;
private Keyout keyout;
private Stack stack;
private acting ac;
public CA()
{
super("基于CA-火灾逃逸模拟");
getContentPane().setBackground(Color.white);
//菜单条
file = new JMenu ("文件");
edit = new JMenu ("编辑");
set = new JMenu ("设置");
run = new JMenu ("运行");
view = new JMenu ("浏览");
windows = new JMenu ("窗口");
help = new JMenu ("帮助");
JMenuItem xin, open, close, save, saveAs;
xin = new JMenuItem ("打开");
open = new JMenuItem ("新建");
save = new JMenuItem ("保存");
saveAs = new JMenuItem ("另存为");
close = new JMenuItem ("关闭");
JMenu del = new JMenu("删除");
del.add ("删除全部");
JMenuItem delLine = new JMenuItem ("删除这行");
del.add (delLine);
delLine.setEnabled (false);
edit.add (del);
file.add (xin);
file.add (open);
file.add (save);
file.add (saveAs);
file.add (close);
JMenuBar bar = new JMenuBar ();
bar.add (file);
bar.add (edit);
bar.add (set);
bar.add (run);
bar.add (view);
bar.add (windows);
bar.add (help);
setJMenuBar (bar);
//button设置
but1 = new JButton("绘图");
add(but1);
but1. setBounds(30,80,60,30);
but1.addActionListener(this);
but2 = new JButton("开始");
add(but2);
but2.setBounds(30, 240, 60, 30);
but2.addActionListener(this);
but3 = new JButton("暂停");
add(but3);
but3.setBounds(30, 290, 60, 30);
but3.addActionListener(this);
but4 = new JButton("清屏");
add(but4);
but4.setBounds(30, 340, 60, 30);
but4.addActionListener(this);
//label设置
jla= new JLabel(" 输入参数");
add(jla);
jla.setBounds(30, 170, 60, 30);
//textfield设置
jte = new JTextField(50);
add(jte);
jte.setBounds(30, 200, 60, 30);
//容器设置
//动态核心函数涉及
showpan = new JPanel( new BorderLayout());
showpan.setSize(new Dimension(400,400));
now= new int[xnum+10][ynum+10];
stack = new Stack();
keyout = new Keyout();
keyout.addMouseListener(this);
keyout.addMouseMotionListener(this);
showpan.add(keyout);
ac = new acting();
ac.start(); //生命演化进程运行
ac.suspend(); //演化暂停
this.add(showpan);
this.add(showpan);
//整体框架设计
this.setSize(650,550);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
// chushi();
}
//动作函数设计
//
public void initact()
{
ac.suspend();
initcell();
while(!stack.isEmpty())stack.pop();
initcell();
keyout.initGraphic();
keyout.setNew();
}
private void initcell()
{
for( int i=0; i<xnum+10; i++ )
for( int j=0; j<ynum+10; j++ )
now[i][j] = 0;
}
//初始时候的随机函数设计
public void initrandom()
{
int x,y;
initact();
for( int i=0; i<300; i++ )
{
x = (int)(Math.random()*(40));
y = (int)(Math.random()*(40));
keyout.setAlive(x,y);
stack.push( new Point(x+4,y+4) );
now[x+4][y+4] = 1;
}
keyout.setNew();
}
//定义内部类,专门用于
public class Keyout extends JPanel
{
private Graphics g;
private Image frontImage,backImage;
private Graphics ft,bg;
public Keyout()
{
backImage = new BufferedImage( xnum*10, ynum*10, 1);
bg = backImage.getGraphics();
frontImage = new BufferedImage( xnum*10, ynum*10, 1);
ft = frontImage.getGraphics(); //定图像位置大小
initBack();
initGraphic();
}
public void paintComponent( Graphics g )
{
super.paintComponent(g);
g.drawImage(frontImage, 150, 30, this); //定图像位置
}
public void initGraphic()
{
ft.drawImage(backImage, 0, 0, this); //以定位图像的坐标为信坐标
}
public void initBack() //初始化背景图像
{
bg.setColor(Color.LIGHT_GRAY);
bg.fillRect( 0, 0, xnum*10, ynum*10 ); //以定位图像的坐标为信坐标
bg.setColor( Color.white );
for ( int i = 0; i < xnum; i++ )
for ( int j = 0; j < ynum; j++ )
bg.drawRect(i * 10, j * 10, 10,10);
}
public void setAlive( int x,int y ) //设置x、y位置为活状态
{
ft.setColor( Color.red );
ft.fillRect( x * 10+1, y * 10+1, 10-1,10-1 );
}
public void setNew() //重画
{
repaint();
}
}
private class acting extends Thread
{
private Point point;
public acting() {}
private void next()
{
initcell();
keyout.initGraphic();
while( !stack.empty() )
{ //计算每个生命周围的生命数
point = (Point)stack.pop();
now[ point.x ][ point.y ] +=-8 ;
now[ point.x + 1 ][ point.y ] += 1;
now[ point.x - 1 ][ point.y ] += 1;
now[ point.x + 1 ][ point.y + 1 ] += 1;
now[ point.x + 1 ][ point.y - 1 ] += 1;
now[ point.x - 1 ][ point.y + 1 ] += 1;
now[ point.x - 1 ][ point.y - 1 ] += 1;
now[ point.x ][ point.y + 1 ] += 1;
now[ point.x ][ point.y - 1 ] += 1;
}
for( int i=1; i<xnum+9; i++ )
for( int j=1; j<ynum+9; j++ )
{
if( now[i][j]==-5 || now[i][j]==-6 || now[i][j]==3 )
{ //设置生命状态为活状态
now[i][j] = 1;
stack.push( new Point( i, j ) );
if( (i-4<xnum && i-4>=0) && (j-4<ynum && j-4>=0) )
keyout.setAlive( i-4, j-4 );
}
else now[i][j] = 0;
}
}//end method next;
public void run()
{
while(true)
{
try{Thread.sleep(100);}
catch( InterruptedException exception ){}
next(); //设置下一刻状态
keyout.setNew(); //刷新所有生命状态
}
}
}
public void mouseClicked( MouseEvent event )
{}
public void mousePressed( MouseEvent event )
{}
public void mouseReleased( MouseEvent event )
{}
public void mouseEntered( MouseEvent event )
{}
public void mouseExited( MouseEvent event )
{}
public void mouseDragged( MouseEvent event )
{}
public void mouseMoved( MouseEvent event )
{}
//响应函数设计
public void actionPerformed( ActionEvent event )
{
if( event.getSource() == but1 )//绘图
{