Random Tree Generator in Java

in art •  7 years ago 


Random Tree Generator in Java


Have you ever feeled the desire to generate a random forest by pressing one button ? In this post I'll show you a small project I've been working on around a year ago and how it works.


Content


  1. How I Started
  2. Rendering
  3. The Algorithm
  4. Trees


How I Started


 


It was a midweek evening and I was bored, so I thought about trying to give myself a programming-challenge. After a while I got the idea to program a Tree-generator. The idea was to create based on an algorithm a tree and than to draw it on my screen.

Because I was programming in Java for a while (and most of the time I still do), I decided to use some Swing  Components (a Java-Run-time integrated GUI-Toolkit).

(A window created in Swing)


Rendering



First of all I had to set up how I wanted my tree to be rendered and I came up with this structure:

A Tree consists of Tree-Elements, which are all Rendering-Objects.

There are two different Rendering-Objects:

Line

A colored line with a starting-point, ending-point and a thickness.

Leaf

A color filled circled with a position.


A Tree was just a List of Tree-Elements.

A class called Screen rendered all Objects, that where added to it and this way I was able to draw the tree on the screen, but where did that tree come from ?


The Algorithm


The first Step on my way to a tree way was the log. The log has always the same color, but a random height.

Also I define a base-color, which will be important for the leafs later.

Not very impressive. The next step were the first branches of the tree. These were less thick than the log. The number of branches was also a little bit random. Every branch was leading from the log to a random location in a 180° radius and with a random length. This way the tree wasn't to boring and also had no branches leading back to the ground, which is only natural, because trees grow in the direction of the sun.

We are getting closer ^^

I could've now added leafs to the tree, but I decided to wait until I made the second branches.

These were starting at the end of their root-branch and had again a random angle and length. Again they were a little bit less thick than the first branches.

And now finally I came to the last step: The Leafs

The leafs were simply put at random positions near the second branches ( just between start-x and end-x, as well as start-y and end-y). They also got a color based on the tree's base-color and a random size.

And this way I programmed my Tree-Generator. I added a KeyListener, so this way I could press some key and the Generator would based on the key I pressed generate a tree with a different amount of branches.

Trees


Here are a few more examples:


Thank you for reading and I hope you enjoyed my post. It would be nice, if you vote, follow or comment on my post, because this way I can see, that you like my content and will know, that you like it.


Have a nice Day ^^


by DrSkort


Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Wow great! ^^ I am going to build my own forest tomorrow :D

Congratulations @drskort! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes received
You got a First Reply

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Congratulations @drskort! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

Click here to view your Board

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @drskort! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!