⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 efloys.java

📁 一个swarm的小程序
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
//
//	This code was written by Ariel Dolan
//	Site	http://www.aridolan.com
//	Email	aridolan@netvision.net.il
//
//	You are welcome to do whatever you wish with this code, as long as you
//	add appropriate credits.
//

import java.applet.*;
import java.awt.*;
import java.net.*;
import java.util.Vector;
import java.util.Date;


class EfloyCanvas extends Canvas
{
	Image image;
	Color color;
	boolean first;
	boolean reset;
	Image OffImg;
	Graphics OffGra;


	public EfloyCanvas(Image img)	{

		super();
 		image = img;
		color = Color.white;
		setBackground(Color.black);
		setForeground(Color.white);
		first = true;
		reset = false;
		//OffImg = createImage(size().width, size().height);

	}

	public EfloyCanvas(Color c)	{

		super();
 		image = null;
		setBackground(Color.black);
		setForeground(Color.white);
		color = c;
		first = true;
		reset = false;
		//OffImg = createImage(size().width, size().height);

	}

	public void Clear() {

		reset = true;
		repaint();

	}


	public void paint(Graphics g) {

		int i;
		Efloy Efloy;

		/*
		if (reset) {
			g.setColor(Color.black);
			g.fillRect(0,0,size().width,size().height);
			reset = false;
			return;
		}

		if (image != null)
			g.drawImage(image,0,0,this);
		else {
		}
		*/

		if (Efloys.Efloys != null) {
			for (i=0;i<Efloys.Efloys.length;i++)
	 		{
				try {
					Efloy = Efloys.Efloys[i];
					Efloy.GetNeighbors();
	 				Efloy.Process();
					Efloy.Draw(g);
				}
				catch (Exception e) {
					//System.out.println("EfloysCanvas Paint. e= "+e.toString());
				}
	 		}
		}

	}


	public void xpaint(Graphics g) {

		int i;
		Efloy Efloy;

		OffImg = createImage(size().width, size().height);

		try {
		    OffGra = OffImg.getGraphics();
		}
	    catch (Exception e) {
			Efloys.appcontext.showStatus("After OffImg e= "+e.toString());
		}


		OffGra.setColor(getBackground());
		OffGra.fillRect(0,0,size().width,size().height);

		for (i=0;i<Efloys.Efloys.length;i++)
	 	{
			Efloy = Efloys.Efloys[i];
			Efloy.GetNeighbors();
	 		Efloy.Process();
			Efloy.Draw(OffGra);
	 	}

		g.drawImage(OffImg, 0, 0, this);
	}


	Graphics GetGra() {

		return this.getGraphics();
	}
}

public class Efloys extends Applet implements Runnable {

	Thread runner;//定义一个新的独立线程
	EfloyCanvas canvas;
	EfloyCommand fcommand;//定义窗体
	EfloyInfo finfo;
	EfloyPredefined fpredefined;
	static Efloy[] Efloys;
	static Efloy[] NewFloys;
	Button Start;
	Button Pause;
	Button Rules;
	Button Info;
	Button Control;
	Button Kick;
	Button Slower;
	Button Faster;
	Button Scramble;
	Button Stranger;
	Button Breed;
	static Button Evolution;
	Button Quit;

	Button Help;
	Button Log;
	Button Predefined;
	Button Numbers;
	Button Sound;
	Button Ranges;

	Graphics gra;
	Panel ControlPanel;
	Panel center;
	Panel center1;
	Label MainTitle;
	URL MainPage;
	static EfloyParam params[];
	static EfloyParam fixpars[];
	static EfloyParam envpars[];
	Font ButtonFont;
	Date StartDate;

	int wa,ha,wc,hc;
	int CurrentBehavior;//当前的行为模式
	int CurrentNum;//当前的ID
	long CurrentStep;//当前的步数
	long CurrentTotalStep;//当前的总步数
	static long CurrentGeneration;//当前的代数
	long CurrentRandom;//进行随机选取邻居的次数
	long SumFitness;//总体适应度
	boolean running;//当前是否在运行
	static boolean First;//是否是刚开始运行
	static boolean ResetPopulation = true;//是否重新设置代,在Start中有用
	static boolean NewGeneration = false;//是否是新的一代?
	static boolean InEvolution = false;//是否在进化模式中
	static boolean DrawNumbers = false;//是否用数字显示每个个体?
	static boolean WithSound = true;//是否要声音
	static boolean InLog = true;//是否在纪录中
	static boolean LimitedRanges = false;

	static EfloyGeneration FloyGen;//当前种群的基因型
	static Vector HistoryData;//历史纪录

	static int NF;
	static int REVDIST;
	static float ACC;
	static float ACCTOMID;	
	static float MAXSPEED;
	static float BOUNCESPEED;
	static float V0;
	static float KICK;
	static long SLEEP;
	static int MARGIN;
	static int NUMNB;
	static int TYPE;
	static String COLOR;


	static AudioClip joy;
	static AudioClip beep;
    static Image picture;
    static AppletContext appcontext;
	static EfloyLog deb;
	static EfloyLog helpwin;
	static EfloyLog rulewin;
	static Dimension ScreenSize;


