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

📄 lsystem.java

📁 植物仿真
💻 JAVA
字号:
package classes;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.util.Date;
import java.applet.Applet;
public class Lsystem extends Applet implements Runnable,ItemListener,ActionListener//主窗口
{
	Thread runner;
	boolean running;
	boolean start;

	int Count=0;
	int R=0,G=0,B=0;

	boolean colorLock=true;
	int exampleChoice=1;//控制"图例演示"中单选按钮状态
	int startX=340;//起始点
	int startY=20;
	double initDirection=60;//作画时的初始方向
	double direction;//中间变量
	double lengthF=3.3;//每步步长
	double rotation=-3;//作图中给定的转角
	int startDepth=7;//画图迭代次数
	int Depth=0;
	int ruleNumber=2;//规则数
	String sStart;//公理;即所要画的字符串
	String sRule[][];//规则数组
	doublePoint a, b;//画线段的两点,由类定义

	Graphics g;
	Choice choice1,choice2,choice3;
	Button step;
	Panel p;

	public void init()
	{
		this.setLayout(new BorderLayout());
		sRule=new String [6][2];
		//初始化为"斜草"规则
		sStart = "G";
		sRule[0][0]="G";
		sRule[0][1]="GFX[+++++GFG][-----GFG]";
		sRule[1][0]="X";
		sRule[1][1]="F-XF";

		choice1=new Choice();
		choice2=new Choice();
		choice3=new Choice();

		choice1.add("前景色");
		choice1.add("背景色");
		choice1.add("植物色");

		choice2.add("开始绘画");
		choice2.add("图例演示");
		choice2.add("参数控制");
		choice2.add("清除画面");

		choice3.add("关于L系统");
		choice3.add("关于程序");

		choice1.addItemListener(this);
		choice2.addItemListener(this);
		choice3.addItemListener(this);

		step=new Button("单步迭代");
		step.addActionListener(this);

		p=new Panel();
		p.setLayout(new FlowLayout());

		p.add(new Label("颜色"));
		p.add(choice1);
		p.add(new Label("画图"));
		p.add(choice2);
		p.add(new Label("帮助"));
		p.add(choice3);
		p.add(step);

		add(p,"South");

		this.setSize(517,550);

		reinit();
	}
	//从新初始化
	public void reinit()
	{
		Depth=startDepth;
		start=true;
		running=false;
	}
    //获取背景色
	public Color getBackColor()
	{
		Color c=this.getBackground();
		return c;
	}
	//获取前景色
	public Color getForeColor()
	{
		Color c=this.getForeground();
		return c;
	}
	//设置背景色
	public void setBackColor()
	{
		Color c=new Color(R,G,B);
		this.setBackground(c);
	}
	//设置前景色
	public void setForeColor()
	{
		Color c=new Color(R,G,B);
		this.setForeground(c);
	}

	public void destroy()
	{
		if(runner!=null)
		{
			runner=null;
			running=false;
		}
	}
	public void start()
	{
		if (runner==null)
		{
			runner=new Thread(this);
			runner.start();
		}
	}

