I am trying to manually draw a tiered tree from a list of nodes. Each node is an object that has information contained in methods, for example:
node.getParent()
node.getChildrenCount()
The problem I got stuck with is drawing a pyramid structure of the tree (correctly indenting the children nodes), with the root being on top in the middle, and children spreading downwards symmetrically.
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());
}
}
What I have so far: http://img10.imageshack.us/img10/8822/rcsi.png
And what I want to achieve: http://img703.imageshack.us/img703/8416/1o05.png
Any help would be appreciated.