二分木を表示する Java クラス用のプログラムを作成しています。しかし、私が直面している問題の 1 つは、プログラムが最初に実行されたときに、プログラムが自身のコピーを無限に作成することです。問題がどこにあるかはかなりわかっていますが、それを修正する方法がわかりません。どんな助けでも大歓迎です、ありがとう!
問題はこのコンストラクターの最後にあると思います:
public Window_Controller() throws HeadlessException {
addMouseListener(this);
addWindowListener(close);
setTitle("Binary Trees - Alpha Stage");
setSize(1200, 700);
setLocation(40, 0);
setVisible(true);
incrementButton = new Button("+1", Color.white, deLoc - 47, y, 45);
decrementButton = new Button("-1", Color.white, deLoc, y, 45);
fullButton = new Button("Full Binary Tree", Color.cyan, x, y, 110);
completeButton = new Button("Complete Binary Tree", Color.green, x + 130, y, 150);
// I think this is the problem
fullTree = new Full_Binary_Tree();
}
このクラスは Window_Controller クラスを拡張するため:
public class Full_Binary_Tree extends Window_Controller
{
/**
*
*/
private static final long serialVersionUID = -3599016597017756766L;
BinaryNode rootNode;
BinaryNode current;
BinaryNode leftNode;
BinaryNode rightNode;
/**
* @throws HeadlessException
*/
public Full_Binary_Tree() throws HeadlessException
{
}
public void add(int value)
{
BinaryNode aNode = new BinaryNode(value);
aNode.setLeftNode(rootNode);
rootNode = aNode;
current = rootNode;
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
if(rootNode.getRightNode() == null) {
g2d.drawLine(rootNode.getX() + 25, rootNode.getY() + 25, rootNode.getX() + 65, rootNode.getY() + 87);
g2d.setColor(nullColor);
g2d.setFont(nullFont);
g2d.drawString("NULL", rootNode.getX() + 60, rootNode.getY() + 100);
g2d.setFont(defaultFont);
g2d.setColor(Color.black);
}
if(rootNode.getLeftNode() == null) {
g2d.drawLine(rootNode.getX() + 25, rootNode.getY() + 25, rootNode.getX() - 15, rootNode.getY() + 87);
g2d.setColor(nullColor);
g2d.setFont(nullFont);
g2d.drawString("NULL", rootNode.getX() - 42, rootNode.getY() + 100);
g2d.setFont(defaultFont);
g2d.setColor(Color.black);
}
if(current != null) {
current.paint(g2d);
}
}
}
main()
メソッドがコンストラクターを呼び出し、その行に到達すると、Full_Binary_Tree のコンストラクターが呼び出され、Window_Constructor クラスのコンストラクターが呼び出されます。もちろん、私は間違っている可能性があり、問題はこれとはまったく関係ありません。