Java repaint() Call paint()

The paint() method is called by the JVM implicitly in two circumstances. One is when the first time frame is created and displayed. The other is when the frame is resized (by dragging the frame border with mouse) by the user. If the programmer would like to call the paint() method in the middle of the coding, he is permitted to call repaint() method. He is not permitted to call paint() method directly (Java repaint() Call paint()).

What is the designing secret in not allowing paint() to call directly? (Java repaint() Call paint())

Yes, calling paint() method raises compilation error. Before the window is to be drawn with new data, the old data must be erased, else, both overwrite (not override) the other and finally the data is not readable. This automatic erasing is done by the repaint() method. How?

The repaint() method calls automatically update() method and in turn update() method calls paint() method.

repaint() –> update() –> paint()

The update() method job is to erase the earlier drawings on the frame. On the cleared surface, the paint() method draws afresh with the latest graphics. If the programmer is allowed to call paint() method directly, there is every possibility that he may forget to call the update() method explicitly. To avoid this and to make Java as a robust language, the designers do not allow to call paint() directly.

1 thought on “Java repaint() Call paint()”

Leave a Comment

Your email address will not be published.