📄 l.java~1~
字号:
package l;import java.awt.*;import java.awt.event.*;import java.applet.*;import java.util.Vector;import java.util.Date;/** * Title: L * Description: * Copyright: Copyright (c) 2003 * Company: J & K Studio * @author keatskk@sina.com * @version 1.0 */public class L extends Applet implements Runnable{ Thread runner; boolean running; boolean start; boolean bStop=false; boolean isStandalone = false; int pStartX =200; //起始点 int pStartY =50; double direction_init =0; // 作画时的初始方向 double direction; double lengthF =6; // 步距 double rotation =-60; // 给定转角 int StartDepth =6; //画图深度 int initDepth; int ruleNumber =2; //规则数 String sStart; //公理;即所要画的字符串 String sRule[][]; //规则 doublePoint a, b; // 画线段所需的两点 Graphics g; Button btnStep = new Button(); Choice choice1 = new Choice(); Button btnConfirm = new Button(); Label lblStep = new Label(); Label label1 = new Label(); /**Get a parameter value*/ public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } /**Construct the applet*/ public L() { } /**Initialize the applet*/ public void init() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } /**Component initialization*/ private void jbInit() throws Exception { resize(400,450); setLayout(new BorderLayout()); this.setBackground(Color.black); Panel panPara = new Panel(); panPara.setBackground(Color.lightGray); btnStep.setLabel("单步迭代"); choice1.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(ItemEvent e) { choice1_itemStateChanged(e); } }); lblStep.setText(" "); label1.setForeground(Color.blue); label1.setText("keatskk"); add("South",panPara); btnConfirm.setLabel("设置参数"); panPara.add(btnConfirm,null); panPara.add(choice1,null); panPara.add(btnStep, null); panPara.add(lblStep, null); panPara.add(label1, null); choice1.addItem("斜草"); choice1.addItem("三角形"); choice1.addItem("树伞"); choice1.addItem("有花蕾的植物"); choice1.addItem("枝"); choice1.addItem("海岸线"); choice1.addItem("蒲公英"); choice1.addItem("灌木丛"); choice1.addItem("棕榈"); choice1.addItem("开花的草"); choice1.addItem("斜枝"); choice1.addItem("杨柳"); choice1.addItem("Juliet集"); choice1.addItem("砖墙"); choice1.addItem("砖砌X形"); choice1.addItem("三角绕三角"); choice1.addItem("一笔迷宫"); choice1.addItem("树"); choice1.addItem("对称的树"); sRule = new String [10][2]; sStart = "X"; sRule[0][0]="X"; sRule[0][1]="--FXF++FXF++FXF--"; sRule[1][0]="F"; sRule[1][1]="FF"; reinit(); } public void reinit(){ initDepth = StartDepth; start=true; running=false; // update(this.getGraphics()); } /**Destroy the applet*/ public void destroy() { if (runner!=null){ runner.stop(); runner=null; running = false; } } /**Get Applet information*/ public String getAppletInfo() { return "Applet Information"; } /**Get parameter info*/ public String[][] getParameterInfo() { return null; } /**Main method*/ public static void main(String[] args) { L applet = new L(); applet.isStandalone = true; Frame frame; frame = new Frame() { protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } public synchronized void setTitle(String title) { super.setTitle(title); enableEvents(AWTEvent.WINDOW_EVENT_MASK); } }; frame.setTitle("Applet Frame"); frame.add(applet, BorderLayout.CENTER); applet.init(); applet.start(); frame.setSize(400,470); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2); frame.setVisible(true); } public void paint(Graphics g) { if(!running){ start=true; return; }else { return; } /* g.setColor(new Color(110,170,60)); // 定义画笔的颜色 a = new doublePoint(pStartX,pStartY); // 起始点 direction = direction_init; // 起始的方向 turtleGraphic(g, sStart, StartDepth); // 核心程序*/ } public void turtleGraphic(Graphics g, String instruction, int depth) { if (depth==0) return; //深度为0即表示可以开始画了 if(bStop)return; depth -= 1; //每递归一次深度减一 Vector aPoint = new Vector();//用堆栈记录[]中的内容 Vector aDirection = new Vector(); String sDirection; int i; int 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)){ turtleGraphic(g, sRule[j][1], depth); //若找到公理符合规则则调用规则 if(bStop){ aPoint.removeAllElements(); aDirection.removeAllElements(); return; } break; } } if (c=='F') { // 如果深度达到所设定的深度时即画出线段 if (depth==0||j==ruleNumber) { 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)); //a=(doublePoint)(aPoint.get(aPoint.size()-1)); sDirection=(String)(aDirection.elementAt(aDirection.size()-1)); //sDirection=(String)(aDirection.get(aDirection.size()-1)); direction=Double.valueOf(sDirection).doubleValue(); aPoint.removeElementAt(aPoint.size()-1); aDirection.removeElementAt(aDirection.size()-1); } } } void choice1_itemStateChanged(ItemEvent e) { btnStep.setLabel("单步迭代"); lblStep.setText(" "); bStop=true; int k=choice1.getSelectedIndex(); if(k==0){ //斜草 pStartX =340; pStartY =20; direction_init =60; lengthF =3.3; rotation =-3; StartDepth =7; ruleNumber =2; sStart = "G"; sRule[0][0]="G"; sRule[0][1]="GFX[+++++GFG][-----GFG]"; sRule[1][0]="X"; sRule[1][1]="F-XF"; } else if(k==1){ pStartX =200; pStartY =50; direction_init =0; lengthF =6; rotation =-60; StartDepth =6; ruleNumber =2; sStart = "X"; sRule[0][0]="X"; sRule[0][1]="--FXF++FXF++FXF--"; sRule[1][0]="F"; sRule[1][1]="FF"; //reinit(); } else if(k==2){ pStartX =200; pStartY =20; direction_init =90; lengthF =4.5; rotation =30; StartDepth =5; ruleNumber =1; sStart = "F"; sRule[0][0]="F"; sRule[0][1]="F[+F[+F][-F]F][-F[+F][-F]F]F[+F][-F]F"; //reinit(); } else if(k==3){ pStartX =200; pStartY =210; direction_init =-90; lengthF =9.5; rotation =-18;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -