`
rzheny
  • 浏览: 9166 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

分形图片和代码展示

 
阅读更多

<div class="iteye-blog-content-contain" style="font-size: 14px"></div>

 

package rzy0705a;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DrawListener implements ActionListener {
    //谢宾斯基三角形
	public void draw(double x11,double y11,double x22,double y22,double x33,double y33,int con) {
		 con--;
		 if(con<1){return;}
		 int x1,x2,x3,y1,y2,y3;
		 x1=(int)x11;
		 x2=(int)x22;
		 x3=(int)x33;
		 y1=(int)y11;
		 y2=(int)y22;
		 y3=(int)y33;
		 g.drawLine(x1,y1,x2,y2);
		 g.drawLine(x2,y2,x3,y3);
		 g.drawLine(x1, y1, x3, y3);
		 double x13=(x11+x33)/2;
		 double x23=(x22+x33)/2;
		 double x12=(x11+x22)/2;
		 double y13=(y11+y33)/2;
		 double y23=(y22+y33)/2;
		 double y12=(y11+y22)/2;
		 draw(x11,y11,x12,y12,x13,y13,con);
		 draw(x12,y12,x22,y22,x23,y23,con);
		 draw(x13,y13,x23,y23,x33,y33,con);
	  }
	
	//谢宾斯基地毯
	public void drawF(double a,double b,double w,double h,int con){
		con--;
		if(con<1){return;}
		g.setColor(Color.green);
		g.fillRect((int)(a+w/9),(int)(b+h/9),(int)(w/9),(int)(h/9));
		g.fillRect((int)(a+w*4/9),(int)(b+h/9),(int)(w/9),(int)(h/9));
		g.fillRect((int)(a+w*7/9),(int)(b+h/9),(int)(w/9),(int)(h/9));
		g.fillRect((int)(a+w/9),(int)(b+h*4/9),(int)(w/9),(int)(h/9));
		
		g.fillRect((int)(a+w*7/9),(int)(b+h*4/9),(int)(w/9),(int)(h/9));
		g.fillRect((int)(a+w/9),(int)(b+h*7/9),(int)(w/9),(int)(h/9));
		g.fillRect((int)(a+w*4/9),(int)(b+h*7/9),(int)(w/9),(int)(h/9));
		g.fillRect((int)(a+w*7/9),(int)(b+h*7/9),(int)(w/9),(int)(h/9));
		drawF(a, b, w/3, h/

 

 3,con);
		drawF(a+w/3, b, w/3, h/3,con);
		drawF(a+2*w/3, b, w/3, h/3,con);
		drawF(a, b+h/3, w/3, h/3,con);
		drawF(a+2*w/3, b+h/3, w/3, h/3,con);
		drawF(a, b+2*h/3, w/3, h/3,con);
		drawF(a+w/3, b+2*h/3, w/3, h/3,con);
		drawF(a+2*w/3, b+2*h/3, w/3, h/3,con);
	}
	
	
	
	private Graphics g;
	public DrawListener(Graphics g){this.g=g;}
	//重写抽象方法
	public void actionPerformed(ActionEvent e) {
	    g.clearRect(0,0,2000,1000);
		//画分形
		if(e.getActionCommand()=="分形1")
	    {int a=-2,b=-2,d=2;
	    double c=-1.2;
	    double x=150,y=150;
		for(int i=0;i<50000;i++){
			double x2=Math.sin(a*y)-Math.cos(b*x);
			double y2=Math.sin(c*x)-Math.cos(d*y);
			int f=(int)(x2*100)+250;
			int h=(int)(y2*120)+300;
			Color cl=new Color(i%256,0,0);
			g.setColor(cl);
			g.drawLine(f,h,f,h);
			x=x2;
			y=y2;
		}}
		

 
		if(e.getActionCommand()=="分形2"){
			//double a=1.40,b=1.56,c=1.40,d=-6.56;
			double a=1.40,b=1.56,c=4,d=4;
			double x=0,y=0;
			for(int i=0;i<100000;i++){
				double x2=d*Math.sin(a*x)-Math.sin(b*y);
				double y2=c*Math.cos(a*x)+Math.cos(b*y);
				int f=(int)(x2*50)+850;
				int h=(int)(y2*50)+350;
				Color cl=new Color(0,i%256,i%256);
				g.setColor(cl);
				g.drawLine(f,h,f,h);
				x=x2;
				y=y2;}
		}
		

 
		if(e.getActionCommand().equals("嵌套矩形")){
		for(int i=0;i<35;i++){
			if(i>=15&&i<=25){                                   
			g.setColor(Color.green);
			g.drawRect(700-10*i,380-10*i,10+20*i, 10+20*i);}
			else if(i>25){g.setColor(Color.orange);
			g.drawRect(700-10*i,380-10*i,10+20*i, 10+20*i);}
			else{g.setColor(Color.red);
		    g.drawRect(700-10*i,380-10*i,10+20*i, 10+20*i);}
			}
		}
		

		if(e.getActionCommand().equals("嵌套圆")){
			for(int i=0;i<46;i++){if(i>=15&&i<=25){                                   
				g.setColor(Color.green);
				g.drawOval(700-10*i,380-10*i,10+20*i, 10+20*i);}
				else if(i<15){g.setColor(Color.red);
				  g.drawOval(700-10*i,380-10*i,10+20*i, 10+20*i);}
				else if(i>=25&&i<35){g.setColor(Color.blue);
				g.drawOval(700-10*i,380-10*i,10+20*i, 10+20*i);}
				else if(i>=35&&i<=45){g.setColor(Color.orange);
				g.drawOval(700-10*i,380-10*i,10+20*i, 10+20*i);}
			   }
			}
		

 
		if(e.getActionCommand().equals("谢宾斯基三角形")){
			draw(0,600,600,600,300,600-300*Math.sqrt(3),12);
			}
		

 
		if(e.getActionCommand().equals("分形3")){
			double x,y,nextx,nexty;
		    x=0;y=0;
			double a=1,b=4,c=60;
			for(int i=0;i<1000000;i++){
				nextx=y-Math.signum(x)*Math.sqrt(Math.abs(b*x-c));
				nexty=a-x;
				int m=(int)(x*1)+400;
				int n=(int)(y*1)+400;
				g.setColor(Color.blue);
				g.drawLine(m, n, m, n);
				x=nextx;
				y=nexty;
			}
		}
		

 
		if(e.getActionCommand().equals("分形4")){
			double x,y,nextx,nexty;
		    x=0;y=0;
			double a=0.4,b=1,c=0;
			for(int i=0;i<1000000;i++){
				nextx=y-Math.signum(x)*Math.sqrt(Math.abs(b*x-c));
				nexty=a-x;
				int m=(int)(x*100)+400;
				int n=(int)(y*100)+400;
				g.setColor(Color.blue);
				g.drawLine(m, n, m, n);
				x=nextx;
				y=nexty;
			}
		}
		

 
		if(e.getActionCommand().equals("分形5")){
			double x,y,nextx,nexty;
		    x=0;y=0;
			double a=-1,b=-2,c=-3;
			for(int i=0;i<1000000;i++){
				nextx=y-Math.signum(x)*Math.sqrt(Math.abs(b*x-c));
				nexty=a-x;
				int m=(int)(x*10)+400;
				int n=(int)(y*10)+400;
				g.setColor(Color.blue);
				g.drawLine(m, n, m, n);
				x=nextx;
				y=nexty;
			    }
		    }
		

 
			if(e.getActionCommand().equals("分形6")){
				//System.out.println("try");
				double x1,y1,nextx1,nexty1;
			    x1=0;y1=0;
				double a1=-1000,b1=0.1,c1=-10;
				for(int i=0;i<100000000;i++){
					nextx1=y1-Math.signum(x1)*Math.sqrt(Math.abs(b1*x1-c1));
					nexty1=a1-x1;
					int m=(int)(x1*30)+300;
					int n=(int)(y1*30)+300;
					g.setColor(Color.blue);
					g.drawLine(m, n, m, n);
					x1=nextx1;
					y1=nexty1;
				}
		}
		

 
		if(e.getActionCommand().equals("谢宾斯基矩形")){
			g.setColor(Color.black);
			g.fillRect(100,100,600,600);
			g.setColor(Color.green);
			g.fillRect(300,300,200,200);
			drawF(100,100,600,600,6);
		}
		

}

}

 

package rzy0705a;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.MouseMotionListener;

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

//定义类DrawBord,继承自JFrame
public class DrawBord extends JFrame{

	/**
	 * 入口主函数
	 */
	public static void main(String[] args) {
		//实例化DrawBord,创建对象
		DrawBord db=new DrawBord();
		//定义显示方法
		db.initUI();
		
		
	}
   public  Graphics g;
   public void initUI(){
	   //设置属性
	   this.setTitle("分形");
	   this.setSize(800, 800);
	   this.setLocationRelativeTo(null);
	   this.setDefaultCloseOperation(3);
	   this.setLayout(new FlowLayout());
	   this.setResizable(true);
       
	  
	  
	   
	   this.setVisible(true);
	   
	   JButton jb=new JButton("分形1");
       this.add(jb);
       JButton jb1=new JButton("分形2");
       this.add(jb1);
       JButton jb2=new JButton("嵌套矩形");
       this.add(jb2);
       JButton jb4=new JButton("嵌套圆");
       this.add(jb4);
       JButton jb5=new JButton("谢宾斯基三角形");
       this.add(jb5);
       JButton jb6=new JButton("分形3");
       this.add(jb6);
       JButton jb7=new JButton("分形4");
       this.add(jb7);
       JButton jb8=new JButton("分形5");
       this.add(jb8);
       JButton jb9=new JButton("分形6");
       this.add(jb9);
       JButton jb10=new JButton("谢宾斯基矩形");
       this.add(jb10);
     
       
       g=this.getGraphics();
	   //实例化对象
	   ActionListener al=new DrawListener(g);
	   jb.addActionListener(al);
	   jb1.addActionListener(al);
	   jb2.addActionListener(al);
	   jb4.addActionListener(al);
	   jb5.addActionListener(al);
	   jb6.addActionListener(al);
	   jb7.addActionListener(al);
	   jb8.addActionListener(al);
	   jb9.addActionListener(al);
	   jb10.addActionListener(al);
	 
	   
	   MouseMotionListener m=new MouseListen(g);
	   this.addMouseMotionListener(m);
	
	   
   }

}

 x上述·

  • 大小: 70.8 KB
  • 大小: 147.4 KB
  • 大小: 14.8 KB
  • 大小: 72 KB
  • 大小: 168.5 KB
  • 大小: 46.7 KB
  • 大小: 212 KB
  • 大小: 39.3 KB
  • 大小: 126 KB
  • 大小: 20.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics