我试图使用递归绘制树.树需要看起来像这样:

我应该怎么做的简短摘要:
length和宽度width这是我如何解决这个问题.我想画出树干,左分支和右分支.我设法做到了这一点,具有以下功能:
public void drawTree(double length, double min_length, double width, double alpha) {
//Draws the trunk of the tree
StdDraw.setPenRadius(width);
StdDraw.line(250, 150, 250, 150+length);
//Left branch
double hypotenuse = (3.0/4.0)*length;
double opposite = Math.sin(alpha) * hypotenuse;
double adjacent = Math.sqrt(Math.pow(hypotenuse, 2)-Math.pow(opposite, 2));
StdDraw.setPenRadius(width*3.0/4.0);
StdDraw.line(250,150+length,250-adjacent,150+length+opposite);
//Right branch
double hypotenuse2 = (2.0/3.0)*length;
double opposite2 = Math.sin(alpha) * hypotenuse2;
double adjacent2 = Math.sqrt(Math.pow(hypotenuse2, 2)-Math.pow(opposite2, 2));
StdDraw.setPenRadius(width*1.0/2.0);
StdDraw.line(250,150+length,250+adjacent2,150+length+opposite2);
}
Run Code Online (Sandbox Code Playgroud)
这是输出,它就像我希望的那样:

我认为剩下的很容易,但我在最近3个小时内没有取得任何进展:/.我包含了停止条件的if语句.但我对递归部分一无所知.我试过这个:
if (length > …Run Code Online (Sandbox Code Playgroud) 编辑:IntelliJ CE
我想要的:能够写作
setCanvas(500,500);
Run Code Online (Sandbox Code Playgroud)
代替
StdDraw.setcanvas(500,500);
Run Code Online (Sandbox Code Playgroud)
问题:我不知道如何正确导入 Stddraw 库。如果我只是这样做
import StdDraw;
Run Code Online (Sandbox Code Playgroud)
IntelliJ 告诉我“StdDraw”符号无法解析。如果我将其注释掉,我可以从 StdDraw 调用方法,但我必须编写 StdDraw.setcanvas(500,500);
StdDraw.java 与 Solver.java 位于同一目录中。
代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
// import StdDraw;//StdDraw is in the same directory as Solver
public class Solver {
public static void main(String[] args) {
System.out.println("Solver main is running.");
StdDraw.setCanvasSize(500, 500);
StdDraw.setPenColor(StdDraw.RED);
StdDraw.filledRectangle(0,0,10,10);
}
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试过: - 确保 Stddraw.java 与我正在编译和运行的文件位于同一目录中 - 查看http://introcs.cs.princeton.edu/java/stdlib/javadoc/StdDraw.html - 搜索完整的代码示例,即。显示如何导入库的代码 - 搜索 YouTube 教程 - 阅读https://www.jetbrains.com/idea/help/library.html - 在 StdDraw 前面添加一些东西,例如。stblib.StdDraw
我试图用正方形填充整个屏幕,每个正方形填充不同的颜色.我能够生成整个正方形的屏幕,但我不能让它们成为随机颜色.这是我到目前为止:
import java.util.Random;
public class RGBRandom
{
public static void main(String[] args)
{
StdDraw.setScale(0, 100);
for (int x = 0; x <= 100; x++)
{
for (int y = 0; y <= 100; y++)
{
int r = (int)Math.random()*256;
int g = (int)Math.random()*256;
int b = (int)Math.random()*256;
StdDraw.setPenColor(r, g, b);
StdDraw.filledSquare(x, y, 0.5);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)