<Swing Dev> How to resolve image tearing

Anthony Petrov anthony.petrov at oracle.com
Fri May 27 12:07:55 UTC 2011


Hi Lin,

On 5/26/2011 10:16 PM, ximalaya wrote:
> 1. Method paint of Canvas(hw component)  is not called when its 
> container JPanel repaints
>     I have a JFrame. Inside the JFrame, there is a JPanel. And inside 
> the JPanel there is a Canvas. I found Canvas.paint was never called when 
> I call JPanel.repaint(). Is it normal? Is it just another case we simply 
> can't sync up with repainting of lw and hw components?

HW components use native ('heavyweight') windows, and as such they rely 
on repaint requests sent by the native system (eg. WM_PAINT on Windows, 
or ExposeNotify on X11). I guess that this fact has been taken into 
account when implementing the JComponent.repaint() method, and hence 
Swing doesn't call <hw>.repaint() manually.

In theory, calling paint()/repaint() for hw components in the 
JComponent.paint()/repaint() shouldn't hurt at all. If Swing team agrees 
that this is feasible, please file an RFE on this feature against Swing 
at http://bugreport.sun.com/bugreport/.


> 2.  Thread safe of Componet.repaint//JComponent.repaint()/
>     I ever read some articles about  JComponet.repaint. It was said that 
> it was thread safe. But I learn from an article that  JComponet.repaint 
> is no longer thread safe since JDK 6 update 22 -
>    "/do not call JComponent.repaint() outside the Swing Event Dispatch 
> Thread" ,
>    / You can find the article here, 
> http://stevensrmiller.com/wordpress/?p=567

I'll let the Swing team members to comment on the above.

>      Does it apply to Componet.repaint also?

However, just want to mention that Component.repaint() (at least, for hw 
components) should not be affected by this since AWT is generally a 
multi-threaded GUI library.

--
best regards,
Anthony



More information about the swing-dev mailing list