[英]Recursively drawing Graphics in Java
對於這個程序,我需要遞歸繪制一個'寶塔',這是一系列漸弱的矩形,在中央對齊,堆疊在一起。 我想我已經得到了實際數字背后的邏輯,但是我無法弄清楚如何將圖形繪制為帶有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()
說明在這里 。
在Java AWT和Swing中,您使用Graphics
/ Graphics2D
方法繪制。
示例: graphics.fillRect(x, y, w, h);
您應該從要繪制的組件中獲取graphics(?:2d)
對象,通常是主框架或某個組件。
在框架的paintComponent()
中調用繪圖應該像這樣工作:
如何在Java中使用paintComponent來繪制多個東西,但是旋轉一個?
這是Java6文檔: http : //docs.oracle.com/javase/6/docs/api/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.