java - 基于定时器遍历树

标签 java algorithm swing

我正在尝试使用 timerpaintComponent 将树(BFS 和 DFS)遍历到 JPanel 的动画...有点像这样.. .

enter image description here

现在,BFS 算法只是立即循环遍历所有节点并将访问过的节点绘制为青色...但我想让人们看到树是如何被遍历的...逐个节点...所以我'我试图添加一个计时器来延迟下一次 while 循环 迭代运行...它根本不起作用...

计时器:

public void timer() {
    int initialDelay = 1000;
    timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
            if (cancelTimer) {
                timer.cancel();
            }   
            if (counter == 3) {
                //reset 
                counter = 0;
            }
            if (counter < 3) {
                ++counter;
                System.out.println(counter);
            }       
        }
    }, initialDelay, 1000); 
}

paintComponent:在遍历节点时重新绘制节点

public void paintComponent(Graphics g) {
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, width, height);

    g.setColor(rootNode.getColor());
    g.fillRect(rootNode.getX(), rootNode.getY(), rootNode.getWidth(), rootNode.getHeight());

    g.setColor(Color.WHITE);
    g.drawString(rootNode.getValue(), rootNode.getX()+9, rootNode.getY()+16);
    paintComponent(g, rootNode);    
}

public void paintComponent(Graphics g, Nodes parentNode) {  
    //keep generating new nodePrintList to load with new Children
    ArrayList<Nodes> nodePrintList = new ArrayList<Nodes>();    

    //base case: end of nodeList
    if (nodeList.indexOf(parentNode)==nodeList.size()-1) {
        System.out.println("\nend");
    }
    else {  
    //traverse nodeList recursively 
        nodePrintList = getChildren(parentNode);    
        //loop through and print all children of node n
        //System.out.println();
        int x = parentNode.getX()-50;

        for (Nodes child : nodePrintList) {             
            g.setColor(child.getColor());
            child.setX(x);
            child.setY(parentNode.getY()+50);
            g.fillRect(child.getX(), child.getY(), child.getWidth(), child.getHeight());        
            g.setColor(Color.WHITE);
            g.drawString(child.getValue(), child.getX()+9, child.getY()+16);
            x+=50;
            //System.out.print("PARENT: " + parentNode.getValue() + " | x,y: " + parentNode.getX() + ", " + parentNode.getY() + "...\n CHILD: " + child.getValue() + " | x,y: " + child.getX() + ", " + child.getY());  
            paintComponent(g, child);
            g.drawLine(parentNode.getX()+10, parentNode.getY()+23, child.getX()+10, child.getY());
        }           
    }
    repaint();

}

BFS():

public void bfs() {

    Queue q = new LinkedList();
    q.add(rootNode);
    rootNode.visited(true);
    rootNode.setColor(Color.cyan);
    printNode(rootNode);
    //only perform check when counter = 10;
    while (!q.isEmpty()) {          
        Nodes n = (Nodes)q.remove();
        Nodes child = null;
        //put all unvisited children in the queue
        while ((child = getUnvisitedChildNode(n)) != null)
        {           
            if (counter == 3) {
                child.visited(true);
                printNode(child);
                q.add(child);
                child.setColor(Color.cyan); 
            }
        }
    }
    if (q.isEmpty()) {
        cancelTimer = true;
        //RepaintManager.currentManager(this).markCompletelyClean(this);
    }
}

有什么想法吗?谢谢!

最佳答案

例如,您可以创建一个 Queue<Nodes>它将接受用于绘画的节点。即,在您的 bfs() 中方法,您可以在其中设置颜色 child.setColor(Color.cyan);将此节点添加到 Queue 。所以:

if (counter == 3) {
    child.visited(true);
    printNode(child);
    q.add(child);
    paintQueue.add(child);
}

在计时器中,以固定延迟,poll这个队列并绘制节点:

timer.scheduleAtFixedRate(new TimerTask() {
    public void run() {
        if (!paintQueue.isEmpty()) {
            Nodes node= paintQueue.poll();
            node.setColor(Color.cyan);
        }     
    }
}, initialDelay, 1000);

关于java - 基于定时器遍历树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17218122/

相关文章:

Java 代理 - 无法正确交换来自 HTTP GET/POST 请求的数据

Java 放置俄罗斯方 block 像 block 算法

java - 从不同的类动态添加文本框和 JSlider

java - PlayFramework 2.4 和 IntelliJ 14

java - Java中BigDecimal转Double时什么时候出现 "data loss"

c++ - 输出无序,使用 MPI 进行并行编程

arrays - 给定一个集合 S,找出其和 <= k 的所有最大子集

java - 根据调用的选项卡更新组件

JavaFX - 从 SwingNode 中删除元素

java - 将数组列表传递给 fragment ?