0

二分木を表示する 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 クラスのコンストラクターが呼び出されます。もちろん、私は間違っている可能性があり、問題はこれとはまったく関係ありません。

4

1 に答える 1

0

Full_Binary_Treeコンストラクター内でオブジェクトを作成する代わりに、オブジェクトを作成するクラスWindow_Controller内で別のメソッドを作成する必要があります。たとえば、コードは次のようになります。Window_ControllerFull_Binary_Tree

public class Window_Controller
{
....
 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();//Don't create object of Full_Binary_Tree here.
    }
  public synchronized void createFullBinaryTreeInstance()
  {
    if (fullTree == null)
    fullTree = new Full_Binary_Tree();
  }
}

さて、オブジェクトを作成するときは、オブジェクトの作成後にメソッドを明示的にWindow_Controller呼び出します。createFullBinaryTreeInstance()お気に入り:

Window_Controller wController = new Window_Controller();
wController.createFullBinaryTreeInstance();
于 2013-04-27T20:54:41.690 に答える