雪花分形图(递归算法)

很奇妙的分形图,佩服作者的想象力,我是在理解的基础上借鉴,Button做的很烂,呵呵!

很喜欢递归算法,我一定要原创一个给你们看看! 

//KochSnowflake.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class KochSnowflake extends JApplet implements ActionListener {
     private final int APPLET_WIDTH = 400;
     private final int APPLET_HEIGHT = 440;
     private final int MIN = 1, MAX = 9;
 
     private JButton increase,decrease;
     private JLabel titleLabel,orderLabel;
     private KochPanel drawing;
     private JPanel appletPanel,tools;
 
public void init() {
     tools = new JPanel();
     tools.setLayout(new BoxLayout(tools,BoxLayout.X_AXIS));
     tools.setBackground(Color.yellow);
     tools.setOpaque(true);
  
     titleLabel = new JLabel(“the koch snowflake”);
     titleLabel.setForeground(Color.black);
  
     increase = new JButton(“increase.gif”);
     increase.setPressedIcon(new ImageIcon(“increasePressed.gif”));
     increase.setMargin(new Insets(0,0,0,0));
     increase.addActionListener(this);
  
    decrease = new JButton(“decrease.gif”);
    decrease.setPressedIcon(new ImageIcon(“decreasePressed.gif”));
    decrease.setMargin(new Insets(0,0,0,0));
    decrease.addActionListener(this);
  
    orderLabel = new JLabel(“order: 1”);
    orderLabel.setForeground(Color.black);
  
     tools.add(titleLabel);
     tools.add(Box.createHorizontalStrut(20));
     tools.add(decrease);
     tools.add(increase);
     tools.add(Box.createHorizontalStrut(20));
     tools.add(orderLabel);
  
    drawing = new KochPanel(1);
  
    appletPanel = new JPanel();
    appletPanel.add(tools);
    appletPanel.add(drawing);
  
    getContentPane().add(appletPanel);
    setSize(APPLET_WIDTH,APPLET_HEIGHT);

}
 
   public void actionPerformed(ActionEvent event) {
         int order = drawing.getOrder();
  
        if(event.getSource() == increase)
             order++;
       else
             order–;
        if(order >= MIN && order <= MAX) {
              orderLabel.setText(“Order:”+order);
              drawing.setOrder(order);
              repaint();
       }
  }

}

//KochPanel.java
import java.awt.*;
import javax.swing.JPanel;

public class KochPanel extends JPanel {
     private final int PANEL_WIDTH =400;
     private final int PANEL_HEIGHT = 400;
     private final double SQ = Math.sqrt(3.0)/6;
     private final int TOPX = 200,TOPY = 20;
     private final int LEFTX = 60,LEFTY = 300;
     private final int RIGHTX = 340,RIGHTY = 300;
 
     private int current;
 
     public KochPanel(int currentOrder) {
          current = currentOrder;
          setBackground(Color.black);
          setPreferredSize(new Dimension(PANEL_WIDTH,PANEL_HEIGHT));
     }
     public void drawFractal(int order,int x1,int y1,int x5,int y5,
                         Graphics page) {
          int deltaX,deltaY,x2,y2,x3,y3,x4,y4;
          if(order == 1)
         page.drawLine(x1,y1,x5,y5);
         else {
         deltaX =x5 – x1;
         deltaY =y5 – y1;
   
         x2 = x1 + deltaX/3;
         y2 = y1 + deltaY/3;
   
         x3 = (int)((x1+x5)/2 + SQ*(y1-y5));
         y3 = (int)((y1+y5)/2 + SQ*(x5-x1));
   
         x4 = x1 + deltaX*2/3;
         y4 = y1 + deltaY*2/3;
   
        drawFractal(order-1,x1,y1,x2,y2,page);
        drawFractal(order-1,x2,y2,x3,y3,page);
        drawFractal(order-1,x3,y3,x4,y4,page);
        drawFractal(order-1,x4,y4,x5,y5,page);
        }
    }
   
   public void paintComponent(Graphics page) {
        super.paintComponent(page);
        page.setColor(Color.green);
    
       drawFractal(current,TOPX,TOPY,LEFTX,LEFTY,page);
       drawFractal(current,LEFTX,LEFTY,RIGHTX,RIGHTY,page);
       drawFractal(current,RIGHTX,RIGHTY,TOPX,TOPY,page);
   }
   public void setOrder(int order) {
       current = order;
    }
   public int getOrder() {
    return current;
   }
  
  
 }

 

    原文作者:递归算法
    原文地址: https://blog.csdn.net/protiger/article/details/2260923
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