Java 初学者swing递归

Java 初学者swing递归,java,swing,recursion,Java,Swing,Recursion,第一次在这里发布。这项编程作业让我难堪,代码如下: import java.awt.*; import javax.swing.*; public class HTree extends JPanel { private Color color = new Color((int)(Math.random() * 256), (int)(Math.random() * 256), (int)(Math.random() * 256)); public void draw(Graphics g

第一次在这里发布。这项编程作业让我难堪,代码如下:

import java.awt.*;
import javax.swing.*;

public class HTree extends JPanel {

private Color color = new Color((int)(Math.random() * 256), (int)(Math.random() * 256), (int)(Math.random() * 256));

public void draw(Graphics g, int n, double sz, double x, double y) {
    if (n == 0) return; 
    double x0 = x - sz/2, x1 = x + sz/2;
    double y0 = y - sz/2, y1 = y + sz/2;
    // draw the 3 line segments of the H  
    g.setColor(color);
    g.drawLine((int)x0, (int)y, (int)x1, (int)y);
    g.drawLine((int)x0, (int)y0, (int)x0, (int)y1);
    g.drawLine((int)x1, (int)y0, (int)x1, (int)y1);
    // recursively draw 4 half-size
    // H-trees of order n-1
    g.setColor(color);
    draw(g, n-1, sz/2, x0, y0);
    draw(g, n-1, sz/2, x0, y1);
    draw(g, n-1, sz/2, x1, y0);
    draw(g, n-1, sz/2, x1, y1);
    repaint();
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    draw(g, 3, .5, .5, .5);
}

public static void main(String[] args) {
    HTree h = new HTree();
    JFrame application = new JFrame("HTree");
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    application.add(h);
    application.setSize(1000, 1000);
    application.setVisible(true);
}
}
它编译正确,但在运行程序时,我得到的只是一个空JFrame。我对swing不太熟悉,但我认为问题在于要么HTree构造函数错误,要么我可能需要在main中调用draw(),但不确定如何使用图形对象调用它。非常感谢您的帮助,谢谢

一些观察结果:

  • 使用相对于面板当前大小的整数坐标

  • 替代以建立初始几何图形

  • 使用
    Color.getHSBColor()
    获得明亮、饱和的颜色;考虑创建一个色域,在每个级别选择不同的颜色。
  • 仅在上构造和操作Swing GUI对象

  • 考虑使用
    JSpinner
    JSlider
    以交互方式控制递归深度,如建议和所示

  • 不要递归调用
    repaint()
    ;使用它来计划对
    paintComponent()
    实现的调用,就像当递归深度响应某些输入时一样

  • 考虑使用
    setStroke()
    和呈现提示,如图所示

  • 考虑使用,每次调用
    ActionListener
    时,将递归深度增加一级


谢谢到目前为止:仍然不确定如何从args[0]传递递归次数,但使用了用户输入实例变量。我能够使用数组将每个递归设置为随机颜色,并使用g.fillRect.Excellent使线条变粗;我在上面又添加了一些建议。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * @see https://stackoverflow.com/a/37450393/230513
 */
public class HTree {

    private void display() {
        JFrame f = new JFrame("HTree");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new Tree());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private static class Tree extends JPanel {

        private final Color color = Color.getHSBColor((float) Math.random(), 1, 1);

        public void draw(Graphics g, int n, double sz, double x, double y) {
            if (n == 0) {
                return;
            }
            double x0 = x - sz / 2, x1 = x + sz / 2;
            double y0 = y - sz / 2, y1 = y + sz / 2;
            // draw the 3 line segments of the H 
            g.setColor(color);
            g.drawLine((int) x0, (int) y, (int) x1, (int) y);
            g.drawLine((int) x0, (int) y0, (int) x0, (int) y1);
            g.drawLine((int) x1, (int) y0, (int) x1, (int) y1);
            // recursively draw 4 half-size
            // H-trees of order n-1
            g.setColor(color);
            draw(g, n - 1, sz / 2, x0, y0);
            draw(g, n - 1, sz / 2, x0, y1);
            draw(g, n - 1, sz / 2, x1, y0);
            draw(g, n - 1, sz / 2, x1, y1);
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            draw(g, 3, getWidth() / 2, getWidth() / 2, getHeight() / 2);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(500, 500);
        }

        public static void main(String[] args) {
            EventQueue.invokeLater(new HTree()::display);
        }
    }
}