Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Draw tree, clouds, leaves with recursion

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Draw tree, clouds, leaves with recursion

    I am trying to use my random method to change how draw tree works but cannot figure it out. This is what i have so far.

    Use pickRandom() to effect how drawTree() works. The number of branches, the angles of the branches, and the lengths of the branches will be varied randomly at each level of recursion. In my program, I varied the number of branches between 2 and 6; I varied the angles between -40 and +40 degrees from that of the parent branch; and I varied the length of a branch between 1/10 and 9/10 of that of the parent branch.


    //Program to create a recursive tree.
    //This is the base program to be used to start the cs258 project on recursion.
     
    import java.applet.Applet;
    import java.awt.*;
    import java.util.Random;
     
    public class Tree extends Applet
    {  /**
    	 * 
    	 */
    	private static final long serialVersionUID = -2796931303737879571L;
    private final int    APPLET_WIDTH  = 320;
    private final int    APPLET_HEIGHT = 320;
    private final double STARTSIZE     = 110.0;
    private final double STARTANGLE    = 180.0;
    private final double CHANGEANGLE   =  30.0;
    private final double FACTOR        =   2.0;
    private final double MINSIZE       =  10.0;
     
    Color skyblue = new Color(31, 200, 226);
    Color grassgreen = new Color(9, 97, 47);
    Color treebark = new Color(77, 49, 25);
     
    int r;
    int maxclouds = 30;
     
    //Initialize the applet.
     
    public void init()  
    {
    	setSize(APPLET_WIDTH, APPLET_HEIGHT); 
    	setBackground(skyblue);
     
     
    }
    private void setColor(Color green) 
    {
    //	setColor(Color green);
     
     
    }
     
    public void paint(Graphics page)  
    { 
    	Graphics2D ga = (Graphics2D) page;
    	ga.setColor(grassgreen);
    	ga.fillRect(0, APPLET_WIDTH-APPLET_WIDTH/4, APPLET_WIDTH, APPLET_HEIGHT/4 );
    	drawTree(page, APPLET_WIDTH/2, APPLET_HEIGHT, STARTSIZE, STARTANGLE);
    	//drawCloud(page, 20, 20, 20, 20, 1);
     
     
    }
     
    public void drawTree( Graphics page, int x, int y, double size, double angle )
    {  
       page.setColor(treebark);
       Point endPoint = calculatePoint(x, y, size, angle );
       page.drawLine(x, y, endPoint.x, endPoint.y);
     
       if (size > MINSIZE)
       		{  
    	   		drawTree(page, endPoint.x, endPoint.y
                                   , size/FACTOR, angle+CHANGEANGLE);
    	   		drawTree(page, endPoint.x, endPoint.y
                                   , size/FACTOR, angle-CHANGEANGLE);
     
     
       		}  
    } 
     
    public void drawGrass(Graphics page, int x, int y, double height, double width)
      {
          page.setColor(grassgreen);
          page.fillRect(APPLET_WIDTH, APPLET_HEIGHT, -320, -90); 
      }
     
    public void drawCloud (Graphics page, int x, int y, int w, int h, int order)
    {
    	page.setColor(Color.white);
    	page.fillOval(10, 10, 20, 30);
    	for(int i = 0; i <30; i++)
    	{
     
    		page.fillOval(pickRandom(0, 80),pickRandom(0, 10),pickRandom(1,3), pickRandom(1, 5));
    	}
     
     
     
    }
     
    public Point calculatePoint( int x, int y, double size, double degree )
    {  
    	Point point = new Point(x, y);
       double radians = Math.PI/180. * degree;
     
       point.x += (int)(size * Math.sin(radians));
       point.y += (int)(size * Math.cos(radians));  
       return point;
    }
     
    public int pickRandom(int min, int max)
     
       {
     
     
     
           return (int)Math.random()*(min-max+1) + min;
       }}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Draw tree, clouds, leaves with recursion

    Can you explain what problems you are having? What does the code do when it is executed? What needs to be changed?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Draw tree, clouds, leaves with recursion

    Use pickRandom() to effect how drawTree() works. The number of branches, the angles of the branches, and the lengths of the branches will be varied randomly at each level of recursion. In my program, I varied the number of branches between 2 and 6; I varied the angles between -40 and +40 degrees from that of the parent branch; and I varied the length of a branch between 1/10 and 9/10 of that of the parent branch.

    i dont know how to do this

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Draw tree, clouds, leaves with recursion

    Confused. Your post says that you did all those things but then you add that you don't know how to do "this". What in that list of things do you specifically NOT know how to do?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Draw tree, clouds, leaves with recursion

    Make a list of the changes you want to make.
    Find in the code where the item you want to change is being drawn.
    Work on some logic to change how it is being drawn.
    An item: the number of branches
    What controls the number of branches? How can that logic be changed to use a random number?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Getting height of binary search tree without using recursion
    By berserker in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 8th, 2013, 01:37 AM
  2. Replies: 1
    Last Post: October 20th, 2013, 11:54 PM
  3. Binary Search Tree Recursion Implementation
    By ambientplanet in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 8th, 2013, 03:46 PM
  4. Implementing a binary search tree using recursion?
    By computerbum in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 3rd, 2012, 08:21 PM
  5. converging point clouds
    By archi_student in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 7th, 2011, 07:25 AM

Tags for this Thread