-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfrtwidth.java
More file actions
53 lines (42 loc) · 1.56 KB
/
Copy pathfrtwidth.java
File metadata and controls
53 lines (42 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.awt.*;
import javax.swing.*;
public class frtwidth extends JFrame {
private final int width = 800;
private final int height = 600;
private final int initialLineWidth = 10;
public frtwidth() {
super("Fractal Tree");
setSize(width, height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
getContentPane().setBackground(Color.WHITE);
}
public void drawTree(Graphics2D g, int x1, int y1, double angle, int depth, int lineWidth) {
if (depth == 0)
return;
int branchLength = depth * 10;
int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * branchLength);
int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * branchLength);
g.setStroke(new BasicStroke(lineWidth));
g.drawLine(x1, y1, x2, y2);
drawTree(g, x2, y2, angle - 20, depth - 1, lineWidth - 1);
drawTree(g, x2, y2, angle + 20, depth - 1, lineWidth - 1);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
int startX = width / 2;
int startY = height - 10;
int angle = -90;
int depth = 10;
drawTree(g2d, startX, startY, angle, depth, initialLineWidth);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
frtwidth fractalTree = new frtwidth();
fractalTree.setVisible(true);
});
}
}