I want to change the code to randomize this tree's angle and length like this photo by using Random Number. Please help me! 
public class DrawTreeFrame extends JFrame {
public DrawTreeFrame() {
setSize(800, 700);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private void drawTree(Graphics g, int x1, int y1, double angle, int length) {
if(length==0)
return;
int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * length * 10.0);
int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * length * 10.0);
g.drawLine(x1, y1, x2, y2);
drawTree(g, x2, y2, angle-20, length-1);
drawTree(g, x2, y2, angle+20, length-1);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.BLACK);
int x1 = getRandomNumber(100, 400);
int y1 = getRandomNumber(400, 800);
double angle = getRandomNumber(-10, 70);
int length = getRandomNumber(5, 15);
drawTree(g, x1, y1, angle, length);
}
public int getRandomNumber(int min, int max) {
return (int) ((Math.random() * (max - min)) + min);
}
public static void main(String[] args) {
new DrawTreeFrame();
}
}
Aucun commentaire:
Enregistrer un commentaire