2

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

在此处输入图像描述

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

定时器:

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);
    }
}

有什么想法吗?谢谢!

4

1 回答 1

3

例如,您可以创建一个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);
于 2013-06-20T15:53:27.337 回答