drawing squares using a recursive method with java

I am trying to write an applet that draws a main square and then uses a recursive method that draws smaller squares on the corners of the main square. I'm really confused on how to go about this. I have drawn the square with the others squares on its corners but i need to do this process recursively and thats where i get lost. I need to set a min side length so the recursive method knows when to stop. Here is my code.

 import javax.swing.JApplet;

 import java.awt.*;
public class LabC extends JApplet
{
    public void paint(Graphics g)
    {
    g.drawRect(50, 100, 100, 100);
    g.drawRect(25, 75, 50, 50);
    g.drawRect(125, 75, 50, 50);
    g.drawRect(125, 175, 50, 50);
    g.drawRect(25, 175, 50, 50);


}
}

I think this is more or less what are you looking for

    public static class LabC extends JLabel {

    public void paintRecursiveWraper(Graphics g, int minW, int minH, int x, int y, int w, int h) {
        g.drawRect(x, y, w, h);
        paintRecusive(g, minW, minH, x, y, w, h);
    }

    public void paintRecusive(Graphics g, int minW, int minH, int x, int y, int w, int h) {
        if (h <= minH || w <= minW) {
            return;
        }

        int newW, newH;
        int newX, newY;
        newW = (int) (w * scaleFactor);
        newH = (int) (h * scaleFactor);

        // Bot Left Square
        newX = x;
        newY = y;
        g.drawRect(newX, newY, newW, newH);
        paintRecusive(g, minW, minH, newX, newY, newW, newH);
        // Bot Right Square
        newX = (int) (x + w * (1 - scaleFactor));
        newY = y;
        g.drawRect(newX, newY, newW, newH);
        paintRecusive(g, minW, minH, newX, newY, newW, newH);
        // Top Left Square
        newX = x;
        newY = (int) (y + h * (1 - scaleFactor));
        g.drawRect(newX, newY, newW, newH);
        paintRecusive(g, minW, minH, newX, newY, newW, newH);
        // Top Right Square
        newX = (int) (x + w * (1 - scaleFactor));
        newY = (int) (y + h * (1 - scaleFactor));
        g.drawRect(newX, newY, newW, newH);
        paintRecusive(g, minW, minH, newX, newY, newW, newH);
    }

    public void paint(Graphics g) {
        paintRecursiveWraper(g, 10, 10, 0, 0, 1000, 1000);

    }
}

scaleFactor has to be between 0 and 1

Edited*

链接地址: http://www.djcxy.com/p/80702.html

上一篇: 整数平方和

下一篇: 使用java的递归方法绘制正方形