Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

Description:
Question 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.
Question 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.
Question 3: First lets tackle some easy changes.
a) 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.
b) 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.
c) Next use setColor() in the drawTree() method to turn the bark of the tree brown.

Question 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.

Question 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.

Question 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.
Question 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 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.

Question 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

Question 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.

Question 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.

Question 11: Zip your Eclipse project along with the synthesis answers and email to harveyd@sou.edu.

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;

}

}

Java, Programming

  • Category:- Java
  • Reference No.:- M91263815
  • Price:- $70

Guranteed 36 Hours Delivery, In Price:- $70

Have any Question?


Related Questions in Java

Question slideshows or carousels are very popular in

Question : Slideshows (or carousels) are very popular in websites. They allow web developers to display news or images on the website in limited space. In this code challenge, you are required to complete the JavaScript ...

In ruby the hash class inherits from enumerable suggesting

In Ruby, the Hash class inherits from Enumerable, suggesting to a programmer that Hashes are collections. In Java, however, the Map classes are not part of the JCF (Java Collections Framework). For each language, provide ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

Assessment instructionsin this assessment you will design

Assessment Instructions In this assessment, you will design and code a simple Java application that defines a class, instantiate the class into a number of objects, and prints out the attributes of these objects in a spe ...

Can someone please help me with the following java

can someone please help me with the following java question The input is an N by N matrix of nonnegative integers. Each individual row is a decreasing sequence from left to right. Each individual column is a decreasing s ...

Operating systems assignment -problem 1 sharing the bridgea

Operating Systems Assignment - Problem 1: Sharing the Bridge A new single lane bridge is constructed to connect the North Island of New Zealand to the South Island of New Zealand. Farmers from each island use the bridge ...

Can someone help me please with those question1what is the

Can someone help me please with those question 1:what is the best data type for student id datatime,currency,number,decimal 2:which relationshipis preferable? one to one,one to many,many to many 3:if you add table A's pr ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

Can someone kindly help me to consider whether java

Can someone kindly help me to consider whether Java provides the facility of operator overloading? If it does, may you kindly describe how overloading operators can be accomplished? If not, may you kindly describe why yo ...

Assignment - java program using array of objectsobjectives

Assignment - JAVA Program using array of objects Objectives - This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details - For this assignment, you are required to develop a Menu ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As