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

📄 efloycommand.java

📁 一个swarm的小程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//
//	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.awt.*;

final class EfloyCommand extends Frame {

    Button Environ;
    Button Fixed;
    Button Variable;
	Button Secondary;
	Button Test;
	Button Reset;
	Button Update;
	Button Updateall;
	Button Quit;
	
	Panel ButtonPanel;
	Panel TitlePanel;
	Panel InputPanel;
	Panel EnvironPanel;
	Panel FixedPanel;
	Panel VariablePanel;
	Panel SecondaryPanel;
	Panel TestPanel;

	Label TitleLabel;
	CardLayout cardLayout;

	Label EnvLabels[];
	Choice[] EnvChoices;
	Label[] EnvDummiesA;
	Label[] EnvDummiesB;
	Label[] EnvDummiesC;

	Label FixLabels[];
	Choice[] FixChoices;
	Label[] FixDummiesA;
	Label[] FixDummiesB;
	Label[] FixDummiesC;

	Label VarLabels[];
	Choice[] VarChoices;
	Label[] VarDummiesA;
	Label[] VarDummiesB;
	Label[] VarDummiesC;

	Label SecLabels[];
	Choice[] SecChoices;
	Label[] SecDummiesA;
	Label[] SecDummiesB;
	Label[] SecDummiesC;

	Choice Floynum;
	Label FloynumLabel;

    Efloy floys[];
	int SelectedFloy;
	Font ButtonFont;

	int CurNF;
	int CurPop;
    
    public EfloyCommand (Efloy f[]) {
        super("Control Center");
        floys = f;
		float fi = 0f;
		int i,j;
        
		ButtonFont = new Font("TimesRoman",Font.PLAIN,12);
		setBackground(Color.white);
		setForeground(Color.black);
		InitControls();

        Environ = new Button("Environment");
        Fixed = new Button("Fixed Traits");
        Variable = new Button("Primary Traits");
		Secondary = new Button("Secondary Traits");
		//Test = new Button("Test");
		//Reset = new Button("Reset");
		Update = new Button("Update");
		Updateall = new Button("Update All");
		Quit = new Button("Quit");

		Floynum = new Choice();
		FillFloyNumbers(Floynum);

        ButtonPanel = new Panel();
        ButtonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        ButtonPanel.add(Environ);
        ButtonPanel.add(Fixed);
        ButtonPanel.add(Variable);
		ButtonPanel.add(Secondary);
		//ButtonPanel.add(Test);
		//ButtonPanel.add(Reset);
		ButtonPanel.add(Update);
		ButtonPanel.add(Updateall);
		ButtonPanel.add(Quit);
		ButtonPanel.setFont(ButtonFont);
        this.add("South", ButtonPanel);

        TitlePanel = new Panel();
        TitlePanel.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 15));
		TitleLabel = new Label("环境的属性");
		TitlePanel.add(TitleLabel);
		FloynumLabel = new Label("Floy编号:");
		TitlePanel.add(FloynumLabel);
		TitlePanel.add(Floynum);
		this.add("North",TitlePanel);
		Font TitleFont = new Font("TimesRoman",Font.BOLD,14);
		TitlePanel.setFont(TitleFont);
		TitlePanel.setForeground(Color.blue);
		UpdateTitle("              环境属性           ", true);
		//UpdateTitle("Environmental Properties", true);
		//UpdateTitle("Environmental Properties", false);
		//UpdateTitle("Primary Traits", true);
		//cardLayout.show(InputPanel,"Variable");

        InputPanel = new Panel();
		cardLayout = new CardLayout(0,0);
		InputPanel.setLayout(cardLayout);

		EnvironPanel = new Panel();
		InitEnviron();
		InputPanel.add("Environ",EnvironPanel);

		FixedPanel = new Panel();
		InitFixed();
		InputPanel.add("Fixed",FixedPanel);

		VariablePanel = new Panel();
		InitVariable();
		InputPanel.add("Variable",VariablePanel);

		SecondaryPanel = new Panel();
		InitSecondary();
		InputPanel.add("Secondary",SecondaryPanel);

		TestPanel = new Panel();
        TestPanel.setLayout(new GridLayout(floys.length+2,9));
		InputPanel.add("Test",TestPanel);

		this.add("Center",InputPanel);
		//Efloys.appcontext.showStatus("Loading Control Screen");

		this.pack();
		this.hide();
		UpdateTitle("环境属性", false);

	}


	private void InitControls() {

		int i, j;

		EnvLabels = new Label[11];
		EnvChoices = new Choice[11];
		EnvDummiesA = new Label[11];
		EnvDummiesB = new Label[11];
		EnvDummiesC = new Label[11];

		FixLabels = new Label[11];
		FixChoices = new Choice[11];
		FixDummiesA = new Label[11];
		FixDummiesB = new Label[11];
		FixDummiesC = new Label[11];

		VarLabels = new Label[11];
		VarChoices = new Choice[11];
		VarDummiesA = new Label[11];
		VarDummiesB = new Label[11];
		VarDummiesC = new Label[11];

		SecLabels = new Label[11];
		SecChoices = new Choice[11];
		SecDummiesA = new Label[11];
		SecDummiesB = new Label[11];
		SecDummiesC = new Label[11];

		Font font = new Font("TimesRoman",Font.BOLD,12);

		for (i=0;i<11;i++) {
			EnvLabels[i] = new Label("");
			EnvChoices[i] = new Choice();
			EnvDummiesA[i] = new Label("");
			EnvDummiesB[i] = new Label("");
			EnvDummiesC[i] = new Label("");
			EnvLabels[i].setFont(font);

			FixLabels[i] = new Label("");
			FixChoices[i] = new Choice();
			FixDummiesA[i] = new Label("");
			FixDummiesB[i] = new Label("");
			FixDummiesC[i] = new Label("");
			FixLabels[i].setFont(font);

			VarLabels[i] = new Label("");
			VarChoices[i] = new Choice();
			VarDummiesA[i] = new Label("");
			VarDummiesB[i] = new Label("");
			VarDummiesC[i] = new Label("");
			VarLabels[i].setFont(font);

			SecLabels[i] = new Label("");
			SecChoices[i] = new Choice();
			SecDummiesA[i] = new Label("");
			SecDummiesB[i] = new Label("");
			SecDummiesC[i] = new Label("");
			SecLabels[i].setFont(font);

		}

	}
	

	private void InitInputPanel(Panel panel, Label[] labels, Choice[] choices, Label[] dummiesa, Label[] dummiesb, Label[] dummiesc, int num) {

		int i;

		GridBagLayout layout = new GridBagLayout();
		panel.setLayout(layout);

		GridBagConstraints constraints = new GridBagConstraints();

		constraints.weightx = 1.0;
		constraints.weighty = 1.0;
		constraints.fill = GridBagConstraints.BOTH;

		for (i=0;i<11;i++) {
 			constraints.gridwidth = 1;
			layout.setConstraints(dummiesa[i], constraints);
			panel.add(dummiesa[i]);

			constraints.gridwidth = 2;
			layout.setConstraints(labels[i], constraints);
			panel.add(labels[i]);

			constraints.gridwidth = 1;
			if (i > num-1) {
				layout.setConstraints(dummiesc[i], constraints);
				panel.add(dummiesc[i]);
			}
			else {
				layout.setConstraints(choices[i], constraints);
				panel.add(choices[i]);
			}


			constraints.gridwidth = GridBagConstraints.REMAINDER;
			layout.setConstraints(dummiesb[i], constraints);
			panel.add(dummiesb[i]);

		}

		panel.resize(panel.preferredSize());

	}



	private void InitVariable() {

		int i;

		VarLabels[0].setText("最大速度");
		VarLabels[1].setText("反弹速度");
		VarLabels[2].setText("加速度");
		VarLabels[3].setText("中心吸引力");
		VarLabels[4].setText("碰撞距离");

		InitChoice(VarChoices[0],Efloys.params, 0);
		InitChoice(VarChoices[1],Efloys.params, 1);
		InitChoice(VarChoices[2],Efloys.params, 2);
		InitChoice(VarChoices[3],Efloys.params, 4);
		InitChoice(VarChoices[4],Efloys.params, 8);

		InitInputPanel(VariablePanel, VarLabels, VarChoices, VarDummiesA, VarDummiesB, VarDummiesC, 5);

	}

	

	private void InitSecondary() {

		int i;

		SecLabels[0].setText("远距离的反映 (本地居民对本地居民");
		SecLabels[1].setText("远距离的反映(本地居民对入侵者)");
		SecLabels[2].setText("远距离的反映(陌生人对本土居民)");
		SecLabels[3].setText("碰撞反映 (本地居民对本地居民)");
		SecLabels[4].setText("碰撞反映(本地对入侵者)");
		SecLabels[5].setText("碰撞反映(入侵者对本地)");

		InitChoice(SecChoices[0],Efloys.params, 5);
		InitChoice(SecChoices[1],Efloys.params, 6);
		InitChoice(SecChoices[2],Efloys.params, 7);
		InitChoice(SecChoices[3],Efloys.params, 9);
		InitChoice(SecChoices[4],Efloys.params, 10);
		InitChoice(SecChoices[5],Efloys.params, 11);

		InitInputPanel(SecondaryPanel, SecLabels, SecChoices, SecDummiesA, SecDummiesB, SecDummiesC, 6);

	}



	private void InitEnviron() {

		int i;

		CurNF = Efloys.NF;
		CurPop = floys[0].PopulationSize;

		EnvLabels[0].setText("每代的个体数");
		EnvLabels[1].setText("移动减缓属性");
		EnvLabels[2].setText("能量值的权重");
		EnvLabels[3].setText("安全度的权重");
		EnvLabels[4].setText("合作度的权重");
		EnvLabels[5].setText("最大的能量变化值");
		EnvLabels[6].setText("最大的安全度变化值");
		EnvLabels[7].setText("最大的合作度变化值");
		EnvLabels[8].setText("幸存的百分比");
		EnvLabels[9].setText("自由邻居重组概率");
		EnvLabels[10].setText("入侵者的生命值");


		InitChoice(EnvChoices[0],Efloys.envpars, 12);
		InitChoice(EnvChoices[1],Efloys.envpars, 3);
		InitChoice(EnvChoices[2],Efloys.envpars, 5);
		InitChoice(EnvChoices[3],Efloys.envpars, 6);
		InitChoice(EnvChoices[4],Efloys.envpars, 7);
		InitChoice(EnvChoices[5],Efloys.envpars, 9);
		InitChoice(EnvChoices[6],Efloys.envpars, 10);
		InitChoice(EnvChoices[7],Efloys.envpars, 11);
		InitChoice(EnvChoices[8],Efloys.envpars, 8);
		InitChoice(EnvChoices[9],Efloys.envpars, 13);
		InitChoice(EnvChoices[10],Efloys.envpars, 14);

		InitInputPanel(EnvironPanel, EnvLabels, EnvChoices, EnvDummiesA, EnvDummiesB, EnvDummiesC, 11);

	}


	private void InitFixed() {

		int i;

		FixLabels[0].setText("类型");
		FixLabels[1].setText("颜色");
		FixLabels[2].setText("邻居数目");
		FixLabels[3].setText("变异因子");
		FixLabels[4].setText("交叉因子");
		FixLabels[5].setText("能量水平");
		FixLabels[6].setText("安全度水平");
		FixLabels[7].setText("合作度水平");
		FixLabels[8].setText("适应度");

		FillTypes(FixChoices[0]);
		FillColors(FixChoices[1]);
		InitChoice(FixChoices[2],Efloys.fixpars, 5);
		InitChoice(FixChoices[3],Efloys.fixpars, 6);
		InitChoice(FixChoices[4],Efloys.fixpars, 7);
		InitChoice(FixChoices[5],Efloys.fixpars, 8);
		InitChoice(FixChoices[6],Efloys.fixpars, 9);
		InitChoice(FixChoices[7],Efloys.fixpars, 10);
		InitChoice(FixChoices[8],Efloys.fixpars, 11);
		
		InitInputPanel(FixedPanel, FixLabels, FixChoices, FixDummiesA, FixDummiesB, FixDummiesC, 9);

	}


	private void InitChoice(Choice choice, EfloyParam param[], int Parnum) {

		float fi;

        for (int i = 0; i <= param[Parnum].nsteps; i++) {
			fi = (float) (param[Parnum].min + i*param[Parnum].step);
            choice.addItem(Float.toString(fi));
		}
        choice.select(Float.toString((float) param[Parnum].value));

	}



	private void FillColors(Choice choice) {

		choice.addItem("BLACK");
		choice.addItem("BLUE");
		choice.addItem("CYAN");
		choice.addItem("DARKGRAY");
		choice.addItem("GRAY");
		choice.addItem("GREEN");
		choice.addItem("LIGHTGRAY");
		choice.addItem("MAGENTA");
        choice.addItem("ORANGE");
		choice.addItem("PINK");
		choice.addItem("RED");
		choice.addItem("WHITE");
		choice.addItem("YELLOW");

		choice.select(floys[floys.length-1].GetColorName());
		//choice.select("GREEN");

	}


	private void FillTypes(Choice choice) {

		choice.addItem("Local");
		choice.addItem("Stranger");

		choice.select(floys[floys.length-1].GetTypeName());
		//choice.select("Local");

⌨️ 快捷键说明

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