Repaint leaves trail

后端 未结 2 1022
情书的邮戳
情书的邮戳 2020-12-21 10:31

I know that this isn\'t the first time that this question has been asked, but the responses haven\'t helped me much, so I am helping I am finally going to get my answer

相关标签:
2条回答
  • 2020-12-21 11:19

    The reason is that in the line g.fillRect(0,0,WIDTH,HEIGHT); it actually is referring to java.awt.image.ImageObserver#WIDTH and java.awt.image.ImageObserver#HEIGHT instead of your instance variables. You can see this with any IDE.

    Because of that the expected black background is painted only in a 1x2 pixel area in the top-left corner. And since the call to super.paint(g); is commented out, not even the gray background is repainted. As a result, old drawings of the car are not overdrawn.

    The code needs to be changed to use MAP.this.WIDTH, or the WIDTH field needs to be renamed to something which does not conflict with the fields inherited from JPanel, or the field needs to be moved into the same JPanel to have higher precedence, or you could also use the getWidth() method from JPanel. And likewise for height.

    0 讨论(0)
  • 2020-12-21 11:21

    Uncomment super.paint(g) [line 87] in your paint method.

    It is responsible for clearing the canvas of any stale objects.

    0 讨论(0)
提交回复
热议问题