gpt4 book ai didi

java - 在Java中递归绘制图形

转载 作者:行者123 更新时间:2023-11-29 05:33:31 26 4
gpt4 key购买 nike

对于这个程序,我需要递归地绘制一个“宝塔”,它是一系列递减的矩形,居中对齐,彼此堆叠。我想我已经了解了实际图形背后的逻辑,但我无法弄清楚如何使用 Graphics2D 将图形实际绘制为矩形。我试图将它硬塞进一个基本的形状绘图程序中,但找不到如何在其中进行递归。

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

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?
}
}

如何在框架中递归绘制图形的图层?

编辑:

对于任何感兴趣的人,这是最终代码

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));
}
}
}

加上这个 JFrame:

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);
}
}

最佳答案

而不是制作 drawLayer()递归,写一个递归createRectangle()添加每个新的 Rectangle实例到 List<Rectangle> .在您的 paintComponent() 实现中呈现列表,图文并茂here .

关于java - 在Java中递归绘制图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20295935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com