---
title: "Something about Fractal(分形)"
author: "danlei94"
date: "2014-11-03T19:02:09Z"
tags: []
categories: ["编程语言"]
slug: "something-about-fractal"
draft: false
lastmod: "2014-11-03T19:02:09Z"
id: 2152012
url: "https://danlei94.iteye.com/blog/2152012"
---

Something about Fractal(分形)

 

最初看到分形两个字的时候，感觉这个词是如此的陌生又熟悉，这根形状一定有关，是不是一些有规律的形状呢？带着这个问题我总结了我在认识和学习分形的过程中所学到的一些知识。

PS：学习分形的时间很短，所以不是很深入，只是结合Java swing中的画笔graphics进行画图，望大家轻喷！

 

 

一、分形的含义：是用来描述对称性自然物的数学方法，主要描述工具是电脑图形,具有强烈的美感震撼力。

 

 

二、怎么画分形图形:

就我的理解，分形就是一些创意图形，但是本质上是有规律可循的图形。比如厉害的科学家们已经总结出分形图形的坐标之间的关系，而我们现在要做的是利用巨人探讨出来的数学关系式去绘制出分形图形来。

基于Java swing 界面：

1.创建窗体JFrame

2.设置窗体的属性

3.加鼠标监听器方法addMouseListener，它将实现在窗体上任意点击一下，就可以显示出分形图形来

4.写一个监听器类来实现MouseListener接口。重写其中的释放方法，在里面绘制分形图形

5.画分形图形，需要传入画笔Graphics g，用g的方法drawline来实现（当然必须要结合for循环或者递归）

6.最终分形就可以完成了

 

以下附上我写的一些分形图形的源代码

 package com.hnu.danny1011;

import java.awt.Color;  
import java.awt.Graphics;

import javax.swing.JButton;  
import javax.swing.JFrame;

public class Fenxing extends JFrame{

 /\**  
  * @param args  
  \*/  
 public static void main(String\[] args) {  
  Fenxing fx=new Fenxing();  
  fx.initUI();

 }

 public void initUI()  
 {  
  this.setTitle("分形递归");  
  this.setSize(700, 700);  
  this.setLocationRelativeTo(null);  
  //this.setResizable(false);  
  this.setDefaultCloseOperation(3);  
  this.setLayout(new java.awt.FlowLayout());  
  this.setVisible(true);  
    
  javax.swing.JButton jb1=new JButton();  
  javax.swing.JButton jb2=new JButton();  
  javax.swing.JButton jb3=new JButton();  
  javax.swing.JButton jb4=new JButton();  
    
  jb1.setText("香蕉");  
  jb2.setText("环形");  
  jb3.setText("细胞");  
  jb4.setText("花环");  
    
  this.add(jb1);  
  this.add(jb2);  
  this.add(jb3);  
  this.add(jb4);  
    
  

  //this.getContentPane().setBackground(Color.black);  
    
 }  
 //Graphics g=this.getGraphics();  
   
public void paint(Graphics g)  
{  
 super.paint(g);  
   
 drawfenxing(0,0,1000);  
}

 public void drawfenxing(double x,double y, int p)  
 {  
  Graphics g=this.getGraphics();  
    
//  draw1(g,x,y,p);  
//  draw2(g,x,y,p);  
  draw3(g,x,y,p);  
   
 }  
   
 public void draw1(Graphics g,double x,double y,int p)  
 {  
  //double x=1,y=1;  
     double a=-2,b=-2,c=1,d=2,temp;   
     for(int i=0;i&lt;100;i++){  
      temp=x;  
   x=Math.cos(a\*x)-Math.sin(b\*y);  
   y=Math.cos(c\*temp)-Math.sin(d\*y);  
   //System.out.println(x+"   "+y);  
   g.setColor(Color.YELLOW);  
   g.drawLine((int)(x\*200+300),(int) (y\*200+300),(int)( x\*200+300),(int)( y\*200+300));  
     }  
   if(p&gt;0)  
    drawfenxing(x,y,p-1);  
   //this.getGraphics().drawLine(50, 50, 100, 100);  
 }  
 public void draw2(Graphics g,double x,double y,int p)  
 {  
  //double x=1,y=1;  
     double a=1.4,b=1.56,c=1.40,d=-6.56,temp;   
     for(int i=0;i&lt;300;i++){  
      temp=x;  
   x=d\*Math.sin(a\*x)-Math.sin(b\*y);  
   y=c\*Math.cos(a\*temp)+Math.cos(b\*y);  
   //System.out.println(x+"   "+y);  
   g.setColor(Color.darkGray);  
   g.drawLine((int)(x\*50+350),(int) (y\*50+350),(int)( x\*50+350),(int)( y\*50+350));  
     }  
   if(p&gt;0)  
    drawfenxing(x,y,p-1);  
   //this.getGraphics().drawLine(50, 50, 100, 100);  
 }  
 public void draw3(Graphics g,double x,double y,int p)  
 {  
  //double x=1,y=1;  
   double a=0.4,b=1,c=0,temp;   
 // double a=1,b=4,c=60,temp;  
     for(int i=0;i&lt;400;i++){  
      temp=x;  
   x=y-Math.signum(x)\*Math.sqrt(Math.abs(b\*x-c));  
   y=a-temp;  
   //System.out.println(x+"   "+y);  
   g.setColor(Color.darkGray);  
   g.drawLine((int)(x\*100+350),(int) (y\*100+350),(int)( x\*100+350),(int)( y\*100+350));  
     }  
   if(p&gt;0)  
    drawfenxing(x,y,p-1);  
   //this.getGraphics().drawLine(50, 50, 100, 100);  
 }  
 public void draw4(Graphics g,double x,double y,int p)  
 {  
  //double x=1,y=1;  
 //    double a=0.4,b=1,c=0,temp;   
  double a=1,b=4,c=60,temp;  
     for(int i=0;i&lt;400;i++){  
      temp=x;  
   x=y-Math.signum(x)\*Math.sqrt(Math.abs(b\*x-c));  
   y=a-temp;  
   //System.out.println(x+"   "+y);  
   g.setColor(Color.darkGray);  
   g.drawLine((int)(x+350),(int) (y+350),(int)( x+350),(int)( y+350));  
     }  
   if(p&gt;0)  
    drawfenxing(x,y,p-1);  
   //this.getGraphics().drawLine(50, 50, 100, 100);  
 }  
}  
注：以上代码，画分形是直接在paint重绘中画的。两种方法均可~

 

 

&lt;!--EndFragment--&gt;