Start by making sure you have read through and understand the JavaDocs for Component#repaint
.
When using these methods, the clipping rectangle is modified so only the area you specify is available to be within in the Graphics
context, this means you are still free to paint outside this area, but it will have no effect.
Also, make sure that you "clear" the previous area first, otherwise you will end up with ghosting artifacts.
What this means is, unless you painting process is optimised to take advantage of this, it may have little to no effect on the time it takes to actually paint the area.
So, in short, yes, calling repaint(int, int, int, int)
, if done correctly will allow you to repaint only a portion of the UI, but you need to make considerations to allow for the fact that what you are trying to paint now might have been painted somewhere else and will need to also be repainted...
Quoting from Painting in AWT and Swing
A program must assume that a call to paint() implies that the area defined by the graphic's clip rectangle is "damaged" and must be completely repainted
And
"Smart" Painting
While the AWT attempts to make the process of
rendering components as efficient as possible, a component's paint()
implementation itself can have a significant impact on overall
performance. Two key areas that can affect this process are:
Using the clip region to narrow the scope of what is rendered.
Using internal knowledge of the layout to narrow the scope of what
children are painted (lightweights only).
If your component is
simple -- for example, if it's a pushbutton -- then it's not worth the
effort to factor the rendering in order to only paint the portion that
intersects the clip rectangle; it's preferable to just paint the
entire component and let the graphics clip appropriately. However, if
you've created a component that renders complex output, like a text
component, then it's critical that your code use the clip information
to narrow the amount of rendering.
Further, if you're writing a complex lightweight container that houses
numerous components, where the component and/or its layout manager has
information about the layout, then it's worth using that layout
knowledge to be smarter about determining which of the children must
be painted. The default implementation of Container.paint() simply
looks through the children sequentially and tests for visibility and
intersection -- an operation that may be unnecessarily inefficient
with certain layouts. For example, if a container layed out the
components in a 100x100 grid, then that grid information could be used
to determine more quickly which of those 10,000 components intersect
the clip rectangle and actually need to be painted.
Basically, what this is saying is, unless you have a need to optimise the painting process, it might not be worth while.