如何将代码转换为递归


import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class DrawIt
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        final int width = 400;
        final int height = 400;
        frame.setSize(width,height);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JComponent component = new JComponent()

        {
            public void paintComponent(Graphics graph){
                draw(graph);
            }
        };
        frame.add(component);
        frame.setVisible(true);
    }
    public static void draw(Graphics g)
    {
        int  x1=100;
        int y1 = 100;
        int length = 10;
        for(int i=0;i<=10;i++)
        {
            int x2 = x1 + length;
            int y2 = y1;
            g.drawLine(x1,y1,x2,y2);
            x1=x2;
            y1=y2;
            y2=y1-length;
            g.drawLine(x1,y1,x2,y2);
            x1=x2;
            y1=y2;
            length+=10;
            x2=x1-length;
            g.drawLine(x1, y1, x2, y2);
            x1=x2;
            y1=y2;
            y2=y1+length;
            g.drawLine(x1, y1, x2, y2);
            x1=x2;
            y1=y2;
            length+=10;
        }
    }
}

如何将其转换为递归?这给了我矩形螺旋而不是递归的方式。帮助。如果这样我使用 4 个带有递归的变量,它会怎么样?

public class DrawIt {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        final int width = 400;
        final int height = 400;
        frame.setSize(width, height);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JComponent component = new JComponent() {
            public void paintComponent(Graphics graph) {
                draw(graph);
            }
        };
        frame.add(component);
        frame.setVisible(true);
    }
    public static void drawpuzzle(Graphics g, int x1, int y1, int length, int count) {
        if (count> 0) {
            int x2 = x1 + length;
            int y2 = y1;
            g.drawLine(x1, y1, x2, y2);
            x1 = x2;
            y1 = y2;
            y2 = y1 - length;
            g.drawLine(x1, y1, x2, y2);
            x1 = x2;
            y1 = y2;
            length += 10;
            x2 = x1 - length;
            g.drawLine(x1, y1, x2, y2);
            x1 = x2;
            y1 = y2;
            y2 = y1 + length;
            g.drawLine(x1, y1, x2, y2);
            x1 = x2;
            y1 = y2;
            length += 10;
            drawpuzzle(g, x1, y1, length, count - 1);
        }
    }
    public static void draw(Graphics g) {
        int x1 = 100;
        int y1 = 100;
        int length = 10;
        int count = 10;
        drawpuzzle(g, x1, y1, length, count);
    }
}

相关内容

  • 没有找到相关文章

最新更新