用Java递归绘制图形

用Java递归绘制图形,java,recursion,graphics,graphics2d,Java,Recursion,Graphics,Graphics2d,对于这个程序,我需要递归地绘制一个“宝塔”,它是一系列递减的矩形,中心对齐,堆叠在一起。我想我已经了解了实际图形背后的逻辑,但我很难弄清楚如何用图形2D将图形绘制成矩形。我试图把它塞进一个基本的图形绘制程序中,却找不到如何在其中使用递归 这是我到目前为止编写的代码,没有考虑图形: import java.awt.Rectangle; public class PagodaDrawer { private int initialY; //Top of the bottom rectangle

对于这个程序,我需要递归地绘制一个“宝塔”,它是一系列递减的矩形,中心对齐,堆叠在一起。我想我已经了解了实际图形背后的逻辑,但我很难弄清楚如何用图形2D将图形绘制成矩形。我试图把它塞进一个基本的图形绘制程序中,却找不到如何在其中使用递归

这是我到目前为止编写的代码,没有考虑图形:

import java.awt.Rectangle;


public class PagodaDrawer
{

private int initialY; //Top of the bottom rectangle
private int initialHeight; //Height for the bottom rectangle
private double scale; //Amount to reduce each layer


public PagodaDrawer(int initialY, int initialHeight, double scaleFactor)
{
    this.initialY = initialY;
    this.initialHeight = initialHeight;
    scale = scaleFactor;
}

public void drawPagoda()
{
    drawLayer(0, initialY, 2 * initialHeight, initialHeight);
}

public void drawLayer(double x, double y, double width, double height)
{
    if(y < 0 || height < 5) //If off the top of the screen, or less than 5 tall
    {
        return;
    }
    drawLayer(x - (((1 - scale)* x) / 2), y + (y * scale), width * scale, height * scale );
    Rectangle r = new Rectangle((int)x, (int)y, (int)(2 * height), (int)height);
    //Draw r?
}
}

编写一个递归的
createRectangle()
,将每个新的
Rectangle
实例添加到
列表中,而不是使
drawLayer()递归。在Java AWT和Swing中,在您的实现中呈现列表,如图所示。

使用
图形
/
图形2D
方法绘制。
示例:
graphics.fillRect(x,y,w,h)

您应该从要绘制的组件(通常是主框架或某个组件)获取
图形(?:2d)
对象

在框架的
paintComponent()
中调用图形应该可以像这样工作:

以下是Java6文档:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class PagodaDrawer extends JPanel
{
private int initialX;
private int initialY; //Top of the bottom rectangle
private int initialHeight; //Height for the bottom rectangle
private double scale; //Amount to reduce each layer
private boolean isRenderable;
private ArrayList<Rectangle> recs;


public PagodaDrawer(int initialX, int initialY, int initialHeight, double scaleFactor)
{
    this.initialX = initialX;
    this.initialY = initialY;
    this.initialHeight = initialHeight;
    scale = scaleFactor;
    isRenderable = false;
    recs = new ArrayList<Rectangle>();
}

public void drawPagoda()
{
    drawLayer(initialX, initialY, 2 * initialHeight, initialHeight);
}

public void drawLayer(double x, double y, double width, double height)
{
    if(y < 0 || height < 5) //If off the top of the screen, or less than 5 tall
    {
        isRenderable = true;
        return;
    }
    drawLayer(x + .5 * (width - (width * scale)), y - (height * scale), width * scale, height * scale );
    Rectangle r = new Rectangle((int)x, (int)y, (int)(2 * height), (int)height);
    recs.add(r);
}

public void paintComponent(Graphics g)
{
    if(!isRenderable)
        return;
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    for(int i = 0; i < recs.size(); i++)
    {
        g2.draw(recs.get(i));
        System.out.println(recs.get(i));
    }
}
}
import javax.swing.JFrame;
import javax.swing.JPanel;



public class DisplayComponent extends JFrame
{
private static final long serialVersionUID = -4279682826771265863L;
private static final int FRAME_WIDTH = 500;
private static final int FRAME_HEIGHT = 500;

private JPanel panel;
private PagodaDrawer p;

public DisplayComponent(int initialHeight, double scaleFactor)
{
    p = new PagodaDrawer(FRAME_WIDTH / 2, FRAME_HEIGHT, initialHeight, scaleFactor);
    panel = new JPanel();
    p.drawPagoda();
    add(p);

    pack();



    setSize(FRAME_WIDTH, FRAME_HEIGHT);
    setVisible(true);
}
}