I already have implemented display of binary search tree . Here's the code , which paints the binary tree in a jpanel .
public void paint(Graphics g) {
super.paint(g);
System.out.println(" in paint");
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int num = bst.size;
int y = 25;
int nodes = 1;
int level = 1;
int length = getWidth();
Queue<Node> q = new LinkedList<Node>();
Queue<Integer> q2 = new LinkedList<Integer>();
q.add(bst.root);
while (num > 0) {
int pX = (int) Math.round(length / (2.0 * nodes));
int x = pX;
for (int i = 0; i < nodes; i++) {
Node n = q.poll();
//
if (n != null) {
num--;
System.out.println(x);
g2.setColor(Color.BLUE);
String str = n.value + "";
System.out.println(str);
//Font f = Font.getFont(str);
int width = str.length();
g2.setColor(Color.YELLOW);
g2.fillOval(x, y, (30 - 2 * level)+width*3, (30 - 2 * level));
g2.setColor(Color.black);
g2.drawString(n.value + "", x + 10 - level, y + 15);
g2.setColor(Color.black);
if (n.left == null)
q.add(null);
else
q.add(n.left);
if (n.right == null)
q.add(null);
else
q.add(n.right);
if (level != 1) {
int xx = q2.poll();
int yy = q2.poll();
g2.drawLine(xx+width*2, yy, x + (15 - 1 * level)+width*2, y);
}
} else {
q2.poll();
q2.poll();
q.add(null);
q.add(null);
}
q2.add(x);
q2.add(y + 15 - level);
q2.add(x + 30 - 2 * level);
q2.add(y + 15 - level);
x += 2 * pX;
}
y += 40;
nodes = 1 << level;
level++;
}
Now as i insert nodes into my tree , I want the parent nodes to change color of the new node progressively , and then ultimate join as a child . or the new node to be inserted moves along the path of it's parent . or something similar Here's an example :
I have no idea how to accomplish that , with timer or likewise .
Conceptually, if each node should have a specific color then each
Node
instance should have aColor
attribute. In the example cited here, theclass Node
has a number of staticupdateXxx()
methods that traverse the program's (simpler) model, updating nodes as specified. In particular,updateColor()
sets each element'sColor
field to the specifiedcolor
. Your implementation ofpaintComponent()
can do something similar.Addendum: As @MadP comments,
javax.swing.Timer
is well suited to periodic GUI updates, as the timer'sactionPerformed()
method is executed on the EDT. In this example, the model is updated with each invocation, and the new state is rendered whenrepaint()
(indirectly) callspaintComponent()
.Okay, this took a little longer then I wanted (10 month olds don't have any patience)
The basic concept revolves around the idea that you need to change from one state to another over a period of time.
Given a start time and the current time, we can calculate the amount of time that the animation has been running, and given the total animation time, the current progress.
With this (and some clever maths) we can calculate the current state from our start state towards our target state.
I've done movement as well, so that may be a little over kill, but the basic premise remains the same.
I place stateful information about the nodes that need to change in animation properties class and use a
javax.swing.Timer
to tick over the animation (at a reasonably steady rate). I then update the state of each node as required and repaint the screen.Update with simple example ;)
Okay, this is a simple example. Basically, it just blinks the node...