Animate Change of color of nodes in insertion of binary search tree

前端 未结 2 1264
长情又很酷
长情又很酷 2020-12-10 20:11

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) {
           


        
2条回答
  •  轮回少年
    2020-12-10 21:00

    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().

提交回复
热议问题