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
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.
Uncomment super.paint(g) [line 87] in your paint method.
It is responsible for clearing the canvas of any stale objects.