英文:
Java GUI draw tree using random number
问题
如何利用“随机数”使这棵树的角度和深度随机变化?
在下面的代码中使用了JFrame。问题的目的是要了解如何在paint方法中传递随机角度和深度的随机化思路。
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 depth) {
if(depth==0)
return;
// 通过随机数调整角度和深度
double randomAngle = Math.random() * 40 - 20; // 在-20到20之间生成随机角度
int randomDepth = (int) (Math.random() * 5) + 1; // 在1到5之间生成随机深度
int x2 = x1 + (int) (Math.cos(Math.toRadians(angle + randomAngle)) * depth * 10.0);
int y2 = y1 + (int) (Math.sin(Math.toRadians(angle + randomAngle)) * depth * 10.0);
g.drawLine(x1, y1, x2, y2);
drawTree(g, x2, y2, angle - randomAngle, depth - randomDepth);
drawTree(g, x2, y2, angle + randomAngle, depth - randomDepth);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.BLACK);
drawTree(g, 400, 600, -90, 10);
}
public static void main(String[] args) {
new DrawTreeFrame();
}
}
英文:
How can I make this tree's angle and depth to be random by using 'random number'?
In the below code JFrame is been used. The intent behind the question is to get the idea of randomizing the angles and the depth, which is passed in the paint method.
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 depth) {
if(depth==0)
return;
int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 10.0);
int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 10.0);
g.drawLine(x1, y1, x2, y2);
drawTree(g, x2, y2, angle-20, depth-1);
drawTree(g, x2, y2, angle+20, depth-1);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.BLACK);
drawTree(g, 400, 600, -90, 10);
}
public static void main(String[] args) {
new DrawTreeFrame();
}
}
答案1
得分: 2
你可以使用 Math.random()
。以下代码方法将为您提供在范围内的随机数;
public int getRandomNumber(int min, int max) {
return (int) ((Math.random() * (max - min)) + min);
}
最终您的代码应该如下所示:
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 depth) {
if(depth==0)
return;
int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 10.0);
int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 10.0);
g.drawLine(x1, y1, x2, y2);
drawTree(g, x2, y2, angle-20, depth-1);
drawTree(g, x2, y2, angle+20, depth-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, -100);
int depth = getRandomNumber(5, 20);
drawTree(g, x1, y1, angle, depth);
}
public int getRandomNumber(int min, int max) {
return (int) ((Math.random() * (max - min)) + min);
}
public static void main(String[] args) {
new DrawTreeFrame();
}
}
英文:
You can use Math.random()
. The following code method will give you random numbers in a range;
public int getRandomNumber(int min, int max) {
return (int) ((Math.random() * (max - min)) + min);
}
Ultimately your code should look like this:-
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 depth) {
if(depth==0)
return;
int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 10.0);
int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 10.0);
g.drawLine(x1, y1, x2, y2);
drawTree(g, x2, y2, angle-20, depth-1);
drawTree(g, x2, y2, angle+20, depth-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, -100);
int depth = getRandomNumber(5, 20);
drawTree(g, x1, y1, angle, depth);
}
public int getRandomNumber(int min, int max) {
return (int) ((Math.random() * (max - min)) + min);
}
public static void main(String[] args) {
new DrawTreeFrame();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论