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

//图形界面

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);  个随机数

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) {

}

 

}

运行结果为:

 

 

分形之概率学下的green tree的更多相关文章

  1. 1.为什么要从古典概率入门概率学《zobol的考研概率论教程》

    在入门概率论与数理统计这门课中,刚开始我们都会从古典概率开始学习,为什么要选择它呢?这是因为古典概率作为一种将生活中的事情简化为有限种情况,并假设它们的发生可能差不多的手段,十分的好用且简洁. 这里我 ...

  2. Linux下无法执行tree命令问题

    Linux下不能使用tree命令,是因为没有安装命令, 执行下面代码就行了 yum install tree -y

  3. Mac 下如何使用 Tree 命令

    方式一 Mac 系统下默认是不带这条命令的,执行下面这条命令也可以打印出树状结构. find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g' 不想每 ...

  4. Mac下命令行tree生成文件树

    不像Windows,Mac环境本身是没有tree命令的,但可以后天呐~ 1.下载文件包并将其放在系统目录下(本人存放路径为/Users/) https://homebrew.bintray.com/b ...

  5. linux下怎么用tree命令以树形结构显示文件目录结构?

    tree命令以树状图列出文件目录结构.不过某些Linux上(Centos 6.4)没有tree命令,本文将介绍安装方法. 常用参数: ? 1 2 3 4 5 6 tree -d 只显示目录.   tr ...

  6. OO方式下,ALV TREE和ALV GRID的不同之处

    作为大部分报表程序的基础,ALV GRID差不多是每个ABAP开发者必须了解和掌握的内容,因此网上也不乏相关资料,而ALV TREE的应用相对较少,中文资料也就比较少见了.实际上,ALV TREE和A ...

  7. linux下的device tree

    在我个人的理解,device tree就是描述硬件设备的,目前有什么配置,把这些配置信息告诉linux内核,让内核去识别,增强了内核的通用性,不用因为平台不同而每次都要编译新内核了. 配置device ...

  8. 保护眼睛,开启浏览器的夜间模式 顺便学下!important的作用

    打开笔记本程序,复制以下代码 *{background-image: none !important; background: none !important; background:#333333 ...

  9. 下拉框 tree 基于 EasyUi

    public string DataTableToJson() { string dataJson = ""; string temp = ""; string ...

随机推荐

  1. 火狐浏览器如何js关闭窗口的几种解决方法

    今天在项目上有一个页面要求在几秒后自动关闭,想着还比较简单,用window.close()就可以了,但是用IE/谷歌/火狐浏览器试了一下,发现IE可以,谷歌用网上的兼容方法也可以实现,但是火狐这里卡住 ...

  2. UIViewController相关知识

    title: UIViewController 相关知识date: 2015-12-13 11:50categories: IOS tags: UIViewController 小小程序猿我的博客:h ...

  3. android 双缓存机制

    废话不多说,直接贴代码! 所谓的双缓存,第一就是缓存在内存里面,第二就是缓存在SD卡里面,当你需要加载数据时,先去内存缓存中查找,如果没有再去SD卡中查找,并且用户可以自选使用哪种缓存! 缓存内存和缓 ...

  4. IOS开发基础知识--碎片10

    1:如何给表格单元列增加选择时的背影效果 if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCel ...

  5. Android自定义ViewGroup,实现自动换行

    学习<Android开发艺术探索>中自定义ViewGroup章节 自定义ViewGroup总结的知识点 一.自定义ViewGroup中,onMeasure理解 onMeasure(int ...

  6. Android Studio 恢复小窗口停靠模式(Docked Mode)

    安卓studio在使用小窗口时,如果我们点击取消了窗口的docked mode模式,窗口就会变成,你一旦触发窗口以外的区域,窗口就会龟缩回去.此时,如果你想要恢复回原来的docked mode的话,具 ...

  7. Git 常见的命令操作

    克隆                git clone git地址 查看分支         git branch 查看git库状态  git status 切换分支         git  che ...

  8. Xcode出现( linker command failed with exit code 1)错误总结

    这种问题,通常出现在添加第三方库文件或者多人开发时. 这种问题一般是找不到文件而导致的链接错误. 我们可以从如下几个方面着手排查. 先可以再试试一下几个方法:  1,看看是不是有新添加的文件跟之前文件 ...

  9. Socket.IO聊天室~简单实用

    小编心语:大家过完圣诞准备迎元旦吧~小编在这里预祝大家元旦快乐!!这一次要分享的东西小编也不是很懂啊,总之小编把它拿出来是觉地比较稀奇,而且程序也没有那么难,是一个比较简单的程序,大家可以多多试试~ ...

  10. 利用split

    java.lang.string.splitsplit 方法将一个字符串分割为子字符串,然后将结果作为字符串数组返回.stringObj.split([separator,[limit]])strin ...