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());
开发者_JAVA百科 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.
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.
精彩评论