• 分形之概率学下的green tree


         今天做的是分形之随机概率,可以和以前做的那个抛色子的做法非常相似,抛色子是用随机点数控制图形,今天做的树叶图形只是用概率的做法去控制图形而已,做法是如出一辙的:

    //图形界面

    package tree0618;

    import java.awt.Color;

    import java.awt.FlowLayout;

    import java.awt.Graphics;

    import java.awt.Graphics2D;

    import javax.swing.JFrame;

    public class treeJF extends JFrame {

    public static void main(String[] args) {

    treeJF fl=new treeJF(

      fl.initUI();

    }

    public void initUI(){

    getContentPane().setBackground(Color.BLACK);

    setTitle("treeFlower");

    setSize(600,500);

    setDefaultCloseOperation(3);

    setLocationRelativeTo(null);

    setLayout(new FlowLayout());

    setVisible(true);

    Graphics g = getGraphics();

    ((Graphics2D) g).setColor(Color.green);

    treeListener tl = new treeListener();

    addMouseListener(tl);

    tl.setGraphics(g);

    }

    }

    //具体的实现

    package tree0618;

    import java.awt.Color;

    import java.awt.Graphics;

    import java.awt.Graphics2D;

    import java.awt.event.MouseEvent;

    import java.awt.event.MouseListener;

    import java.util.Random;

     

    public class treeListener implements MouseListener {

    float x=0, y=0;

    float a, b, c, d, e = 0, f;

    private Graphics g;

    public Color color;

    public int i;

    public float x1, y1;

    public void setGraphics(Graphics gra) {

    g = (Graphics2D) gra;

    }

    public void mouseClicked(MouseEvent e) {

    Random rand = new Random();

    for (int i = 0; i < 10000; i++) {

    int s = rand.nextInt(100);  //表示总共有100个随机数

    if (s < 10) {  //0-9,表示概率是十分之一

    a = 0;b = 0;c = 0;d = (float) 0.16;f = 0;

    } else if (s < 18) {  //表示的是10-17,概率为0.18

    a = (float) 0.5;b = (float) -0.26;c = (float) 0.23;d = (float) 0.22;f = (float) 1.6;

    } else if (s < 26) {  //19-25,表示概率为0.07

    a = (float) -0.15;b = (float) 0.28;c = (float) 0.26;d = (float) 0.24;f = (float) 0.14;

    } else {  //其他的则为26-100,概率是0.75

    a = (float) 0.35;b = (float) 0.04;c = (float) -0.04;d = (float) 0.85;f = (float) 1.6;

    }

    //图形的运算公式

    x1 = a * x + b * y;

    y1 = c * x + d * y + f;

    x = x1;

    y = y1;

    //g.drawLine((int)(x1*(-50)+300),(int)(y1*(-50)+400),(int)(x1*(-50)+500),(int)(y1*(-50)+500));这种写法是错误的,注意:随机数表示的是无数个点,所以不是画线是画点

     

    g.drawLine((int)(x1*(-50)+300),(int)(y1*(-50)+500),(int)(x1*(-50)+300),(int)(y1*(-50)+500));

    }

    x=0;

    y=0;

    }

    //对比抛色子:if (i < 4) {

    // 画四个点

    t = e.getX();

    h = e.getY();

    g.drawLine(t, h, t, h);

    // 分别存储四个点

    if (i == 0) {

    t1 = t;

    h1 = h;

    }

     

    else if (i == 1) {

    t2 = t;

    h2 = h;

    }

     

    else if (i == 2) {

    t3 = t;

    h3 = h;

    }

     

    else if (i == 3) {

    t4 = t;

    h4 = h;

    }

    i++;

    } else {

    Random rand = new Random();

    for (j = 0; j < 200000; j++) {

    int s = rand.nextInt(3);

    if (s == 0) {// 如果选中的是A点

    t4 = (t1 + t4) / 2;

    h4 = (h1 + h4) / 2;

    } else if (s == 1) {// 如果选中的是B点

    t4 = (t2 + t4) / 2;

    h4 = (h2 + h4) / 2;

    } else {// 如果选中的是C点

    t4 = (t3 + t4) / 2;

    h4 = (h3 + h4) / 2;

    }

    g.drawLine(t4, h4, t4, h4);

    i = 0;

    }

    }

    // System.out.println(m+"   "+x);

     

    public void mousePressed(MouseEvent e) {

     

    }

     

    public void mouseReleased(MouseEvent e) {

     

    }

     

    public void mouseEntered(MouseEvent e) {

    }

     

    public void mouseExited(MouseEvent e) {

     

    }

     

    public static void main(String[] args) {

    }

     

    }

    运行结果为:

     

     

  • 相关阅读:
    postgresql远程连接不上
    Linux安装配置
    exportfs rv报错
    PostgreSQL14中预定义角色pg_write_all_data和pg_read_all_data
    Windows Server 2016上安装SQL Server 2019 AlwaysOn Availability Groups(一)
    C#学习教程:LINQ to Entities无法识别方法’System.String Split(Char )’方法
    Git 配置 sshkey 免密登录
    RedmiBook pro 15 笔记本 Manjaro 下网卡(8852AE)驱动安装记录
    vue 中 computed 、created 、mounted 的先后顺序
    .net开源框架开源类库(整理)
  • 原文地址:https://www.cnblogs.com/java-7/p/5608870.html
Copyright © 2020-2023  润新知