23

I've spent maybe the last two hours browsing and reading up on these methods and the Graphics class, and maybe I'm stupid, haha, but I'm just not understanding them. What are they for? I understand that they're supposed redraw or update components on the screen, but I have never understood why this is required (I'm new to this). For example, if I'm moving a JLabel around the screen, a call to setLocation() moves it just fine. Is that a scenario in which repaint() isn't required? In which scenarios is it useful, and why?

Apologies if you feel that this is a question that could be solved using the search function, but for whatever reason I'm not getting it.

richarbernal
  • 1,063
  • 2
  • 14
  • 32
Daniel
  • 581
  • 1
  • 5
  • 11
  • 1
    Repaint is called internally. It is called from within JLabel's setLocation method. (It is actually a longer chain of method calls, feel free to follow it and see what's inside. You an do it in any serious IDE out there) – toniedzwiedz May 26 '12 at 18:13
  • Okay, that it is called within setLocation() certainly helps a little bit. Thank you. When would I change something that WOULDN'T call it? – Daniel May 26 '12 at 20:59
  • all controls from swing library can repaint themselves whenever their properties connected in any way with how they look (position, size, color, text, etc.) change. However, when you want to achieve a non-default result, implement the paint method yourself. Drawing geometrical figures in JApplets is a common exercise. You may also want to read up on the subject of double-buffering, which is connected to the way both methods are called. A more real-life example from my experience is extending a swing component to draw the spectrum of a fourier transform of a wav file by overriding paint – toniedzwiedz May 26 '12 at 21:47
  • Thank you! Makes much more sense now. So if all I'm using in terms of visuals are built into swing, I don't have to worry about calling repaint()? – Daniel May 26 '12 at 22:08
  • that's right, the library classes will do it for you. – toniedzwiedz May 26 '12 at 22:19

3 Answers3

24

Difference between Paint() and Repaint() method

Paint():

This method holds instructions to paint this component. Actually, in Swing, you should change paintComponent() instead of paint(), as paint calls paintBorder(), paintComponent() and paintChildren(). You shouldn't call this method directly, you should call repaint() instead.

Repaint():

This method can't be overridden. It controls the update() -> paint() cycle. You should call this method to get a component to repaint itself. If you have done anything to change the look of the component, but not its size ( like changing color, animating, etc. ) then call this method.

Pete
  • 7,289
  • 10
  • 39
  • 63
Siva Charan
  • 17,940
  • 9
  • 60
  • 95
  • Yes, +1 for mentioning that paint() itself shouldn't usually be overridden by user code in Swing. – Neil Coffey May 26 '12 at 19:18
  • Unfortunately, having read lots of documentation, I already have heard this. I just would like to understand what applications I would use it in, and what it actually means to "paint this component". – Daniel May 26 '12 at 20:57
  • 1
    The logic of this answer seems a bit counter-intuitive to me. For paint, you describe, "You shouldn't call this method directly, you should call repaint() instead." Then you go on to say, "If you have done anything to change the look of the component, but not it's size then call this method." for repaint, implying if I have changed its size I can't call either of these functions. If I have changed the size and that's why I want it to redraw the UI, then what do I call? – searchengine27 Aug 18 '15 at 23:27
  • Does repaint() reset the component to its original state? Let's say I have a JPanel with a white background, and I've drawn a triangle on it. Is it going repaint to delete that triangle, and reset the JPanel? – carloswm85 Oct 02 '19 at 17:10
5

The paint() method supports painting via a Graphics object.

The repaint() method is used to cause paint() to be invoked by the AWT painting thread.

Baz
  • 36,440
  • 11
  • 68
  • 94
0

It's not necessary to call repaint unless you need to render something specific onto a component. "Something specific" meaning anything that isn't provided internally by the windowing toolkit you're using.

blend
  • 132
  • 1
  • 9
  • I'm sorry, I don't understand that response. Could you provide an example please? Render what onto what? – Daniel May 26 '12 at 18:13
  • See [this article](http://java.sun.com/products/jfc/tsc/articles/painting/) for a good explanation. – blend May 26 '12 at 18:21