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 javax.swing.JFrame; public class Tree extends JFrame { 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; } } ArrayList ls; public Tree(){ ls = new ArrayList<>(); setSize(820, 820); setTitle("JFrame"); setBackground(Color.WHITE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void addL(double ssx, double ssy, double llen, double aangle) { System.out.println(llen); if (llen<1) { repaint(); return; } Limb l = new Limb(ssx, ssy, llen, aangle); ls.add(l); repaint(); try { Thread.sleep(5); } catch (Exception e) {} ls.add(new Limb(l.ex(), l.ey(), llen*3, aangle+Math.PI/2)); ls.add(new Limb(l.ex(), l.ey(), llen*3, aangle-Math.PI/2)); addL(l.ex(), l.ey(), llen-4, aangle); /** addL(l.ex(), l.ey(), llen*0.50, aangle+Math.PI/6); addL(l.ex(), l.ey(), llen*0.50, aangle); addL(l.ex(), l.ey(), llen*0.50, aangle-Math.PI/6); **/ } public String toString() { return ""; } public static void main(String[] args) { Tree m = new Tree(); m.repaint(); m.addL(410, 800, 40, -Math.PI/2); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.clearRect(0, 0, 820, 800); g2.setStroke(new BasicStroke(1)); for (Limb l:ls) { //System.out.println(l.sx + " " + l.sy + " " + l.ex() + " " + l.ey()); g2.draw(new Line2D.Float((int)l.sx, (int)l.sy, (int)l.ex(), (int)l.ey())); } } }