LoganBlackisle

opg 5

Aug 27th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 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. private static final long serialVersionUID = 1L;
  7.  
  8. public FractalTree() {
  9. setBounds(100, 100, 800, 600);
  10. setResizable(false);
  11. setDefaultCloseOperation(EXIT_ON_CLOSE);
  12. }
  13.  
  14. private void drawTree(Graphics g, int x1, int y1, double angle, int depth) {
  15. if (depth == 0)
  16. return;
  17. int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 10.0);
  18. int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 10.0);
  19. g.drawLine(x1, y1, x2, y2);
  20. drawTree(g, x2, y2, angle - 30, depth - 1);
  21. drawTree(g, x2, y2, angle + 30, depth - 1);
  22. }
  23.  
  24. @Override
  25. public void paint(Graphics g) {
  26. g.setColor(Color.BLACK);
  27. drawTree(g, 400, 500, -90, 9);
  28. }
  29.  
  30. public static void main(String[] args) {
  31. new FractalTree().setVisible(true);
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment