JavaBFS算法-尝试在JPanel上绘制节点

JavaBFS算法-尝试在JPanel上绘制节点,java,algorithm,swing,breadth-first-search,Java,Algorithm,Swing,Breadth First Search,我刚刚实现了BFS和DFS算法 我的最终目标是将算法动画化到JPanel上 但首先,我想在屏幕上绘制节点各自的父子关系: 到目前为止,我已经能够实现: 我的油漆成分如下: public void paintComponent(Graphics g) { ArrayList<Nodes> nodePrintList = new ArrayList<Nodes>(); g.setColor(Color.WHITE); g.f

我刚刚实现了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;
    }

}
问题:

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

}
现在我正在努力将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>()); 
    }
}
公共组件(图形g){
g、 设置颜色(颜色为黑色);
g、 fillRect(0,0,宽度,高度);
//绘制初始根节点
g、 setColor(Color.GREEN);
g、 fillRect(rootNode.getX(),rootNode.getY(),rootNode.getWidth(),rootNode.getHeight());
g、 设置颜色(颜色为黑色);
g、 抽绳(rootNode.getValue(),rootNode.getX()+8,rootNode.getY()+17);
paintComponent(g,0,newarraylist());
}
//画孩子
public void paintComponent(图形g、int索引、ArrayList nodePrintList){
Nodes currNode=nodeList.get(索引);
nodePrintList=getChildren(currNode);
x=currNode.getX();
y=currNode.getY();
//一级
if(index==0&&!nodePrintList.isEmpty()){
y+=50;
x-=100;
颜色=color.CYAN;
}//二级
else if(index==1&&!nodePrintList.isEmpty()){
y+=100;
x-=130;
颜色=颜色。黄色;
}
//并且需要继续为所有索引添加逻辑。。。
//基本情况:没有更多的孩子
if(nodeList.indexOf(currNode)=nodeList.size()-1&&nodePrintList.isEmpty()){
System.out.println(“\nend”);
}
否则{
//循环并打印节点n的所有子节点
用于(子节点:nodePrintList){
g、 设置颜色(颜色);
System.out.print(“+currNode.getValue()+”的子项:“+child.getValue());
g、 fillRect(x+=50,y,child.getWidth(),child.getHeight());
//写下它是哪个节点
g、 设置颜色(颜色为黑色);
g、 抽绳(child.getValue(),x+8,y+17);
//在父子关系之间添加红边
g、 setColor(Color.red);
g、 抽绳(currNode.getX()+10,currNode.getY()+25,x+10,y-2);
}
paintComponent(g,++索引,新的ArrayList());
}
}
您可以看到,红边从父
A
适当地连接到其子
B、C、D
,但红边没有从
B
连接到其子
E
F


请帮忙

这里有一种递归的方法:

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有多个父节点),这使得整个布局方式更加困难,因为您必须记住,是否已经绘制了节点(以及在何处,如果要绘制箭头…).

您需要编写递归遍历树的例程。@RomanC请参见上文。我已经用递归解决方案更新了代码。我在连接边缘时遇到问题。或者让节点自己绘制,并向其子节点所在的位置添加箭头。