Go to top

# L-Systems - Tree Fractal

, 20 May 2014
 Rate this:
Working example of a Tree fractal to demonstrate Turtle Graphics in Java
What are L- Systems ?

Ans : An excerpt from Wikipedia :-

An L-system or Lindenmayer system is a parallel rewriting system and a type of formal grammar. An L-system consists of an alphabet of symbols that can be used to make strings, a collection of production rules that expand each symbol into some larger string of symbols, an initial "axiom" string from which to begin construction, and a mechanism for translating the generated strings into geometric structures. L-systems were introduced and developed in 1968 by Aristid Lindenmayer, a Hungarian theoretical biologist and botanist at the University of Utrecht. Lindenmayer used L-systems to describe the behaviour of plant cells and to model the growth processes of plant development

So, I wrote a Java code to generate the Tree Fractal. The code itself is self-explanatory.

Here's the code :

```import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/**
* @author psychocoder
*/
public class L_Systems_Tree extends JPanel {

private static final long serialVersionUID = 1L;

public L_Systems_Tree() {
}

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 * 12.0);
int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 12.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 paintComponent(Graphics g) {
g.setColor(Color.DARK_GRAY);
drawTree(g, getWidth() / 2, getHeight(), -90, 10);
}

public static void main(String... args) {
final JFrame frame = new JFrame("L Systems - Tree Fractal");
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
frame.setContentPane(new L_Systems_Tree());
frame.setSize(1000, 700);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setResizable(true);
frame.setVisible(true);
}
});
}
} ```

Screenshot

## Share

Student
India
My name is Animesh Shaw, Currently I am pursuing B. Tech in Computer Science. My Screen name is Psycho_Coder. I am active in many Security or Ethical Hacking forums. I love programming and exploring the depths of Computer Science.

Programming Languages know : C, C++,Java, Python and PHP.

You can even contact me on the following :-

2. CS(Computer Science) Lovers : https://www.facebook.com/groups/csloversforever/
My facebook group to discuss programming. You can join this group if you are serious about computer science and programming.

3. You can mail me at psychocoder@outlook.com

My Interests :

I have great interests in the following topics in Computer Science :-

1. Programming Language Concepts and Methodologies.
2. Computational Linguistics.
3. Cryptography.

Github Profile : https://github.com/PsychoCoderHC

## You may also be interested in...

### Protecting Android™ Applications with Secure Code Signing Certificates

 First Prev Next
 Fractals Angel Ivanov 29-Jun-14 6:12
 Re: Fractals Psycho_Coder 29-Jun-14 7:10
 Last Visit: 31-Dec-99 18:00     Last Update: 15-Sep-14 21:08 Refresh 1