簡體   English   中英

Java BFS算法-嘗試在JPanel上繪制節點

[英]Java BFS Algorithm - Trying to paint nodes on JPanel

我剛剛實現了BFS和DFS算法。

我的最終目標是將算法動畫化到JPanel上...

但是首先,我想以各自的父子關系將節點繪制到屏幕上:

在此處輸入圖片說明

到目前為止,我已經能夠實現:

在此處輸入圖片說明

我的繪畫成分如下:

public void paintComponent(Graphics g) {        
    ArrayList<Nodes> nodePrintList = new ArrayList<Nodes>();    
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, width, height);

    int x = 50, y = 50;

    //if currentNode has no more children, move to next node
    g.setColor(Color.GREEN);
    g.fillRect(x, y, getRootNode().getWidth(), getRootNode().getHeight());

    g.setColor(Color.BLACK);
    g.drawString(getRootNode().getValue(),x+9, y+16);

    nodePrintList = getChildren(rootNode);  
    x-=30;  
    for (Nodes n : nodePrintList) {     
        System.out.println("\nChildren of " + rootNode.getValue() + ": " + n.getValue());
        g.setColor(Color.BLUE);
        g.fillRect(x, y+30, n.getWidth(), n.getHeight());
        g.setColor(Color.WHITE);
        g.drawString(n.getValue(),x+9, y+45);
        x+=30;
    }

}

通過調用getChildren(Nodes n)來獲取該父級列表中的當前子級:

//need to pass a new index to getChildren once current node has no more children
public ArrayList<Nodes> getChildren (Nodes n) {
    ArrayList<Nodes> childrenList;
    childrenList = new ArrayList<Nodes>();  
    int index = nodeList.indexOf(n);
    int col = 0;
    while (col < size) {
        if (adjMatrix[index][col] == 1) {
            childrenList.add(nodeList.get(col));
        }
        col++;
    }
    return childrenList;
}

問題:

現在,我很難將rootNode傳遞給getChildren(Node n) ...因此它將返回所有正確的節點...但是一旦當前節點沒有更多的孩子並且列表中包含了該節點,我就需要傳遞下一個節點被退回...但正在努力去做。

如果當前節點沒有更多的子節點可以輸出時,如果我能夠傳遞下一個節點,則應該獲得所需的表示形式。

謝謝!


更新的代碼:

我試圖遞歸地遍歷樹並畫出節點...

控制台輸出正確...

Children of A: B
Children of A: C
Children of A: D
Children of B: E
Children of B: F
end

但是我畫它們的方式根本不是很動態...我實際上是為每個索引添加一個“層” ...然后在它們之間增加一條邊

在此處輸入圖片說明

這是我使用索引的遞歸實現:

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

    //paint initial rootNode
    g.setColor(Color.GREEN);
    g.fillRect(rootNode.getX(), rootNode.getY(), rootNode.getWidth(), rootNode.getHeight());
    g.setColor(Color.black);
    g.drawString(rootNode.getValue(), rootNode.getX()+8, rootNode.getY()+17);

    paintComponent(g, 0, new ArrayList<Nodes>());
}

//paint children
public void paintComponent(Graphics g, int index, ArrayList<Nodes> nodePrintList) { 
    Nodes currNode = nodeList.get(index);
    nodePrintList = getChildren(currNode);  

    x = currNode.getX();
    y = currNode.getY();

    //tier 1
    if (index == 0 && !nodePrintList.isEmpty()) {
        y += 50;
        x -= 100;
        color = Color.CYAN;
    }//tier 2
    else if (index == 1 && !nodePrintList.isEmpty()) {
        y += 100;
        x -= 130;
        color = Color.YELLOW;
    }
           //and would need to keep adding logic for all indices...

    //base case: no more children
    if (nodeList.indexOf(currNode)==nodeList.size()-1 && nodePrintList.isEmpty()) {
        System.out.println("\nend");
    }
    else {          
        //loop through and print all children of node n
        for (Nodes child : nodePrintList) {
            g.setColor(color);
            System.out.print("\nChildren of " + currNode.getValue() + ": " + child.getValue());
            g.fillRect(x+=50, y, child.getWidth(), child.getHeight());

            //write which node it is
            g.setColor(Color.black);
            g.drawString(child.getValue(), x+8, y+17);

            //add red edge between parent-child
            g.setColor(Color.red);
            g.drawLine(currNode.getX()+10, currNode.getY()+25, x+10, y-2);

        }
        paintComponent(g, ++index, new ArrayList<Nodes>()); 
    }
}

您可以看到紅色邊緣從父A適當地連接到其子B, C, D ,但紅色邊緣沒有從B與其子EF

請幫忙!

這是一種遞歸執行的方法:

public void paintComponent(Graphics g) {        
    paintComponent(g, rootNode)
}

public void paintComponent(Graphics g, Nodes curRoot) {        
    ...
    nodePrintList = getChildren(curRoot);  
    for (Nodes n : nodePrintList) {     
        System.out.println("\nChildren of " + rootNode.getValue() + ": " + n.getValue());
        ...
        paintComponent(g, n);
    }
}

但是,每次下/上樹時,您都必須插入x和y坐標,因此您要記住在樹的n層上繪制最后一個框的位置。

哦,從上面的圖形中,我看到您的圖中一個子節點可以有多個父節點(F有多個父節點),這使整個布局變得更加困難,因為您必須記住,是否已經繪制了一個節點(以及在哪里繪制箭頭...)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM