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

📄 lexample.java

📁 植物仿真
💻 JAVA
字号:
package classes;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Lexample extends JFrame implements ActionListener,ItemListener
{
	JButton ok,cancel;
	JPanel p_1,p_2;
	JRadioButton box_1,box_2,box_3,box_4,box_5,box_6,box_7;
	JRadioButton box_8,box_9,box_10,box_11,box_12;
	ButtonGroup group;
	JTextArea text;
	int choice;
	Lsystem localsystem;

	Lexample(Lsystem lsystem,String s)
	{
		super(s);
		localsystem=lsystem;

		Container con=this.getContentPane();
		ok=new JButton("确定");
		cancel=new JButton("取消");
		
		group=new ButtonGroup();
		
		p_1=new JPanel();
		p_2=new JPanel();
		
		con.setLayout(new BorderLayout());
		p_1.setLayout(new GridLayout(13,1));
		p_2.setLayout(new GridLayout(1,5));
		
		text=new JTextArea();
		//文本区初始化为"斜草"规则
		text.setText("");
		text.setBackground(Color.lightGray);
		text.append("sStart =G\n");
		text.append("sRule[0][0]=G\n");
		text.append("sRule[0][1]=GFX[+++++GFG][-----GFG]\n");
		text.append("sRule[1][0]=X\n");
		text.append("sRule[1][1]=F-XF");
		text.setEditable(false);        

		box_1=new JRadioButton("斜草",false);
		group.add(box_1);
		box_2=new JRadioButton("树伞",false);
		group.add(box_2);
		box_3=new JRadioButton("有花蕾的植物",false);
		group.add(box_3);
		box_4=new JRadioButton("枝",false);
		group.add(box_4);
		box_5=new JRadioButton("蒲公英",false);
		group.add(box_5);
		box_6=new JRadioButton("灌木丛",false);
		group.add(box_6);
		box_7=new JRadioButton("棕榈",false);
		group.add(box_7);
		box_8=new JRadioButton("开花的草",false);
		group.add(box_8);
		box_9=new JRadioButton("斜枝",false);
		group.add(box_9);
		box_10=new JRadioButton("杨柳",false);
		group.add(box_10);
		box_11=new JRadioButton("树",false);
		group.add(box_11);
		box_12=new JRadioButton("对称的树",false);
		group.add(box_12);
		
		choice=localsystem.exampleChoice;
		//由主窗体获取当前是哪个图形规则,并将这一规则的
		//图形名称设置为选种状态
		switch(choice)
		{
			case 1: box_1.setSelected(true);
				break;
			case 2: box_2.setSelected(true);
				break;
			case 3: box_3.setSelected(true);
				break;
			case 4: box_4.setSelected(true);
				break;
			case 5: box_5.setSelected(true);
				break;
			case 6: box_6.setSelected(true);
				break;
			case 7: box_7.setSelected(true);
				break;
			case 8: box_8.setSelected(true);
				break;
			case 9: box_9.setSelected(true);
				break;
			case 10: box_10.setSelected(true);
				break;
			case 11: box_11.setSelected(true);
				break;
			case 12: box_12.setSelected(true);
				break;
		}

		p_1.add(box_1);
		p_1.add(box_2);
		p_1.add(box_3);
		p_1.add(box_4);
		p_1.add(box_5);
		p_1.add(box_6);
		p_1.add(box_7);
		p_1.add(box_8);
		p_1.add(box_9);
		p_1.add(box_10);
		p_1.add(box_11);
		p_1.add(box_12);
   		
		p_2.add(new Label());
		p_2.add(ok);
		p_2.add(new Label());
		p_2.add(cancel);
		p_2.add(new Label());
		
		con.add(p_1,"West");
		con.add(text,"Center");
		con.add(p_2,"South");
		
		box_1.addItemListener(this);
		box_2.addItemListener(this);
		box_3.addItemListener(this);
		box_4.addItemListener(this);
		box_5.addItemListener(this);
		box_6.addItemListener(this);
		box_7.addItemListener(this);
		box_8.addItemListener(this);
		box_9.addItemListener(this);
		box_10.addItemListener(this);
		box_11.addItemListener(this);
		box_12.addItemListener(this);
		
		ok.addActionListener(this);
		cancel.addActionListener(this);
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent we)
			{setVisible(false);}
		});
		
		setBackground(Color.lightGray);
		setSize(320,400);
		setResizable(false);
		//获取默认显示器的尺寸
		Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
		//设置窗体显示位置
		this.setLocation((d.width-this.getSize().width)/2,(d.height-this.getSize().width)/2);
		setVisible(false);
	}
	
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource()==ok)
		{
			if(choice==1)//"斜草"
			{ 
				localsystem.startX=340;
				localsystem.startY=20;
				localsystem.initDirection=60;
				localsystem.lengthF=3.3;
				localsystem.rotation=-3;
				localsystem.startDepth=7;
				localsystem.ruleNumber=2;
				localsystem.sStart="G";
				localsystem.sRule[0][0]="G";
				localsystem.sRule[0][1]="GFX[+++++GFG][-----GFG]";
				localsystem.sRule[1][0]="X";
				localsystem.sRule[1][1]="F-XF";
				localsystem.exampleChoice=1;
			}
			if(choice==2)//"树伞"
			{
				localsystem.startX=240;
				localsystem.startY=10;
				localsystem.initDirection=90;
				localsystem.lengthF=4.5;
				localsystem.rotation=30;
				localsystem.startDepth=5;
				localsystem.ruleNumber=1;
				localsystem.sStart="F";
				localsystem.sRule[0][0]="F";
				localsystem.sRule[0][1]="F[+F[+F][-F]F][-F[+F][-F]F]F[+F][-F]F";
				localsystem.exampleChoice=2;
			}
			if(choice==3)//"有花蕾的植物"
			{
				localsystem.startX=240;
				localsystem.startY=210;
				localsystem.initDirection=-90;
				localsystem.lengthF=9.5;
				localsystem.rotation=-18;
				localsystem.startDepth=13;
				localsystem.ruleNumber=4;
				localsystem.sStart="K";
				localsystem.sRule[0][0]="S";
				localsystem.sRule[0][1]="[+++G][---H]FFS";
				localsystem.sRule[1][0]="G";
				localsystem.sRule[1][1]="+G[-FH]F";
				localsystem.sRule[2][0]="H";
				localsystem.sRule[2][1]="-H[+FG]F";
				localsystem.sRule[3][0]="K";
				localsystem.sRule[3][1]="FSF";
				localsystem.exampleChoice=3;
			}
			if(choice==4)//"枝"
			{ 
				localsystem.startX=240;
				localsystem.startY=5;
				localsystem.initDirection=90;
				localsystem.lengthF=1.5;
				localsystem.rotation=-25.7341;
				localsystem.startDepth=6;
				localsystem.ruleNumber=1;
				localsystem.sStart="F";
				localsystem.sRule[0][0]="F";
				localsystem.sRule[0][1]="F[+F]F[-F]F";
				localsystem.exampleChoice=4;
			}
			if(choice==5)//"蒲公英"
			{
				localsystem.startX=240;
				localsystem.startY=10;
				localsystem.initDirection=90;
				localsystem.lengthF=0.37;
				localsystem.rotation=30;
				localsystem.startDepth=10;
				localsystem.ruleNumber=2;
				localsystem.sStart="Y";
				localsystem.sRule[0][0]="X";
				localsystem.sRule[0][1]="X[-FFF][+FFF]FX";
				localsystem.sRule[1][0]="Y";
				localsystem.sRule[1][1]="YFX[+Y][-Y]";
				localsystem.exampleChoice=5;
			}
			if(choice==6)//"灌木丛"
			{
				localsystem.startX=300;
				localsystem.startY=20;
				localsystem.initDirection=90;
				localsystem.lengthF=3.5;
				localsystem.rotation=-30;
				localsystem.startDepth=6;
				localsystem.ruleNumber=1;
				localsystem.sStart="F";
				localsystem.sRule[0][0]="F";
				localsystem.sRule[0][1]="FF-[-F+F+F]+[+F-F-F]";
				localsystem.exampleChoice=6;
			}
			if(choice==7)//"棕榈"
			{
				localsystem.startX=240;
				localsystem.startY=20;
				localsystem.initDirection=90;
				localsystem.lengthF=5.6;
				localsystem.rotation=-18;
				localsystem.startDepth=12;
				localsystem.ruleNumber=5;
				localsystem.sStart="SLFFF";
				localsystem.sRule[0][0]="S";
				localsystem.sRule[0][1]="[+++H][---G]TS";
				localsystem.sRule[1][0]="G";
				localsystem.sRule[1][1]="+H[-G]L";
				localsystem.sRule[2][0]="H";
				localsystem.sRule[2][1]="-G[+H]L";
				localsystem.sRule[3][0]="T";
				localsystem.sRule[3][1]="TL";
				localsystem.sRule[4][0]="L";
				localsystem.sRule[4][1]="[-FFF][+FFF]F";
				localsystem.exampleChoice=7;
			}
			if(choice==8)//"开花的草"
			{
				localsystem.startX=240;
				localsystem.startY=10;
				localsystem.initDirection=90;
				localsystem.lengthF=3;
				localsystem.rotation=-30;
				localsystem.startDepth=8;
				localsystem.ruleNumber=2;
				localsystem.sStart="G";
				localsystem.sRule[0][0]="G";
				localsystem.sRule[0][1]="[+FGF][-FGF]XG";
				localsystem.sRule[1][0]="X";
				localsystem.sRule[1][1]="XFX";
				localsystem.exampleChoice=8;
			}
			if(choice==9)//"斜枝"
			{
				localsystem.startX=370;
				localsystem.startY=30;
				localsystem.initDirection=90;
				localsystem.lengthF=2;
				localsystem.rotation=-1.2;
				localsystem.startDepth=6;
				localsystem.ruleNumber=1;
				localsystem.sStart="F";
				localsystem.sRule[0][0]="F";
				localsystem.sRule[0][1]="F[+++++++++++++++++++++++++F]-F[-------------------------F]F";
				localsystem.exampleChoice=9;
			}
			if(choice==10)//"杨柳"
			{
				localsystem.startX=200;
				localsystem.startY =0;
				localsystem.initDirection=90;
				localsystem.lengthF=7;
				localsystem.rotation=-22.5;
				localsystem.startDepth=5;
				localsystem.ruleNumber=1;
				localsystem.sStart="F";
				localsystem.sRule[0][0]="F";
				localsystem.sRule[0][1]="FF+[+F-F-F]-[-F+F+F]";
				localsystem.exampleChoice=10;
			}
			if(choice==11)//"树"
			{
				localsystem.startX=260;
				localsystem.startY=10;
				localsystem.initDirection=90;
				localsystem.lengthF=0.35;
				localsystem.rotation=30;
				localsystem.startDepth=10;
				localsystem.ruleNumber=2;
				localsystem.sStart="X";
				localsystem.sRule[0][0]="X";
				localsystem.sRule[0][1]="F[+X]F[-X]+X";
				localsystem.sRule[1][0]="F";
				localsystem.sRule[1][1]="FF";
				localsystem.exampleChoice=11;
			}
			if(choice==12)//"对称的树"
			{
				localsystem.startX=250;
				localsystem.startY=10;
				localsystem.initDirection=90;
				localsystem.lengthF=0.35;
				localsystem.rotation=30;
				localsystem.startDepth=10;
				localsystem.ruleNumber=2;
				localsystem.sStart ="X";
				localsystem.sRule[0][0]="X";
				localsystem.sRule[0][1]="F[+X][-X]FX";
				localsystem.sRule[1][0]="F";
				localsystem.sRule[1][1]="FF";
				localsystem.exampleChoice=12;
			}
			this.setVisible(false);
			localsystem.reinit();
		}

		else if(ae.getSource()==cancel)
			this.setVisible(false);
	}
	
	public void itemStateChanged(ItemEvent ie)
	{

		if(ie.getItemSelectable()==box_2)
		{//"树伞"
			choice=2;
			text.setText("");
			text.append("sStart =F\n");
			text.append("sRule[0][0]=F\n");
			text.append("sRule[0][1]=F[+F[+F][-F]F][-F[+\n");
			text.append("                       F][-F]F]F[+F][-F]F");
		}
		else if(ie.getItemSelectable()==box_3)
		{//"有花蕾的植物"
			choice=3;
			text.setText("");
			text.append("sStart= K\n");
			text.append("sRule[0][0]=S\n");
			text.append("sRule[0][1]=[+++G][---H]FFS\n");
			text.append("sRule[1][0]=G\n");
			text.append("sRule[1][1]=+G[-FH]F\n");
			text.append("sRule[2][0]=H\n");
			text.append("sRule[2][1]=-H[+FG]F\n");
			text.append("sRule[3][0]=K\n");
			text.append("sRule[3][1]=FSF");
		}
		else if(ie.getItemSelectable()==box_4)
		{//"枝"
			choice=4;
			text.setText("");
			text.append("sStart = F\n");
			text.append("sRule[0][0]=F\n");
			text.append("sRule[0][1]=F[+F]F[-F]F");
		}
		else if(ie.getItemSelectable()==box_5)
		{//"蒲公英"
			choice=5;
			text.setText("");
			text.append("sStart = Y\n");
			text.append("sRule[0][0]=X\n");
			text.append("sRule[0][1]=X[-FFF][+FFF]FX\n");
			text.append("sRule[1][0]=Y\n");
			text.append("sRule[1][1]=YFX[+Y][-Y]");
		}
		else if(ie.getItemSelectable()==box_6)
		{//"灌木丛"
			choice=6;
			text.setText("");
			text.append("sStart = F\n");
			text.append("sRule[0][0]=F\n");
			text.append("sRule[0][1]=FF-[-F+F+F]+[+F-F-F]");
		}
		else if(ie.getItemSelectable()==box_7)
		{//"棕榈"
			choice=7;
			text.setText("");
			text.append("sStart = SLFFF\n");
			text.append("sRule[0][0]=S\n");
			text.append("sRule[0][1]=[+++H][---G]TS\n");
			text.append("sRule[1][0]=G\n");
			text.append("sRule[1][1]=+H[-G]L\n");
			text.append("sRule[2][0]=H\n");
			text.append("sRule[2][1]=-G[+H]L\n");
			text.append("sRule[3][0]=T\n");
			text.append("sRule[3][1]=TL\n");
			text.append("sRule[4][0]=L\n");
			text.append("sRule[4][1]=[-FFF][+FFF]F");
		}
		else if(ie.getItemSelectable()==box_8)
		{//"开花的草"
			choice=8;
			text.setText("");
			text.append("sStart = G\n");
			text.append("sRule[0][0]=G\n");
			text.append("sRule[0][1]=[+FGF][-FGF]XG\n");
			text.append("sRule[1][0]=X\n");
			text.append("sRule[1][1]=XFX");
		}
		else if(ie.getItemSelectable()==box_9)
		{//"斜枝"
			choice=9;
			text.setText("");
			text.append("sStart = F\n");
			text.append("sRule[0][0]=F\n");
			text.append("sRule[0][1]=F[+++++++++++++++\n");
			text.append("                      ++++++++++F]-F[-----\n");
			text.append("                       --------------------F]F");
		}
		else if(ie.getItemSelectable()==box_10)
		{//"杨柳"
			choice=10;
			text.setText("");
			text.append("sStart = F\n");
			text.append("sRule[0][0]=F\n");
			text.append("sRule[0][1]=FF+[+F-F-F]-[-F+F+F]");
		}
		else if(ie.getItemSelectable()==box_11)
		{//"树"
			choice=11;
			text.setText("");
			text.append("sStart = X\n");
			text.append("sRule[0][0]=X\n");
			text.append("sRule[0][1]=F[+X]F[-X]+X\n");
			text.append("sRule[1][0]=F\n");
			text.append("sRule[1][1]=FF");
		}
		else if(ie.getItemSelectable()==box_12)
		{//"对称的树"
			choice=12;
			text.setText("");
			text.append("sStart = X\n");
			text.append("sRule[0][0]=X\n");
			text.append("sRule[0][1]=F[+X][-X]FX\n");
			text.append("sRule[1][0]=F\n");
			text.append("sRule[1][1]=FF");
		}
		else
		{
			choice=1;
			text.setText("");
			text.append("sStart =G\n");
			text.append("sRule[0][0]=G\n");
			text.append("sRule[0][1]=GFX[+++++GFG][-----GFG]\n");
			text.append("sRule[1][0]=X\n");
			text.append("sRule[1][1]=F-XF");
		}
	}
	
}
		
		

⌨️ 快捷键说明

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