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) {
Conceptually, if each node should have a specific color then each Node
instance should have a Color
attribute. In the example cited here, the class Node
has a number of static updateXxx()
methods that traverse the program's (simpler) model, updating nodes as specified. In particular, updateColor()
sets each element's Color
field to the specified color
. Your implementation of paintComponent()
can do something similar.
Addendum: As @MadP comments, javax.swing.Timer is well suited to periodic GUI updates, as the timer's actionPerformed()
method is executed on the EDT. In this example, the model is updated with each invocation, and the new state is rendered when repaint()
(indirectly) calls paintComponent()
.