Why is this print line command executing twice?

瘦欲@ 提交于 2019-12-23 20:29:06

问题


I have the code below.

It all works, but annoyingly, the print line command in the while loop runs twice. There is (and I have tested for it), only unique items in the queue, no duplicates.

public void paint(Graphics g) {

    boolean isParent;

    int drawCount = 1;

    int x = 0, y = 0, width = 0, height = 0;
    Color colour;

    while (!qtreeQueue.empty()) {

        drawNode = (QuadTreeNode) qtreeQueue.deque();
        isParent = drawNode.getIsParent();

        if (!isParent) {
            x = drawNode.getRectangle().x;
            y = drawNode.getRectangle().y;
            width = drawNode.getRectangle().width;
            height = drawNode.getRectangle().height;
            colour = getRectangleColour(drawNode);
            System.out.println(drawCount + ". Drawing: x = " + x + "; y = " + y + 
                    "; width = " + width + "; height = " + height + 
                    "; colour = " + colour.toString());
            minMax(drawNode);
            g.setColor(colour);
            g.fillRect(x, y, width, height);
            drawCount++;
        }
    }
    System.out.println("Minimum level of tree: " + min + "\nMaximum level: " + max);
}

Appreciate the help.


回答1:


That means the paint method is being called twice, which is perfectly normal. The system can call paint as many times as it wants, so you shouldn't perform any operation that might change the state of your program within that method.



来源:https://stackoverflow.com/questions/5204626/why-is-this-print-line-command-executing-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!