The listing of treejava is supplied below as a starting


java programing

Description

1) The listing of Tree.java is supplied below as a starting point for this lab. A picture of Tree.java output is also shown. Either cut/paste the code or type it in. Run the program to verify that it works. Study the listing until you understand completely what it does. In the Java Class Library (appendix M) look up Graphics, Point, and Applet if you need to refresh yourself on how to use these classes.

2) This is a good lab to make changes one at a time. In this way, you won't ever be too far from having a working program. Always make a backup copy after each step. Name your new program NewTree.java. A picture of the output of my version of this program is shown below.

3) First lets tackle some easy changes.

  • An easy change is to set the background color in the init() method. The Applet class method setBackground() can be called to color the sky section. There are examples of setBackground() in the text.
  •  Another easy change is to color the grass section green. First use setColor() to alter the foreground color that will be used. The graphics method fillRect() can than be called. In my completed program, whose output is shown below, the grass occupies the bottom quarter of the display. Remember that Java considers the point (0,0) to be the upper left corner of the display; the coordinate (APPLET_WIDTH, APPLET_WIDTH) is the bottom right corner (See the listing below); and 0 degrees points vertically down towards the bottom.
  •  Next use setColor() in the drawTree() method to turn the bark of the tree brown.

4) In this lab we will declare many constants. I would suggest that as you proceed with the implementation, you parameterize every constant. Note how APPLET_WIDTH, and APPLET_WIDTH are declared in the listing below. This way, if you want to see how different values affect the output, you will be able to simply change a single value without altering any of the programmed logic.

5) Now lets concentrate enhancing the trees that are drawn. We'll need to widen the trunk, add leaves, and randomly vary the number/length/angles of the tree branches.

  •  First create a method that randomly picks a number between two integers. It's a good idea to make this a method because we'll be using it many times in this lab. The signature line is: int pickRandom(int min, int max); You can use the method Math.Random() to pick a random number between 0 and 1. That value needs to be scaled between min and max; and the scaled result is returned. An example of the use of this method is x = pickRandom(3, 10). After the call, x will have some value between 3 through 10.pickRandom() needs to contain only one executable statement.
  •  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.

6) Next widen the branches so they are not stick figures. One way to accomplish this is to use the Graphics fillPolygon() method. You'll need four horizontal and vertical coordinates. If (x,y), and (x',y') represents the coordinates for the ends of a branch and width represents the width of the branch; the four points to be supplied to fillPolygon() are (x,y), (x+width,y), (x'+width,y'), and (x',y'). It is a good idea to tie the length of the branch to the width. In my implementation, I used an 8 to 1 ratio.

7) We need a method to draw the leaves. This method is to be called by drawTree() each time it finishes drawing a branch. See if you can figure out the proper place(s) to insert this call in the Tree.java listing shown below.

  •  The following signature should be used to implement the method to draw a leaf:

public void drawLeaf

(Graphics g,int x,int y,int h,int w,int angle,int order);

where: x,y = the coordinates for top left corner of a rectangle surrounding the leaf; h,w = the height and width of the leaf; angle = the angle of the direction that the leaf points, order = the level of the recursion.

  •  As an initial step draw something simple without recursion. Drawing a circle or oval will suffice for this step. A simple drawLeaf() implementation allows you to establish that the leaves are being inserted properly on your tree. In this version of drawLeaf(), set the color to some shade of green. The statement Color color = new Color(102, 153, 51); instantiates a dark green. The 102, 153, and 51 in the above statement are respectively the red, green, and blue components of the instantiated color.
  •  If everything works so far, we're ready to program the real drawLeaf() method. I would suggest that you debug it separately and then plug it into the program after you know it works.

i. The first requirement for this method is that it should have at least three levels of recursion. The initial call will specify the orderparameter to be total levels of recursion (ex: 3). The base case to terminate the recursion is when order < 1; Recursive calls to the leaf method specifies the last parameter as order - 1 so the base case will eventually be reached.
ii. Your part in this design is to come up with a fractal pattern that resembles a leaf. Any reasonable pattern that you come up will suffice. See the snowflake example in the text to become familiar with how to draw images using fractals. We'll also talk about some design possibilities in class.
iii. A final requirement for the drawLeaf() method, is to vary the colors at each level of recursion. A simple array colors[] = new Color[3] can be created for this purpose (Assuming 3 levels of recursion). An example of a statement that could be used is page.setColor(colors[order]); where order is the recursion level and page is the instantiated Graphics object. Remember to instantiate the array object itself and also to instantiate each index of the color array with a color. Lots of rgb color patterns can be found on the Internet. Let me know if you can't find a color that you need.

iv. The final step relating to the tree phase of the project is to add a For loop in you paint program to draw multiple trees. Randomly select the size and position of each tree. The base of the tree should be in the grass area. In my program, I varied the initial tree size between 20 and 100 pixels. Make sure to draw at least 3 trees. You can draw more if you like. I chose to draw 8 in my implementation.

8) Now we move to the task of drawing the clouds. The first step is to succeed drawing one cloud. A good java method that can be used is fillOval(), which is called using the variables x, y, width, and height. Note that the x, y arguments given to fillOval() point to the pixel at the top left corner of the oval; not the middle. The signature for the method to use for drawing clouds is:

void drawCloud

(Graphics page, int x, int y, int w, int h, int order)

where: x, y is the coordinate to the top left pixel of the cloud; w,h indicates the width and height of the cloud; order is the level of recursion.

You can design your drawCloud method as you wish, but I've included the following pseudo code as a possibility.

RETURN if level <= 1

Randomly pick a color and fill the oval using x, y, w, and h

Find the middle of the oval (x+w/2,y+h/2)
FOR a random number of recursive calls

Randomly pick an angle

Randomly pick a distance from the middle

Compute a new point (newX, newY) based on angle/distance from x/y

Randomly pick a new width (newW) and height (newH)

Recursively call drawCloud using newX, newY, newW, newH

9) The final step is to draw multiple clouds using a For loop. Randomly pick the coordinates and the size of each cloud. In my program, I chose to draw 30 clouds having a random width and height with a 75 pixel maximum. All of my clouds were drawn starting on the top half of the screen so they would not likely expand to the grass section.

10) Answer the synthesis questions in an rtf or doc file (answers.rtf or answers.doc). Type your name and the lab number on this file and include the questions with the answers.

11) Zip your Eclipse project along with the synthesis answers and email to [email protected].

Listing of Tree.java
// 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.*;

public class Tree extends Applet

{ 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;

// Initialize the applet.

public void init() { setSize(APPLET_WIDTH, APPLET_HEIGHT); }

// Create the drawing that displays on the applet.

public void paint(Graphics page)

{ drawTree(page, APPLET_WIDTH/2, APPLET_HEIGHT, STARTSIZE, STARTANGLE);

}

public void drawTree( Graphics page, int x, int y, double size, double angle )

{ 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 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;

}

}

Request for Solution File

Ask an Expert for Answer!!
JAVA Programming: The listing of treejava is supplied below as a starting
Reference No:- TGS01036283

Expected delivery within 24 Hours