-1

I wonder that how the function below is working. With the first function call, it paints only background over the old square location, but the with second function call it paints red square.

if(CURR_X != x || CURR_Y != y) {
     //The square is moving, repaint backgorund
     //over the old square location
     repaint(CURR_X,CURR_Y,CURR_W+OFFSET,CURR_H+OFFSET);
     //Update coordinates
     square.setX(x);
     square.setY(y);
     repaint(square.getX(),square.getY(),
                square.getWidth()+OFFSET,square.getHeight()+OFFSET);
}
Maroun
  • 94,125
  • 30
  • 188
  • 241
  • See [*Painting in AWT and Swing*](http://www.oracle.com/technetwork/java/painting-140037.html). – trashgod Dec 04 '16 at 11:27
  • http://stackoverflow.com/questions/10852897/repaint-method-in-java i think youll find youre answer over there – Koen2K Dec 04 '16 at 11:38

1 Answers1

4

The repaint() method passes the paint request to the RepaintManager. When multiple requests are received is a short period of time the RepaintManager will consolidate the two individual requests into a single request.

So if you have something like:

repaint(5, 5, 20, 20);
...
repaint( 30, 30, 20, 20);

The RepaintManager ends up consolidating them into a single repaint of (5, 5, 45, 45). So this larger area will include the area of both individual requests. So then the paintComponent() method paints the background of that area and then paints the square.

camickr
  • 321,443
  • 19
  • 166
  • 288