Sinisterly
[Java] L- Systems : Tree - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Java, JVM, & JRE (https://sinister.ly/Forum-Java-JVM-JRE)
+--- Thread: [Java] L- Systems : Tree (/Thread-Java-L-Systems-Tree)

Pages: 1 2 3


[Java] L- Systems : Tree - Psycho_Coder - 10-21-2013

Tree Fractal example code.

[Image: lsys_zps4b1319f7.png]

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);
            }
        });
    }
}

Played a bit with the code and got some simple designs :-

Spoiler:
[Image: tree1_zpsa9a69261.png]

[Image: tree2_zpsfd478bdf.png]

[Image: tree3_zps2b0a4204.png]

[Image: tree4_zps76379be9.png]

[Image: tree5_zps07df9f3f.png]

[Image: tree6_zpsf76b01d5.png]



RE: [Java] L- Systems : Tree - Deque - 10-21-2013

Neat. That's beautiful code (as far as Java can be beautiful) and a beautiful result.

Some minor things: The @author tag belongs above the class declaration.
If you print the stacktrace you don't need to print the error message too. It is already done with the stacktrace.
You can use invokeLater() instead of invokeAndWait(). The latter is used for applets.

Code:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Make it a habit to use DISPOSE_ON_CLOSE instead.


RE: [Java] L- Systems : Tree - Psycho_Coder - 10-21-2013

(10-21-2013, 07:37 PM)Deque Wrote: Neat. That's beautiful code (as far as Java can be beautiful) and a beautiful result.

Some minor things: The @author tag belongs above the class declaration.
If you print the stacktrace you don't need to print the error message too. It is already done with the stacktrace.
You can use invokeLater() instead of invokeAndWait(). The latter is used for applets.

Code:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Make it a habit to use DISPOSE_ON_CLOSE instead.

Now this is an example that we must never be in a hurry. I had to the post code in my fb group and I was in a hurry, and then forgot to re-factor it. I knew about the first three things that you said. Initially I made an applet but later changed to JFrame but forgot to change the invokeAndWait() to invokeLater().

but about the 4th point that you mentioned about using DISPOSE_ON_CLOSE why ? will you please state the reason. I find that JFrame.EXIT_ON_CLOSE is used in most places and even in the Oracles Java tutorial section examples and so I used EXIT_ON_CLOSE. Whatever the reason maybe I will from now on use what you asked me to do (just as I followed your saying always)

Code Updated.

Sorry for this silly mistakes, I will take care from now on to verify and refactor my code before posting. Thanks.


RE: [Java] L- Systems : Tree - Deque - 10-21-2013

(10-21-2013, 07:56 PM)Psycho_Coder Wrote: but about the 4th point that you mentioned about using DISPOSE_ON_CLOSE why ? will you please state the reason. I find that JFrame.EXIT_ON_CLOSE is used in most places and even in the Oracles Java tutorial section examples and so I used EXIT_ON_CLOSE. Whatever the reason maybe I will from now on use what you asked me to do (just as I followed your saying always)

It's for the same reason you shouldn't use System.exit() in your code.
EXIT_ON_CLOSE shuts down the JVM. This results into code that is:
  • hard to embed
  • hard to reuse
  • hard to make unit tests for

I.e. if you create a .jar another person want's to use in his/her program and your program comes to call EXIT, because the window is closed, the program that embeds your .jar will be closed too. No matter if there are still windows open.

I.e. if you place a ShutdownHook or take any other precautions to close everything correctly before shutdown (cleaning temp files, closing streams, save actions, ...), it won't be called.

I.e. it will be impossible to create unit tests when the program just shuts down.

Don't use EXIT_ON_CLOSE or System.exit(). DISPOSE_ON_CLOSE will shut down your program if the last window was closed and nothing else (threads etc) still runs. (If you use Eclipse you can see if it runs at the red square.) Of course you will have to take more care for a proper shutdown. It may happen that your program still runs after closing it, because something else is still running in the background. But it is your responsibility to close everything properly.

If every programmer would just do that (avoid System.exit()) it would have already had spared me a lot of time decompiling, fixing and recompiling .jar files.


RE: [Java] L- Systems : Tree - Psycho_Coder - 10-22-2013

Played with the code and got some simple designs.

EDIT: The code has not been updated with the code that has effects.


RE: [Java] L- Systems : Tree - cervito21 - 11-26-2013

hi sir im newbie can help how to run that codes? its amazing


RE: [Java] L- Systems : Tree - cervito21 - 11-26-2013

hi sir im newbie can help how to run that codes? its amazing


RE: [Java] L- Systems : Tree - Psycho_Coder - 11-26-2013

(11-26-2013, 03:46 PM)cervito21 Wrote: hi sir im newbie can help how to run that codes? its amazing

Do you use any Java IDE ?

If not then just paste the code in any text editor and save as L_Systems_Tree.java, then from terminal write the following ;-

javac L_Systems_Tree.java
java L_Systems_Tree


RE: [Java] L- Systems : Tree - Psycho_Coder - 11-26-2013

(11-26-2013, 03:46 PM)cervito21 Wrote: hi sir im newbie can help how to run that codes? its amazing

Do you use any Java IDE ?

If not then just paste the code in any text editor and save as L_Systems_Tree.java, then from terminal write the following ;-

javac L_Systems_Tree.java
java L_Systems_Tree


RE: [Java] L- Systems : Tree - cervito21 - 11-27-2013

sir i am using netbeans ide, how can i run it?