Advertisement
DavidsonDFGL

FracTree

Jul 2nd, 2016
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import javax.swing.JFrame;
  4.  
  5. public class FractalTree extends JFrame
  6. {
  7.     public double toRad(double grad)
  8.     {
  9.         return (grad*Math.PI)/180;
  10.     }
  11.  
  12.     public FractalTree()
  13.     {
  14.         super("Fractal Tree");
  15.         setBounds(0, 0, 1024, 768);
  16.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  17.     }
  18.  
  19.     private void drawTree(Graphics g, int x1, int y1, double angle, int depth)
  20.     {
  21.         if (depth == 0) return;
  22.         int x2 = x1 + (int) (Math.cos(toRad(angle)) * depth * 10.0);
  23.         int y2 = y1 + (int) (Math.sin(toRad(angle)) * depth * 10.0);
  24.         g.drawLine(x1, y1, x2, y2);
  25.         drawTree(g, x2, y2, angle - 20, depth - 1);
  26.         drawTree(g, x2, y2, angle + 20, depth - 1);
  27.     }
  28.  
  29.     @Override
  30.     public void paint(Graphics g)
  31.     {
  32.         getContentPane().setBackground(Color.BLACK);
  33.         g.setColor(Color.YELLOW);
  34.         drawTree(g, 512, 768, -90, 11);
  35.     }
  36.  
  37.     public static void main(String[] args) {
  38.         JFrame jf = new FractalTree();
  39.         jf.setVisible(true);
  40.         jf.getContentPane().setBackground(Color.BLACK);
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement