谢尔宾斯基的分形三角形
import java.awt.Graphics; import java.awt.Point;//时代Java公众号 - nowjava.com 提供 import javax.swing.JFrame; public class Main extends JFrame { public static final int WINDOW_SIZE = 512; public static final int THRESHOLD = 10; // stopping criterion for recursion public static int P1_x, P1_y, P2_x, P2_y, P3_x, P3_y; public Main() { super("Sierpinski"); setSize(WINDOW_SIZE, WINDOW_SIZE); /** 来自 n o w j a v a . c o m - 时 代 Java**/ // A simple triangle P1_x = (int) getSize().getWidth() / 2; ; P1_y = 40; P2_x = 20; P2_y = (int) getSize().getHeight() - 20; P3_x = (int) getSize().getWidth() - 20; P3_y = (int) getSize().getHeight() - 20; setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // Compute the midpoint public Point getMiddle(Point p1, Point p2) { return new Point((int) (p1.getX() + p2.getX()) / 2, (int) (p1.getY() + p2.getY()) / 2); } public void paint(Graphics g) { super.paint(g); draw(new Point(P1_x, P1_y), new Point(P2_x, P2_y), new Point(P3_x, P3_y)); } public void draw(Point p1, Point p2, Point p3) { // termination condition if (p1.distance(p2) < THRESHOLD && p1.distance(p3) < THRESHOLD && p2.distance(p3) < THRESHOLD) return; // stop recursion // draw the current triangle Graphics g = getGraphics(); g.drawLine((int) p1.getX(), (int) p1.getY(), (int) p2.getX(), (int) p2.getY()); g.drawLine((int) p2.getX(), (int) p2.getY(), (int) p3.getX(), (int) p3.getY()); g.drawLine((int) p3.getX(), (int) p3.getY(), (int) p1.getX