Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Fractal Tree (RecursionC)

807599Feb 24 2007 — edited Feb 25 2007
Okay well I'm almost done. I'm just trying to tidy up my code. There's a really ugly sequence of 'if' statements that I'd be very thankful if I could understand how to condense this into a for loop.

Anyway, the idea of the lab is to draw an l-system fractal tree. It'll we fairly clear what I'm trying to do if you run the code.

I'm planning on strapping on a UI in the mean time but any help would be really appreciated

-kentt
/**
 * Fractal trees (L-systems) using Swing.
 *
 * @author Kentt
 * @studentid 188758
 * @course CMPT 167
 */

// imports
import javax.swing.*;		// JPanel
import java.awt.*;		// Graphics
import java.awt.geom.*;		// AffineTransform

/**
 * Fractal trees (L-systems) using Swing.
 */
public class FractalTree extends JPanel {

    /**
    * Initialize GUI widgets and internal variables
    */
    FractalTree(){}

    
    // recursivly draws a fractal tree
    public void drawFractal(Graphics g,
            double length, // length of line
            double lengthCompression, // factor of compression of line
            double angle, // angle line will be drawn at
            double angleCompression, // compression of angle
            int recurse, // how many levels of recursion
            int abscissa, // shift along the x-axis
            int ordinate, // shift along the y-axis
            int previousAbscissas[]) 
    {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
    	
        g2d.translate( abscissa, ordinate );
    	
        if (recurse<100) // for DEBUGGING contol purposes ONLY!
        {
/*            
            // DEBUG
            System.out.printf("Recurse: %d\nLength: %f\n Angle: %f\nAbscissa: %d\nOrdinate: %d\n\n",
                    recurse, length, angle, abscissa, ordinate); //debug
*/            
            
            AffineTransform origXform = g2d.getTransform();
            
            previousAbscissas[recurse] = abscissa;
            
            for ( int count=1 ; count<=(int)Math.pow(2,recurse) ; count++)
            {
                
                // draw left and right and return to original position
                g2d.rotate( Math.toRadians(angle) ); // rotate to draw first branch
                g2d.drawLine( 0, 0, 0, (int) length*(-1) ); // draw first branch
                g2d.rotate( Math.toRadians(angle *-2 ) );  // rotate to draw second branch
                g2d.drawLine( 0, 0, 0, (int) length*(-1) ); // draw 2nd branch
                g2d.rotate( Math.toRadians( angle ) ); // rotate back to vertical

                if ( count%32 == 0 ) 
                {
                    g2d.translate( -2 * previousAbscissas[recurse-5]
                            + 2 * previousAbscissas[recurse-4]
                            + 2 * previousAbscissas[recurse-3]
                            + 2 * previousAbscissas[recurse-2]
                            + 2 * previousAbscissas[recurse-1]
                            + 2 * previousAbscissas[recurse], 0); // move to neighbouring branch(s)                    
                    continue;
                }   
                    
                
                if ( count%16 == 0 )
                {
                    g2d.translate( -2 * previousAbscissas[recurse-4]
                            + 2 * previousAbscissas[recurse-3]
                            + 2 * previousAbscissas[recurse-2]
                            + 2 * previousAbscissas[recurse-1]
                            + 2 * previousAbscissas[recurse], 0); // move to neighbouring branch(s)                    
                    continue;
                }   
                
                if (count%8 == 0 )
                {
                    g2d.translate( -2 * previousAbscissas[recurse-3]
                            + 2 * previousAbscissas[recurse-2]
                            + 2 * previousAbscissas[recurse-1]
                            + 2 * previousAbscissas[recurse], 0); // move to neighbouring branch(s)                    
                    continue;
                }
                
                   
                if (count%4 == 0 )
                {
                    System.out.printf("count:  %d\n recurse:  %d\n\n",count, recurse);
                    g2d.translate( -2 * previousAbscissas[recurse-2]
                            + 2 * previousAbscissas[recurse-1]
                            + 2 * previousAbscissas[recurse], 0); // move to neighbouring branch(s)                    
                    continue;
                }
             
                if ( count%2 == 0 )
                {
                    //shift a different amount
                    g2d.translate( -2 * previousAbscissas[recurse-1] 
                            + 2 * previousAbscissas[recurse], 0); // move to neighbouring branch(s)                    
                    continue;
                } //end if
                
                else
                    g2d.translate(-2*previousAbscissas[recurse], 0); // move to neighbouring branch(s)                    
                                
            } //end for
            
            
            g2d.setTransform( origXform );
            
            // recalculate


            abscissa = (int) ( length * Math.sin( Math.toRadians(angle) ) ); 
            ordinate = (int) -( length * Math.cos( Math.toRadians(angle) ) ); 
            length *= lengthCompression;
            angle *= angleCompression;

            recurse++;
            if (length >2) // recursivly draw until sufficiently short
                drawFractal ( g, length, lengthCompression, angle, angleCompression,
                        recurse, abscissa, ordinate, previousAbscissas);


        } //end if
        
    }
    
    /**
    * Redraw the window pane.
    * @param g Graphics context
    */
    public void paintComponent(Graphics g) 
    {
        
        int previousAbscissas[];
        previousAbscissas = new int[20];

        
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        Dimension dim = getSize();

        AffineTransform origXform = g2d.getTransform();	// save old transform

        g2d.translate( dim.width/2, dim.height/2 );	// centre of window

        g2d.setTransform( origXform );			// restore original

        drawFractal(g, 250, .6, 30, 0.75, 0, 300, 550, previousAbscissas );
   }

    
    
    
    /**
    * Set up the window and pane.
    */
    private static void createAndShowGUI() 
    {
        // Set the look-and-feel to match the current system
        try {
        UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
        } catch (UnsupportedLookAndFeelException e) {
        } catch (ClassNotFoundException e) {
        } catch (InstantiationException e) {
        } catch (IllegalAccessException e) {
        }

        JFrame frame = new JFrame("Fractal Tree");	// create window
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new FractalTree();		// content pane
        panel.setOpaque(true);

        frame.setContentPane(panel);		// connect up and show
        frame.pack();
        frame.setSize( 600, 600 );			// window width, height
        frame.setVisible(true);
   }

    /**
    * Schedule a job for the event-dispatching thread:
    * creating and showing this application's GUI.
    * @param args Command-line arguments
    */
    public static void main( String args[] ) 
    {
        // This is the thread-safe way to initialize the GUI
        Runnable doRun = new Runnable() 
        {
            public void run() 
            {
                createAndShowGUI(); 
            }
        };
        javax.swing.SwingUtilities.invokeLater( doRun );
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 25 2007
Added on Feb 24 2007
4 comments
269 views