Click here to Skip to main content
Click here to Skip to main content
Go to top

L-Systems - Tree Fractal

, 20 May 2014
Rate this:
Please Sign up or sign in to vote.
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)
        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);

    public void paintComponent(Graphics g) {
        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() {

            public void run() {
                frame.setContentPane(new L_Systems_Tree());
                frame.setSize(1000, 700);



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

India 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 :-
1, CS - Computer Science Lovers : Please like it.
2. CS(Computer Science) Lovers :
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

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 :
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

QuestionFractals PinmemberAngel Ivanov29-Jun-14 6:12 
AnswerRe: Fractals PinprofessionalPsycho_Coder29-Jun-14 7:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140905.1 | Last Updated 21 May 2014
Article Copyright 2014 by Psycho_Coder
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid