7月6日 分型与迭代总结(谢尔宾斯基三角形)

 

        胡哥说,酷只要能画出一个点,就能画出这个世界。现在我对这句话深信不疑了。这几天,接触到了不少的漂亮的用代码“写”出的图片,我越来越觉得Java是个神奇的东西。

 

        前天我画出了一个镯子,昨天又画了几个细胞,觉得好有意思啊。昨天冥思苦想了半天,终于把胡哥让画的谢尔宾斯基三角形画出来了,尴尬好有成就感啊。
 
        开始时我弄错了要连线的点,导致画出的三角形只划分了中间的。后来我发现其实每个需要划分的三角形就是跟原先最大的三角形是一样的,我觉得我顿悟了递归的涵义啦啦啦酷

 

        下面是我的谢尔宾斯基三角形的代码。

这是Asanjiaoxxing类:

import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

	//定义一个DrawFrame继承自JFrame
	public class Asanjiaoxing extends JFrame{
		//设置全局变量
		Graphics g;
		JTextField jtf1,jtf2;
		private static final long serialVersionUID = 1L;
		//函数入口
		public static void main(String[] args) {
			//实例化一个DrawFrame
			Asanjiaoxing df = new Asanjiaoxing();		
			//调用UnitGui函数 
			df.UnitGui();		
		}		
		public void UnitGui(){
			//设置窗体的属性值
			this.setTitle("谢尔宾斯基三角形");
			this.setSize(new Dimension(600,500));
			this.setDefaultCloseOperation(3);
			this.setLocationRelativeTo(null);
			
			//实例化一个面板
			JPanel jp = new JPanel();
			//实例化一个标签对象
			JLabel jl1  = new JLabel("请输入边长:");
			jp.add(jl1);
		
			//实例化一个文本框
			JTextField jtf1 = new JTextField(5);
			jp.add(jtf1);
			this.jtf1 = jtf1;
			
			//实例化一个标签对象
			JLabel jl2  = new JLabel("请输入循环次数:");
			jp.add(jl2);
			
			//实例化一个文本框
			JTextField jtf2 = new JTextField(5);
			jp.add(jtf2);
			this.jtf2 = jtf2;
			this.add(jp);
			
			this.setVisible(true);
			this.g =this.getGraphics();
			MouseL ml = new MouseL(g,jtf1,jtf2);
			this.addMouseListener(ml);
			}
	 		}			

 这是监听器:

 

package zyf_0706;

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JTextField;

public class MouseL implements MouseListener {
	
	Graphics g ;
	JTextField jtf1,jtf2;
	int count;
	public MouseL(Graphics g,JTextField jtf1,JTextField jtf2){
		
		this.g = g;
		this.jtf1 = jtf1;
		this.jtf2 = jtf2;
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		int x1,y1,length,n;
		x1 = e.getX();
		y1 = e.getY();
		length = (int)Double.parseDouble(jtf1.getText());
		n = (int)Double.parseDouble(jtf2.getText());
		this.count=n;
		double x2,y2,x3,y3;
		int a1,b1,a2,b2,a3,b3;

		x2 = x1+length;
		y2 = y1;
		x3 = x1+length/2;
		y3 = y1-length/2*Math.sqrt(3);
		
		a1 = x1;

		a2 = (int)x2;
		a3 = (int)x3;
		b1 = y1;
		b2 = (int)y2;
		b3 = (int)y3;
		
		g.drawLine(a1,b1,a2,b2);
		g.drawLine(a1,b1,a3,b3);
		g.drawLine(a2,b2,a3,b3);
		draw(a1,b1,a2,b2,a3,b3,count);
	 	
	}
			
	 		//定义一个画三角形的方法
	 		public void draw(int x1,int y1,int x2,int y2,int x3,int y3,int count){
	 			
	 			int A1,B1,A2,B2,A3,B3;
	 			if(count>0){
	 			count--;			
	 			A1=(x1+x2)/2;
	 			A2=(x1+x3)/2;
	 			A3=(x2+x3)/2;
	 			B1=(y1+y2)/2;
	 			B2=(y1+y3)/2;
	 			B3=(y2+y3)/2;
				g.drawLine(A1,B1,A2,B2);
				g.drawLine(A1,B1,A3,B3);
				g.drawLine(A2,B2,A3,B3);
			
				this.draw(A1, B1, A2, B2, x1, y1,count);
				this.draw(A1, B1, x2, y2, A3, B3,count);
				this.draw(x3, y3, A2, B2, A3, B3,count);	
	 			}
	 			else{
	 				return;
	 			}
	 		}
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}

 

下面是我画的几个:

 

版权声明:本文为匿名原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: