下圖就是我在Java畫圖板上畫出的一個謝爾賓斯地毯
java
製做謝爾賓斯地毯咱們須要用到——迭代算法
理解:
dom
咱們能夠看出2圖是由若干個1圖這樣的基本圖形組成的佈局
一樣無論迭代多少次都是由基本圖形組成spa
因此咱們的任務就是畫出1圖這樣的基本圖形,剩下的就交給迭代去完成code
public void draw(Graphics g,int n,int x,int y,int width,int height) { /** *x:左上角x座標,y:左上角y座標 *width:寬,height:高 *n:迭代層次 */ g.fillRect(x+width/3,y+height/3,width/3,height/3); if(n==0)//遞歸結束條件 return; n--; //八個維度 draw(g,n,x, y, width/3, height/3); //1 draw(g,n,x+width/3, y, width/3, height/3);//2 draw(g,n,x+2*(width/3), y, width/3, height/3);//3 draw(g,n,x, y+height/3, width/3, height/3);//4 draw(g,n,x+2*(width/3), y+height/3, width/3, height/3);//5 draw(g,n,x, y+2*(height/3), width/3, height/3);//6 draw(g,n,x+width/3, y+2*(height/3), width/3, height/3);//7 draw(g,n,x+2*(width/3), y+2*(height/3), width/3, height/3);//8 }
理解:
原理和地毯同樣,都要用到迭代orm
這是最基礎的謝爾賓斯三角形對象
最外面的大三角形的三個座標是咱們本身取的blog
咱們只要知道六個頂點,畫出六條直線,剩下的就能夠交給遞歸了遞歸
假設最外層三角形的座標分別爲(x1,y1)(x2,y2),(x3,y3)
那麼三個中點的座標就是
public void drawtri(Graphics g,int n,int x1,int y1,int x2,int y2,int x3,int y3 ){ g.drawLine(x1,y1,x2,y2); g.drawLine(x3,y3,x2,y2); g.drawLine(x1,y1,x3,y3); g.drawLine((x1+x2)/2,(y1+y2)/2,(x1+x3)/2,(y1+y3)/2); g.drawLine((x1+x2)/2,(y1+y2)/2,(x2+x3)/2,(y2+y3)/2); g.drawLine((x3+x2)/2,(y3+y2)/2,(x1+x3)/2,(y1+y3)/2); if(n==0) return; n--; drawtri(g,n,x1,y1,(x1+x2)/2,(y1+y2)/2,(x1+x3)/2,(y1+y3)/2); drawtri(g,n,(x1+x2)/2,(y1+y2)/2,x2,y2,(x3+x2)/2,(y3+y2)/2); drawtri(g,n,(x1+x3)/2,(y1+y3)/2,(x2+x3)/2,(y2+y3)/2,x3,y3); }
咱們在畫圖板上加上兩個按鈕,達到點擊按鈕
以鼠標點擊與落下的線段做爲地毯的對角線去繪製地毯
以咱們在畫圖板上點擊的三個點去繪製三角形
畫圖板類:
import javax.swing.JFrame; //窗體 import javax.swing.JButton; //按鈕 import java.awt.FlowLayout; //流式佈局器 import java.awt.Graphics; //畫筆 public class DrawPad { public static void main(String args[]){ DrawPad dp = new DrawPad();//建立畫圖板對象 dp.showUI(); } public void showUI(){ JFrame jf = new JFrame(); DrawPadListener dl = new DrawPadListener(); String[] brnstrs = {"地毯","三角形"}; for (int i = 0; i < brnstrs.length; i++) { JButton btn = new JButton(brnstrs[i]); btn.setName(brnstrs[i]); btn.addActionListener(dl);//按鈕添加監聽器 jf.add(btn);//將按鈕添加到窗體上 } FlowLayout fl = new FlowLayout(); //流式佈局器,使得添加到窗體上的組件從上到下,從左到右排列 //JFrame默認是 jf.setTitle("可視化"); //名稱 jf.setSize(800,600); //尺寸 jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //關閉進程 jf.setLayout(fl); //設置流式佈局 jf.setResizable(false); //不能夠改變窗體大小 jf.setLocationRelativeTo(null);//居中顯示 jf.addMouseListener(dl); jf.setVisible(true); //設置窗體可視 Graphics g = jf.getGraphics(); //取畫筆 dl.g = g; } }
監聽器類:
package 分形; import java.awt.Color; //顏色 import java.awt.Graphics; //畫筆 import java.awt.event.ActionEvent; import java.awt.event.ActionListener;//動做監聽器 import java.awt.event.MouseEvent; import java.awt.event.MouseListener; //鼠標監聽器 import java.util.Random; public class DrawPadListener implements MouseListener, ActionListener{ String btnstr; Graphics g; int x1,x2,y1,y2,x3,y3,x4,y4,x5,y5; int count = 0; int n = 1; Random r = new Random(); public void actionPerformed(ActionEvent e){ btnstr = e.getActionCommand(); System.out.println("actionPerformed方法被調用,btnstr爲:"+btnstr); } public void mouseClicked(MouseEvent e){ System.out.println("點擊"); } public void mousePressed(MouseEvent e){ System.out.println("按下"); x1=e.getX(); y1=e.getY(); double a,b,c,d; if(btnstr.equals("三角形")){ if(count == 0){ x3 = e.getX(); y3 = e.getY(); count++; }else if(count==1){ x4 = e.getX(); y4 = e.getY(); count++; }else if(count==2){ x5 = e.getX(); y5 = e.getY(); drawtri(g,n,x3,y3,x4,y4,x5,y5); count = 0; } } } public void mouseReleased(MouseEvent e){ System.out.println("釋放"); x2=e.getX(); y2=e.getY();//釋放點的座標 if(btnstr.equals("地毯")) drawcpt(g,n,x1,y1,x2-x1,y2-y1); } public void mouseEntered(MouseEvent e){ System.out.println("進入"); } public void mouseExited(MouseEvent e){ System.out.println("退出"); } //謝爾賓斯地毯 public void drawcpt(Graphics g,int n,int x,int y,int width,int height) { g.fillRect(x+width/3,y+height/3,width/3,height/3); if(n==0)//遞歸結束條件 return; n--; drawcpt(g,n,x, y, width/3, height/3); drawcpt(g,n,x+width/3, y, width/3, height/3); drawcpt(g,n,x+2*(width/3), y, width/3, height/3); drawcpt(g,n,x, y+height/3, width/3, height/3); drawcpt(g,n,x+2*(width/3), y+height/3, width/3, height/3); drawcpt(g,n,x, y+2*(height/3), width/3, height/3); drawcpt(g,n,x+width/3, y+2*(height/3), width/3, height/3); drawcpt(g,n,x+2*(width/3), y+2*(height/3), width/3, height/3); } //謝爾賓斯三角形 public void drawtri(Graphics g,int n,int x1,int y1,int x2,int y2,int x3,int y3 ){ g.drawLine(x1,y1,x2,y2); g.drawLine(x3,y3,x2,y2); g.drawLine(x1,y1,x3,y3); g.drawLine((x1+x2)/2,(y1+y2)/2,(x1+x3)/2,(y1+y3)/2); g.drawLine((x1+x2)/2,(y1+y2)/2,(x2+x3)/2,(y2+y3)/2); g.drawLine((x3+x2)/2,(y3+y2)/2,(x1+x3)/2,(y1+y3)/2); if(n==0) return; n--; drawtri(g,n,x1,y1,(x1+x2)/2,(y1+y2)/2,(x1+x3)/2,(y1+y3)/2); drawtri(g,n,(x1+x2)/2,(y1+y2)/2,x2,y2,(x3+x2)/2,(y3+y2)/2); drawtri(g,n,(x1+x3)/2,(y1+y3)/2,(x2+x3)/2,(y2+y3)/2,x3,y3); } }