hi i need a tutor for java lab i already did part of the program i need to make the rest but it is hard this is the lab Description 1) The listing of Tree.java is supplied below as a starting point...


hi i need a tutor for java lab i already did part of the program i need to make the rest but it is hard this is the lab 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. d. 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. e. 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. f. 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. a. 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. b. 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. a. 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. b. 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. c. 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 order parameter to be total levels of recursion (ex: 3). The base case to terminate the recursion is when order <>




May 18, 2022
SOLUTION.PDF

Get Answer To This Question

Submit New Assignment

Copy and Paste Your Assignment Here