How to resolve the algorithm Fractal tree step by step in the Java programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Fractal tree step by step in the Java programming language
Table of Contents
Problem Statement
Generate and draw a fractal tree.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Fractal tree step by step in the Java programming language
1. Import Statements:
- The code imports necessary libraries for graphics and window management.
2. FractalTree
Class:
- This is the main class that extends the
JFrame
class to create a window to display the fractal tree. - It defines a constructor that sets up the window's properties (title, size, etc.).
3. drawTree
Method:
- This is the recursive function that draws the fractal tree. It takes the following parameters:
g
: TheGraphics
object used to draw on the screen.x1
,y1
: The coordinates of the starting point of the trunk.angle
: The angle at which the trunk is drawn.depth
: The current depth of the fractal tree.
4. Drawing the Trunk:
- The method first checks if the
depth
is 0, which is the base case where no more branches are drawn. - It then calculates the end coordinates (
x2
,y2
) of the trunk using trigonometry based on the givenangle
anddepth
. - The trunk is drawn as a line from (
x1
,y1
) to (x2
,y2
).
5. Recursive Branching:
- The method recursively calls itself twice to draw two branches from the end point of the trunk, one with an angle 20 degrees smaller and one with an angle 20 degrees larger.
- This creates the branching structure of the fractal tree.
6. Overriding the paint
Method:
- The
paint
method is overridden to draw the fractal tree on the window. - It sets the color to black and calls the
drawTree
method with the appropriate parameters to draw the tree.
7. main
Method:
- This is the entry point of the program. It creates an instance of the
FractalTree
class and makes it visible.
Source code in the java programming language
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class FractalTree extends JFrame {
public FractalTree() {
super("Fractal Tree");
setBounds(100, 100, 800, 600);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void drawTree(Graphics g, int x1, int y1, double angle, int depth) {
if (depth == 0) return;
int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 10.0);
int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 10.0);
g.drawLine(x1, y1, x2, y2);
drawTree(g, x2, y2, angle - 20, depth - 1);
drawTree(g, x2, y2, angle + 20, depth - 1);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.BLACK);
drawTree(g, 400, 500, -90, 9);
}
public static void main(String[] args) {
new FractalTree().setVisible(true);
}
}
You may also check:How to resolve the algorithm Trigonometric functions step by step in the MAXScript programming language
You may also check:How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the Wren programming language
You may also check:How to resolve the algorithm Set right-adjacent bits step by step in the Quackery programming language
You may also check:How to resolve the algorithm Longest common subsequence step by step in the Groovy programming language
You may also check:How to resolve the algorithm ISBN13 check digit step by step in the BCPL programming language