递归树GUI

时间:2014-02-18 02:07:28

标签: java user-interface recursion tree

我是这个网站的新手,所以请原谅我的noobish方式哈哈。

我正在尝试在GUI树上创建更多的芽。

在过去的3天里,我一直在努力做到这一点,但每次我改变一些东西都会破坏整个画面! 代码为:

public class RecursiveTreeExample extends JPanel {

    private DrawTurtle t;
    private static final int width = 400;
    private static final int height = 400;

    public RecursiveTreeExample() {
        super.setPreferredSize(new Dimension(width,height));
        t = new DrawTurtle(width, height);
        draw();
    }
    void draw() {
        int s = 100;
        t.back(s);
        embeddedSample(s, 5);
    }

    void embeddedSample(int size, int iter) {
          if (size > 0 && iter > 0) {

            t.forward(size);
            t.right(45);
            embeddedSample(size/2, iter-1);
            t.left(90);
            embeddedSample(size/2, iter-1);
            t.right(45);
                embeddedSample(size/4, iter+3);                    
            t.back(size);
          }
        }

    public void paintComponent(Graphics page) {
        super.paintComponent(page);
        t.draw(page);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame ("Turtle");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

        JPanel turtle = new RecursiveTreeExample();

        frame.getContentPane().add(turtle);

        frame.pack();
        frame.setVisible(true);
    }

}

0 个答案:

没有答案