分形是其组成部分以某种方式与整体相似的形,亦即:如果一个图形其组成部分以某种方式与整体相似,则称该图形为分形。可以依靠递归实现。所谓递归就是函数自己调用自己
package Fractal;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
/*
* 定义简单画板类
*author daidan
*/
public class DrawingBorder extends JFrame {
/**
* 程序的入口,主函数
*/
private static Graphics g;
public double x1=200;
public double y1=200;
public double x2,y2;
public static void main(String[] args) {
// 实例化一个简单画板界面类的对象
DrawingBorder db = new DrawingBorder();
// 调用初始化界面的方法
db.initUI();
}
//初始化界面的方法
public void initUI( ){
this.setTitle("简单画板");
this.setSize(600,500);
this.setDefaultCloseOperation(3);
this.setLayout(new FlowLayout());
JButton jbfx1=new JButton("分形1");
JButton jbfx2=new JButton("分形2");
JButton jbfx3=new JButton("分形3");
JButton jbfx4=new JButton("分形4");
this.add(jbfx4);
this.add(jbfx1);
this.add(jbfx2);
this.add(jbfx3);
/*
* 匿名内部类
*
*/
ActionListener al=new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("分形1")){
for(int i=0;i<50000;i++){
g.fillOval((int)(x1*100+300), (int)(y1*100+300), 2, 2);
x2=-0.656*Math.sin(1.40*y1)-Math.sin(1.56*x1);
y2=1.40*Math.cos(1.40*x1)-Math.cos(1.56*y1);
x1=x2;
y1=y2;}
}elseif(e.getActionCommand().equals("分形2"))
{for(int i=0;i<50000;i++){
Color c=new Color(250,0,0);
g.setColor(c);
g.fillOval((int)(x1*100+300), (int)(y1*100+300), 2, 2);
x2=Math.sin(-2*y1)-Math.cos(-2*x1);
y2=Math.sin(-1.2*x1)-Math.cos(2*y1);
x1=x2;
y1=y2;
}
} elseif(e.getActionCommand().equals("分形3")){
x1=0;
y1=0;
Color c=new Color(250,0,0);
g.setColor(c);
for(int i=0;i<500000;i++){
double x = x1+300;
double y = y1+300;
g.drawLine((int)x, (int)y, (int)x, (int)y);
x2=y1-Math.signum(x1)*Math.sqrt(Math.abs(4*x1-60));
y2=1-x1;
x1=x2;
y1=y2;
System.out.println(x);
}
}elseif(e.getActionCommand().equals("分形4")){
x1=0.0234;
y1=0.012345;
Color c=new Color(250,20,20);
g.setColor(c);
for(int i=0;i<500000;i++){
double x = x1*1000+300;
double y = y1*1000+300;
System.out.println(x+" " +y);
g.drawLine((int)x, (int)y, (int)x, (int)y);
double a=2;
x2=x1*Math.cos(a)-(y1-Math.pow(x1, 2))*Math.sin(a);
y2=x1*Math.sin(a)+(y1-Math.pow(x1, 2))*Math.cos(a);
x1=x2;
y1=y2;
}
}
};
jbfx1.addActionListener(al);
jbfx2.addActionListener(al);
jbfx3.addActionListener(al);
jbfx4.addActionListener(al);
this.setVisible(true);
g = this.getGraphics();
}
}
/************************************************************/
主界面
import java.awt.Graphics;
import javax.swing.JFrame;
import cn.test0320.XueHua;
///新建一个类继承了JFrame
public class XieEr extends JFrame{
/**
* main
*/
public static void main(String[] args) {
XieEr jf=new XieEr();
jf.init();
}
public int l=300,i=5;
private Graphics g;
public void init(){
this.setSize(800,700);
this.setTitle("雪花曲线");
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
//设置可见后获取画布
this.setVisible(true);
g=this.getGraphics();
}
//将画雪花曲线定义在重绘中
public void paint(Graphics g){
super.paint(g);
XueHua xh=new XueHua(g);
xh.init(l, i,100,200);
}
/**************************************************************/
//画雪花曲线的类
/****************************************************************/
import java.awt.Graphics;
public class XueHua {
/**
*xuehua类
*/
private int l,i;
//角度v
private Graphics g;
//一个三的N次方的数组
public int[] a=new int[]{1,3,9,27,81,243,729,2187};
//构造函数,传入画板
public XueHua(Graphics g){
this.g=g;
}
public void init(int l,int i,int x,int y){
this.l=l;
this.i=i;
//将雪花曲线的三角形的三边分别进行递归
this.xuehua(x, y, 0, 1);
this.xuehua(x+l, y, 2*Math.PI/3, 1);
this.xuehua(x+l/2, y+l*Math.sin(Math.PI/3), -2*Math.PI/3, 1);
}
//将利用这个函数进行递归,x,y起始点坐标,z角度 ,n用来判断到递归底层画曲线的长度
public void xuehua(double x,double y,double z,int n){
if (n<a[i]){
int i;
//第一次递归
xuehua(x,y,z,3*n);
//计算第二个点坐标
double x2=x+l*Math.cos(z)/(3*n),y2=y+l*Math.sin(z)/(3*n);
//改变角度
z=z-Math.PI/3;
xuehua(x2,y2,z,3*n);
//计算第三个点坐标
double x3=x2+l*Math.cos(z)/(3*n),y3=y2+l*Math.sin(z)/(3*n);
//改变角度
z=z+2*Math.PI/3;
xuehua(x3,y3,z,3*n);
double x4=x3-l*Math.cos(Math.PI-z)/(3*n),y4=y3+l*Math.sin(z)/(3*n);
z=z-Math.PI/3;
xuehua(x4,y4,z,3*n);
}else{
//若已经到了底层则画线
g.drawLine((int)x, (int)y, (int)(x+l*Math.cos(z)/(n)), (int)(y+l*Math.sin(z)/(n)));
}
}
}
/*************************************************/