	public void stop()
	{
		if (runner!=null)
		{
			runner=null;
			start=false;
			running=false;
		}
	}
	public void run()
	{
		while(true)
		{
			if(start)
			{
				start=false;
				g=this.getGraphics();
				g.clearRect(0,0,517,550);
				a=new doublePoint(startX,startY); // 起始点
				direction=initDirection;        // 起始的方向
				HuiHua(g, sStart, startDepth);
				g.finalize();
				running=false;
			}
		}

	}
	public void paint(Graphics g)
	{
		g=this.getGraphics();
		if(!running)
		{
			start=true;
			return;
		}
		else
		{
			return;
		}
	}

	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource()==step)
		{
			g=this.getGraphics();
			g.clearRect(0,0,560,560);
			a=new doublePoint(startX,startY); // 起始点
			direction=initDirection;// 起始的方向
			Count=0;
			if(Depth==startDepth)
				Depth=0;
			HuiHua(g,sStart,(++Depth)%(startDepth+1));
		}
	}

	public void itemStateChanged(ItemEvent ie)//各个子菜单按钮的时间处理
	{
		if(ie.getItemSelectable()==choice1)
		{
			if(choice1.getSelectedIndex()==0)//“前景色”的处理
			{
				colorLock=false;
				g=this.getGraphics();
				g.clearRect(0,0,517,550);
				Color_set fore_cs=new Color_set(this,"前景色",0);
				fore_cs.setVisible(true);
			}
			if(choice1.getSelectedIndex()==1)//“背景色”的处理
			{
				g=this.getGraphics();
				g.clearRect(0,0,517,550);
				Color_set back_cs=new Color_set(this,"背景色",1);
				back_cs.setVisible(true);
			}
			if(choice1.getSelectedIndex()==2)//“植物色”的处理
			{
				colorLock=true;
			}
		}
		if(ie.getItemSelectable()==choice2)
		{
			if(choice2.getSelectedIndex()==0)//“开始绘画”的处理
			 {
			 g=this.getGraphics();
			 a=new doublePoint(startX,startY); // 起始点
			 direction=initDirection;// 起始的方向
			 Count=0;
			 HuiHua(g,sStart,startDepth);
			 }
			if(choice2.getSelectedIndex()==1)//“图例演示”的处理
			{
				g=getGraphics();
				g.clearRect(0,0,517,550);
				Lexample ex=new Lexample(this,"图例演示");
				ex.setVisible(true);
			}
			if(choice2.getSelectedIndex()==2)//“参数控制”的处理
			{
				g=this.getGraphics();
				g.clearRect(0,0,517,550);
				Lcontrol lc=new Lcontrol(this,"参数控制");
				lc.setVisible(true);
			}
			if(choice2.getSelectedIndex()==3)//“清除画面”的处理
			{
				g=this.getGraphics();
				g.clearRect(0,0,517,550);
			}
		}
		if(ie.getItemSelectable()==choice3)
		{
			if(choice3.getSelectedIndex()==0)//“关于L系统”的处理
			{
				g=this.getGraphics();
				g.clearRect(0,0,517,550);
				AboutSystem as=new AboutSystem("关于L系统",1);
				as.setVisible(true);
			}
			if(choice3.getSelectedIndex()==1)//“关于程序”的处理
			{
				g=this.getGraphics();
				g.clearRect(0,0,517,550);
				AboutPrograme ap=new AboutPrograme("关于程序");
				ap.setVisible(true);
			}
		}
	}
    // 核心程序
	public void HuiHua(Graphics g,String instruction, int depth)
	{
		if (depth==0)
			return;   //深度为0即表示可以开始画了
		depth -= 1;
		//每递归一次深度减一
		Vector aPoint = new Vector();//用堆栈记录[]中的内容
		Vector aDirection = new Vector();
		String sDirection;

		int i,j;
		char c;

		for (i=0;i<instruction.length();i++)
		{
			c = instruction.charAt(i);  //获取公理中的字符
			// 开始递归
			for(j=0;j<ruleNumber;j++)
			{
				if (c==sRule[j][0].charAt(0))
				{
					HuiHua(g,sRule[j][1],depth); //若找到公理符合规则则调用规则
					break;
				}
			}

			if (c=='F')
			{
				// 如果深度达到所设定的深度时即画出线段
				if (depth==0||j==ruleNumber)
				{
					if(colorLock)
					{
						if(Count>75)
							Count=0;
						g=this.getGraphics();
						g.setColor(new Color(0,180+(75-Count++)%75,0));
					}
					double rad=2*Math.PI*direction/360;// 角度转换
					double p=lengthF * Math.cos(rad);
					double q=lengthF * Math.sin(rad);
					b = new doublePoint(a.x+p, a.y+q);
					g.drawLine((int)(a.x), (int)(400-a.y), (int)(b.x) ,(int)(400-b.y));
					a = b; // 前一线段的终点为后一线段的起始点
				}
			}
			else if (c=='+') direction += rotation; //逆时针转角度
			else if (c=='-') direction -= rotation; //顺时针转角度
			else if (c=='[')
			{  //入栈
				aPoint.addElement(a);
				sDirection = String.valueOf(direction);
				aDirection.addElement(sDirection);
			}
			else if (c==']')
			{  //出栈
				a=(doublePoint)(aPoint.elementAt(aPoint.size()-1));
				sDirection=(String)(aDirection.elementAt(aDirection.size()-1));
				direction=Double.valueOf(sDirection).doubleValue();
				aPoint.removeElementAt(aPoint.size()-1);
				aDirection.removeElementAt(aDirection.size()-1);
			}

		}

	}
	class doublePoint
	{ //为保存双精度的点而新建一个类
		double x;
		double y;
		doublePoint(double x1,double y1)
		{x=x1;y=y1;}
	}
}

⌨️ 快捷键说明

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