BFS および DFS アルゴリズムを実装しました。
私の最終的な目標は、アルゴリズムを JPanel にアニメーション化することです...
しかし、まず、ノードをそれぞれの親子関係で画面にペイントしたいと思います。

こんなに出てるのに…

私がやろうとしているのは、子の(x,y)=親の(x, y)+ 50px ...のように設定することです:
g.fillRect(x+=50, y=parentNode.getY()+50, child.getWidth(), child.getHeight());
そうすれば、親の子は常に低くなり、親からオフセットされます...
写真を参考に…
ノードB's(x、y)は親ノード(x + 50、y + 50)と等しくなりますA's...そのようなもの...最終的にxは左にオフセットが必要になることがわかっているので、下に素敵なピラミッド効果が得られます親...
public void paintComponent(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, width, height);
g.setColor(Color.CYAN);
g.fillRect(rootNode.getX(), rootNode.getY(), rootNode.getWidth(), rootNode.getHeight());
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();
for (Nodes child : nodePrintList) {
System.out.print("PARENT: " + parentNode.getValue() + " | x,y: " + parentNode.getX() + ", " + parentNode.getY() + "...\n CHILD: " + child.getValue() + " | x,y: " + child.getX() + ", " + child.getY());
g.setColor(Color.GREEN);
g.fillRect(x+=50, y=parentNode.getY()+50, child.getWidth(), child.getHeight());
paintComponent(g, child);
}
}
}
どうすればそれを達成できるのでしょうか?なぜ子供たちはすべてAの下の同じ層に出てくるのですか? ノードEであり、ノードのF下にある必要がありますB
System.out.print("Parent: " + parentNode.getValue() + ". Child: " + child.getValue());からのコンソール出力は次のとおりであるため、再帰が機能していることはわかっています。
PARENT: A | x,y: 180, 100...
CHILD: B | x,y: 180, 100
PARENT: B | x,y: 205, 150...
CHILD: E | x,y: 180, 100
PARENT: B | x,y: 205, 150...
CHILD: F | x,y: 180, 100
PARENT: A | x,y: 180, 100...
CHILD: C | x,y: 180, 100
PARENT: A | x,y: 180, 100...
CHILD: D | x,y: 180, 100
PARENT: D | x,y: 205, 150...
CHILD: G | x,y: 180, 100
end