import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Line2D; import java.util.ArrayList; import java.util.Random; import javax.swing.JFrame; /** * Snowflake / coastline printer * * For snowflake, set the shape variable below to 3 or more. The best value is probably 6. * For coastline, set shape to 1 and set size to 600. **/ public class Tri extends JFrame { private final double sq2 = Math.sqrt(2); private final int shape = 3; private final int siz=200; private class Limb{ double sx; double sy; double len; double angle; public Limb(double ssx, double ssy, double llen, double aangle) { this.sx=ssx; this.sy=ssy; this.len=llen; this.angle=aangle; } public double ex() { return this.sx+Math.cos(angle)*len; } public double ey() { return this.sy+Math.sin(angle)*len; } } private Random random; private ArrayList ls; public Tri(){ ls = new ArrayList<>(); setSize(820, 820); random = new Random(); setTitle("JFrame"); setBackground(Color.WHITE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); double interiorAngle = 180-(180.0*(shape-2))/shape; Limb p = new Limb(150, 150, siz, 0); ls.add(p); for (int i=1; i1) pm=1; ls.add(new Limb(l25.ex(), l25.ey(), l.len*0.25*sq2, l.angle+pm*Math.PI/4)); ls.add(new Limb(l75.ex(), l75.ey(), l.len*0.25*sq2, l.angle-pm*(1.25*Math.PI))); ls.add(l2); if (ls.size()%20==0) repaint(); try { Thread.sleep(20); } catch (Exception e) {} addL(); } public String toString() { return ""; } public static void main(String[] args) { // TODO Auto-generated method stub Tri m = new Tri(); m.repaint(); m.addL(); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.clearRect(0, 0, 820, 800); g2.setStroke(new BasicStroke(1)); for (Limb l:ls) { g2.draw(new Line2D.Float((int)l.sx, (int)l.sy, (int)l.ex(), (int)l.ey())); } } }