Today I will write about the recursive implementation of K-level Koch snowflakes (K value requires you to enter manually). As for what Koch snowflakes are, please Baidu.
First, let’s think about how to write this program. When count = 0, it should be a triangle. These three points are determined from the beginning, and future changes will develop based on these three points. When it is not 0, you need to calculate the 9 points relative to this triangle, namely the two points on each edge, and the third vertex of the triangle corresponding to it.
First, add a panel in JFrame, and we need to draw a picture on this panel.
Let’s take a look at this picture again. This picture introduces the process of calculating the other three points through two points.
Now start drawing in the panel:
static class showpanel extends JPanel{ int number = 0; public void setNumber(int number) { this.number = number; repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g);//Draw a simple panel int side =(int)(Math.min((int)getWidth(),(int)getHeight())*0.8); int high =(int)(side*Math.cos(Math.toRadians(30))); Point p1 = new Point(getWidth() / 2, 10); Point p2 = new Point(getWidth() / 2 - side / 2, 10 + high); Point p3 = new Point(getWidth() / 2 + side / 2, 10 + high); playKochSnowFlake(g, number, p1, p2); playKochSnowFlake(g, number, p2, p3); playKochSnowFlake(g, number, p3, p1); }Now start writing recursive functions.
public static void playKochSnowFlake(Graphics g,int number,Point p1,Point p2) { if(number == 0){ g.drawLine(p1.x, p1.y,p2.x, p2.y); } else{ int deltaX = p2.x - p1.x; int deltaY = p2.y - p1.y; Point x = new Point(p1.x + deltaX / 3, p1.y + deltaY / 3); Point y = new Point(p1.x + deltaX * 2 / 3, p1.y + deltaY * 2 / 3); Point y = new Point(p1.x + deltaX * 2 / 3); Point z = new Point( (int)((p1.x + p2.x) / 2 + Math.sin(Math.toRadians(60)) * (p1.y - p2.y) / 3), (int)((p1.y + p2.y) / 2 + Math.sin(Math.toRadians(60)) * (p2.x - p1.x) / 3)); playKochSnowFlake(g, number - 1, p1, x); playKochSnowFlake(g, number - 1, x, z); playKochSnowFlake(g, number - 1, z, y); playKochSnowFlake(g, number - 1, z); playKochSnowFlake(g, number - 1, z); playKochSnowFlake(g, number - 1, z); playKochSnowFlake(g, number - 1, z); playKochSnowFlake(g, number - 1, z); playKochSnowFlake(g, number - 1, y, p2); } }Then add a JTextField jta to the main panel. The data it enters is to be passed into the number. So add a listener to it. If there is already data input, call the setNumber() function in it to set the number variable.
jta.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { spl.setNumber(Integer.parseInt(jta.getText())); } }); So overall it has been completed, and the rest is the short answer form settings.
Here is a complete java code:
import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class SnowFlake extends JFrame { private JTextField jta = new JTextField(5); private showpanel spl = new showpanel(); static class showpanel extends JPanel{ int number = 0; public void setNumber(int number) { this.number = number; repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g);//Draw a simple panel int side =(int)(Math.min((int)getWidth(),(int)getHeight())*0.8); int high =(int)(side*Math.cos(Math.toRadians(30))); Point p1 = new Point(getWidth() / 2, 10); Point p2 = new Point(getWidth() / 2 - side / 2, 10 + high); Point p3 = new Point(getWidth() / 2 + side / 2, 10 + high); playKochSnowFlake(g, number, p1, p2); playKochSnowFlake(g, number, p2, p3); playKochSnowFlake(g, number, p3, p1); } public static void playKochSnowFlake(Graphics g,int number,Point p1,Point p2) { if(number == 0){ g.drawLine(p1.x, p1.y,p2.x, p2.y); } else{ int deltaX = p2.x - p1.x; int deltaY = p2.y - p1.y; Point x = new Point(p1.x + deltaX / 3, p1.y + deltaY / 3); Point y = new Point(p1.x + deltaX * 2 / 3, p1.y + deltaY * 2 / 3); Point y = new Point(p1.x + deltaX * 2 / 3); Point z = new Point( (int)((p1.x + p2.x) / 2 + Math.sin(Math.toRadians(60)) * (p1.y - p2.y) / 3), (int)((p1.y + p2.y) / 2 + Math.sin(Math.toRadians(60)) * (p2.x - p1.x) / 3)); playKochSnowFlake(g, number - 1, p1, x); playKochSnowFlake(g, number - 1, x, z); playKochSnowFlake(g, number - 1, z, y); playKochSnowFlake(g, number - 1, z); playKochSnowFlake(g, number - 1, z); playKochSnowFlake(g, number - 1, z); playKochSnowFlake(g, number - 1, z); playKochSnowFlake(g, number - 1, z); playKochSnowFlake(g, number - 1, y, p2); } } } public SnowFlake() { JPanel panel = new JPanel(); panel.setLayout(new FlowLayout()); panel.add(new JLabel("Please input the number")); panel.add(jta); add(spl,BorderLayout.CENTER); add(panel,BorderLayout.SOUTH); jta.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { spl.setNumber(Integer.parseInt(jta.getText())); } }); } public static void main(String args[]) { SnowFlake snowFlake = new SnowFlake(); snowFlake.setSize(300, 300); snowFlake.setTitle("SnowFlake"); snowFlake.setLocationRelativeTo(null); snowFlake.setVisible(true); } }Reproduction image:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.