首页 技术 正文
技术 2022年11月14日
0 收藏 652 点赞 3,644 浏览 2484 个字

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

//图形界面

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

 

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295