Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

repaint() Method in Java

Tags:

java

awt

repaint

I am playing around with the Java graphics class a little bit and I was just wondering--when is it necessary to call the method repaint()? I tried commenting it out and it didn't seem to affect my output GUI. I've seen it used a lot in Java GUI code that I've read, though. Would anyone mind explaining when to use it and when not to use it?

like image 914
stk1234 Avatar asked Nov 04 '22 22:11

stk1234


2 Answers

The repaint() refreshes the view (component), so whenever you make any change on the component, you must call it. For instance, if you rotate the graphical component, you must make a call to repaint() in order to see the change on the containing component

like image 80
Joseph Elcid Avatar answered Nov 08 '22 08:11

Joseph Elcid


It's never really necessary in most swing applications, because they handle it automatically (for common operations such as changing text values on buttons and adding data to a list box).

Generally, it's only if you've made some sort of change that swing won't automatically pick up - for example, you're not using a layout manager and are resizing components manually (because normally the layout manager repaints its components when necessary).

like image 45
Tharwen Avatar answered Nov 08 '22 08:11

Tharwen