Java的BFS算法 - 试图画上的JPanel节点 [英] Java BFS Algorithm - Trying to paint nodes on JPanel
问题描述
我刚刚实施了BFS和DFS算法。
I've just implemented a BFS and DFS algorithm.
我的最终目标是算法动画到一个JPanel ...
My ultimate goal is to animate the algorithm onto a JPanel...
不过,首先我想画在各自的亲子关系中的节点到屏幕上:
But firstly I'd like to paint the nodes to the screen in their respective parent-child relationship:
到目前为止,我已经能够实现:
So far I've been able to achieve:
我的涂料成份如下:
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(节点N)
获取该父列表当前儿童:
Which gets the current children in a list for that parent by calling 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(节点N)
...所以这将返回所有正确的节点...但我需要通过一旦在当前节点没有更多的孩子的下一个节点列表已返回...但我在努力如何做到这一点。
Right now I'm hard passing the rootNode to getChildren(Node n)
... so it will return all the correct nodes... But I need to pass the next node in once the current node has no more children and the list has been returned... but am struggling how to do it.
如果我能顺利通过下一个节点,一旦当前节点没有更多的孩子来输出,我应该得到重新presentation我在找。
If I'm able to pass the next node in once the current node has no more children to output, I should get the representation I'm looking for.
谢谢!
更新时间是code:
我已经尝试递归遍历树,画出来的节点...
I've attempted recursively traversing the tree and painting out the nodes...
控制台输出是正确的......
The console output is correct...
Children of A: B
Children of A: C
Children of A: D
Children of B: E
Children of B: F
end
但我画他们的方式是不是非常有活力在所有...我有效地增加一个层为每个索引...然后将它们之间的边缘
But the way I'm painting them is not very dynamic at all... I'm effectively adding a "tier" for each index... then an edge between them
下面是一个使用索引我的递归实现:
Here is my recursive implementation using an index:
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连接
其子电子
和 F
。
You can see that the Red edges are connecting appropriately from parent A
to its children B, C, D
, but red edges are not connecting from B
to its children E
and F
.
任何帮助,请!
推荐答案
下面是一个方法来递归地做到这一点:
Here's a way to do it recursively:
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级。
However, you'll have to meddle with x and y coordinates every time you go down/up the tree, so you remember where you painted your last box on level n of the tree.
呵呵,并从图形上面我看到在图中的子节点可以有多个家长(F有多个父),这使得整个布点的方式很难,因为你必须要记住,如果一个节点已经有被抽(何地,如果你要画箭头...)。
Oh and from the graphics above I see that in your graph a child node can have multiple parents (F has multiple parents), which makes the whole layouting way harder, because you'll have to remember, if a node has already been drawn (and where, if you want to draw arrows ...).
这篇关于Java的BFS算法 - 试图画上的JPanel节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!