问题
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