ノードのリストから階層化されたツリーを手動で描画しようとしています。各ノードは、メソッドに含まれる情報を持つオブジェクトです。次に例を示します。
node.getParent()
node.getChildrenCount()
私が立ち往生した問題は、ツリーのピラミッド構造を描画することです (子ノードを正しくインデントします)。ルートは中央の上にあり、子は対称的に下に広がります。
private void drawTree(Graphics2D graphics) {
int width = 110;
int height = 40;
int y = 10;
for (int i = 0, nodesSize = nodes.size(); i < nodesSize; i++) {
AttributedNode node = nodes.get(i);
Rectangle rectangle;
if (i == 1) { // draw root
rectangle = new Rectangle(getRootX(), y, width, height);
} else {
if (node.getChildCount() == 1) { // if one child draw beneath
rectangle = new Rectangle(getRootX(), y, width, height);
} else {
rectangle = new Rectangle(getRootX() + 40, y, width, height);
}
}
y += 50;
graphics.draw(rectangle);
addStringToRectangle(graphics, rectangle, node.getText());
}
}
私がこれまでに持っているもの: http://img10.imageshack.us/img10/8822/rcsi.png
そして私が達成したいこと
: http://img703.imageshack.us/img703/8416/1o05.png
感謝されます。