基于计时器的遍历树

时间:2013-06-20 15:46:58

标签: java algorithm swing

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

enter image description here

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

有什么想法?谢谢!

1 个答案:

答案 0 :(得分:3)

例如,您可以创建一个Queue<Nodes>来接受绘画节点。也就是说,在您设置颜色bfs()的{​​{1}}方法中,将此节点添加到child.setColor(Color.cyan);。所以:

Queue

在定时器中,以固定延迟,if (counter == 3) { child.visited(true); printNode(child); q.add(child); paintQueue.add(child); } 此队列并绘制节点:

poll