i have the following code, taken partly from a Red Black Tree Java implementation. This is the gui part. I am really bad at Java Graphics and i was hoping to get some help regarding this matter..
public void paintComponent(final Graphics g) {
super.paintComponent(g);
if (tree == null) {
return;
}
tree.traverseInorder(new Inter.Visitor() {
private int x = gridwidth;
public void visit(LBTN node) {
coordinates.put(node, new Point(x, gridheight * (depth(node)+1)));
x += gridwidth;
}
});
tree.traversePostorder(new Inter.Visitor() {
public void visit(LBTN node) {
String data = node.getinfo().toString();
Point center = (Point)coordinates.get(node);
if (node.getParent() != null) {
Point parentPoint = (Point)coordinates.get(node.getParent());
g.setColor(Color.black);
g.drawLine(center.x, center.y, parentPoint.x, parentPoint.y);
}
FontMetrics fm = g.getFontMetrics();
Rectangle r = fm.getStringBounds(data, g).getBounds();
r.setLocation(center.x - r.width/2, center.y - r.height/2);
Color color = getNodeColor(node);
Color textColor =
(color.getRed() + color.getBlue() + color.getGreen() < 382)
? Color.white
: Color.black;
g.setColor(color);
g.fillRect(r.x - 2 , r.y - 2, r.width + 4, r.height + 4);
g.setColor(textColor);
g.drawString(data, r.x, r.y + r.height);
}
});
}
As you can see, from the bottom codes part, a rectangle is being drawn for the red black tree nodes. I want to change that to ellipses while still making the program functional, i.e, still using the values that are assigned to the rectangle. I want to be able to create ellipses based on the same values
Any help will be much appreciated. Regards
I think replacing this,
with this,
should work.
http://docs.oracle.com/javase/6/docs/api/java/awt/Graphics.html#fillOval%28int,%20int,%20int,%20int%29