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

📄 treepanel.java

📁 一个用JAVA编辑的L-System中的生长树案例
💻 JAVA
字号:
/**
 * 用LS 文法生成的树 
 * 支持单一规则
 */
import java.awt.Point;
import java.util.Stack;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
public class TreePanel extends FractalPanel {
 //树的很多形态
 /**
  * F[-F]F[+F]F    25 向上  
  * FF+[+F-F-F]-[-F+F+F] 20 蓬松
  * F[+F]F[-F+F] 25度  刺
  */
    private int alpha;
    private String rule;
    private int time;
    private String picString;
    private int currentAngle = 90; // 当前绘图方向
    private Stack (State) s = new Stack (State) ();
    private Point currentPoint = new Point(300,500); // 当前画笔在的点
    private int length;
    // 字符串参数带F
    public TreePanel(String rule, int alpha, int time, int length) {
        this.rule = rule;
        this.alpha = alpha;
        this.time = time;
        this.length = length;
        this.picString = rule;
        initPic();
    }
    private void initPic() {
        for (int i = 0; i < time; i++) {
            LS();
        }
    }
    // 根据规则生成字符串
    private void LS() {
        picString = picString.replaceAll("F", rule);
    }
    public void draw() {
        //初始化
        currentAngle = 90;
        s = new Stack {State}();
        currentPoint = new Point(300, 650); 
        // 对picString进行分析,根据字符串指示信息画图
        int length = picString.length();
        for (int offset = 0; offset < length; offset++) {
            switch (picString.charAt(offset)) {
            case 'F':
                forwardLine();
                break;
            case 'f':
                forwardWithOutLine();
               break;
            case '+':
                antiRotate(alpha);
                break;
            case '-':
                rotate(alpha);
               break;
            case '[':
                saveState();
                break;
            case ']':
                loadState();
                break;
            }
        }
    }
    /***************************************************************************
     * 画图辅助函数 坐标系说明: (0,0)为左上角的点,angle是以水平向右为0度
     **************************************************************************/
    /**
     * 根据递归层数向前画线
     */
    private void forwardLine() {
        Graphics g = getGraphics();
        int x = currentPoint.x;
        int y = currentPoint.y;
        int endX = (int)(x + length*Math.cos(currentAngle/180.0 * Math.PI));
        int endY = (int)(y - length*Math.sin(currentAngle/180.0 * Math.PI));
        g.drawLine(x,y,endX,endY);
        currentPoint.setLocation(endX,endY);
    }
    /**
     * 根据递归层树前进不画线
     */
    private void forwardWithOutLine() {
        int endX = (int)(currentPoint.x + length*Math.cos(currentAngle/180.0 * Math.PI));
        int endY = (int)(currentPoint.y - length*Math.sin(currentAngle/180.0 * Math.PI));
        currentPoint.setLocation(endX,endY);
    }
    /**
     * 顺时针旋转
     * 
     * @param rotateAngle
     *            旋转角度
     */
    private void rotate(int rotateAngle) {
        currentAngle -= rotateAngle;
    }
    /**
     * 逆时针旋转
     * 
     * @param rotateAngle
     *            旋转角度
     */
    private void antiRotate(int rotateAngle) {
        currentAngle += rotateAngle;
    }
    private void saveState() {
//        State state = new State(currentPoint, currentAngle);
        Point p = new Point();
        p.setLocation(currentPoint.x,currentPoint.y);
        State state = new State(p,currentAngle);
        s.push(state);
    }
    private void loadState() {
        State state = s.pop();
        currentPoint = state.position;
        currentAngle = state.angle;
    }
    /**
     * javaBean 存放当前画笔信息
     */
    private class State {
        int angle;
        Point position;
        State(Point currentPoint, int angle) {
            position = currentPoint;
            this.angle = angle;
        }
    }
}

⌨️ 快捷键说明

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