Why Choose Us?
0% AI Guarantee
Human-written only.
24/7 Support
Anytime, anywhere.
Plagiarism Free
100% Original.
Expert Tutors
Masters & PhDs.
100% Confidential
Your privacy matters.
On-Time Delivery
Never miss a deadline.
Read the code for the drawTree method so that you understand what it's doing
Read the code for the drawTree method so that you understand what it's doing. Modify the program so the tree looks more realistic. Try using some Math.random statments to help you make it look more natural.
Feel free to change the following:
this.setSize(800, 500); // line 11
drawTree(g2D, 300, 500, -90, 6, 5); // line 18
and the code in the drawTree method.
Main.java
import java.awt.*;
import javax.swing.*;
class Main extends JFrame {
private Canvas canvas;
public Main() {
super("Tree");
this.setSize(800, 500);
this.setVisible(true);
canvas = new Canvas() {
@Override
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
drawTree(g2D, 300, 500, -90, 6, 5);
}
};
this.add(canvas);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void drawTree(Graphics2D g, int x1, int y1, double angle, int depth, int width) {
if (depth == 0) {
return;
}
if (width < 1) {
width = 1;
}
g.setStroke(new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g.setColor(new Color((int) (Math.random() * 256), (int) (Math.random() * 256), (int) (Math.random() * 256)));
int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 16.0);
int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 16.0);
g.drawLine(x1, y1, x2, y2);
drawTree(g, x2, y2, angle - 30, depth - 1, width);
drawTree(g, x2, y2, angle + 30, depth - 1, width);
}
public static void main(String args[]) {
new Main();
}
}
Expert Solution
Need this Answer?
This solution is not in the archive yet. Hire an expert to solve it for you.