	public void init() {
		int i;
		Float temp;
		String s;

		ButtonFont = new Font("TimesRoman",Font.PLAIN,12);

	    deb = new EfloyLog("Log Window");
	    //deb.toBack();
		deb.hide();
        deb.showMsg("");
	    deb.reshape(50,40,540,380);
		deb.start();

	    helpwin = new EfloyLog("Help Window");
	    //helpwin.toBack();
		helpwin.hide();
        helpwin.showMsg("");
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		ScreenSize = d;
		int w = d.width;
		int h = d.height;
		helpwin.reshape(0,0,w-2,h-2);
	    //helpwin.reshape(10,40,620,380);
		CreateHelp(helpwin);

	    rulewin = new EfloyLog("Behavioral Rules of eFloys");
	    //rulewin.toBack();
		rulewin.hide();
        rulewin.showMsg("");
		rulewin.reshape(2,2,w-2,h-2);
		CreateRules(rulewin);

		setLayout(new BorderLayout());

		Panel bot = new Panel();
		Start = new Button(" Restart ");
		Scramble = new Button(" Scramble ");
		Stranger = new Button(" Insert Stranger ");
		Breed = new Button(" Breed ");
		Evolution = new Button(" Start Evolution ");
		Quit = new Button(" Quit ");

		bot.setFont(ButtonFont);

		bot.add(Start);
		bot.add(Scramble);
		bot.add(Breed);
		bot.add(Evolution);
		bot.add(Stranger);
		add("South",bot);

		Numbers = new Button("Show Numbers");
		Sound = new Button("Turn Sound Off");
		Slower = new Button("Move Slower");
		Faster = new Button("Move Faster");
		Pause = new Button("Pause");
		Ranges = new Button("Limited Ranges");
		


		Help = new Button("Show Help");
		Log = new Button("Show Log");
		Predefined = new Button("Predefined");
		Control = new Button("Edit Properties");
		Info = new Button("Show Info");
		Rules = new Button("Show Rules");
		

		Panel west = new Panel();
		west.setLayout(new GridLayout(6,1));
		west.setFont(ButtonFont);

		west.add(Numbers);
		west.add(Sound);
		west.add(Slower);
		west.add(Faster);
		west.add(Pause);
		west.add(Ranges);

		add("West",west);


		Panel east = new Panel();
		east.setLayout(new GridLayout(6,1));
		east.setFont(ButtonFont);

		east.add(Help);
		east.add(Rules);
		east.add(Log);
		east.add(Predefined);
		east.add(Info);
		east.add(Control);

		add("East",east);


		Panel north = new Panel();
		Font BoldFont = new Font("TimesRoman",Font.BOLD,12);
		north.setFont(BoldFont);
		//north.setBackground(Color.blue);
		//north.setForeground(Color.white);
		//MainTitle = new Label(".                 The eFloys Aquarium                 .");
		MainTitle = new Label("The eFloys Aquarium");
		MainTitle.setForeground(Color.black);
		north.add(MainTitle);
		add("North",north);

		appcontext = getAppletContext();
		picture = getImage(getCodeBase(),"Efloys1.gif");
		joy = getAudioClip(getCodeBase(), "joy.au");
		beep = getAudioClip(getCodeBase(), "Beep.au");


		canvas = new EfloyCanvas(Color.red);
		//canvas = new EfloyCanvas(picture);
		add("Center",canvas);
		canvas.repaint();
		gra = canvas.GetGra();

		center = new Panel();
		center.setLayout(new GridLayout(2,2));
		center.add(new Button("B1"));
		center.add(new Button("B2"));
		center.add(new Button("B3"));
		center.add(new Button("B4"));
		//add("Center",center);
		//center.repaint();
		//remove(center);

		center1 = new Panel();
		center1.setLayout(new GridLayout(2,2));
		center1.add(new Button("B1a"));
		center1.add(new Button("B2a"));
		center1.add(new Button("B3a"));
		center1.add(new Button("B4a"));
		//add("Center",center1);

		HistoryData = new Vector();

		//Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		//resize(d.width-20,250);
		resize(620,250);

		CurrentBehavior = 0;
		First = true;

	}

	public void destroy() {

		deb.dispose();
		helpwin.dispose();
		rulewin.dispose();

	}


	public void start()
	{
		if (runner == null)
		{
			runner= new Thread(this);
			runner.start();
			running = true;
			CurrentStep = 0;
			CurrentTotalStep = 0;
			CurrentRandom = 0;
			CurrentGeneration = 0;
		}
	}
	
	public void stop()
	{
		if (runner!=null)
		{
			runner.stop();
			runner=null;
			running = false;
		}
	}


	public void run() {
		//不断运行这段程序,如果是第一次运行那么先进行初始化

		if (First) {
			InitParamsExtended();
			GetParameters();
			Restart(false);
			randemize();
			First = false;
		}
	
		//不停的运行下去,如果杀死了异己既产生了新的一代或者跌代的次数太大则也重新产生一代
		while (true) {

			if (First) Restart(false);
			First = false;

			CurrentStep++;
			CurrentTotalStep++;
		 	if (Math.random()< (double) KICK) randemize();
			if (((InEvolution) && (NewGeneration)) || ((InEvolution) &&(CurrentStep > 5000))) {
				StartDate = new Date();
				deb.showMsg("New Generation #"+(CurrentGeneration+1)+" started at "+StartDate.toLocaleString());
				CreateNewGeneration();
			}
		 	canvas.repaint();
		 	try { Thread.sleep(SLEEP);}
		 	catch (InterruptedException e) {
				showStatus("Thread.sleep InterruptedException "+e.toString());
			}
		}
		


	}


	float GetOneParameter(String name, float def) {

		String s;
		Float temp;
		float FloatPar;

		s=getParameter(name);
		if (s==null)
			FloatPar=def;
		else {
			temp = new Float(s);
			FloatPar = temp.floatValue();
		}

		return FloatPar;

	}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -