Java - repaint(x, y, w, h) doesn't call paintComponent? [duplicate]

拥有回忆 提交于 2019-12-24 13:40:15

问题


Possible Duplicate:
Java - repaint(x, y, w, h) doesn't call paintComponent? (with SSCCE)

I'm trying out this neato performance trick repaint(x, y, w, h) and it sure is helping performance a lot.

Unfortunately, the special extras I've put into a paintComponent in the same class are now not being painted. I put a System.out.println() test at the beginning of paintComponent and it turns out it's not even called (as our astute readers probably were thinking from the beginning of this paragraph). When I use a plain repaint(), paintComponent() is called, no problem.

Specifically, I've got a JLabel, with a mouseListener, that on mouseEnter repaints the label.

What's the deal? I hope I'm missing something and this is still possible? That extra performance sure is nice...


回答1:


repaint() is actually a one-liner that calls repaint(0, 0, width, height), so your basic thesis here -- that repaint() with arguments is fundamentally different from repaint() without -- is provably false. If I were a betting man, I'd bet that the arguments you're passing to this method describe a region with zero area (i.e., width <= 0) and so the RepaintManager is simply ignoring the requests.

You may be able to demonstrate this by changing the arguments to known good constant values, or maybe just println() the values of the arguments before you pass them.




回答2:


you can use JComponent#paintImmediately

1) be sure by testing your output to the Swing GUI by using / wrapping to the

if (SwingUtilities.isEventDispatchThread()) {

otherwise you could get an exception from RepaintManager

2) repaint() could be invoke EDT by default, untill first Thread#sleep(int) called

3) invoke code for painting from Swing Timer and output will be on EDT, but Timer doesn't works too if Thread#sleep(int) called



来源:https://stackoverflow.com/questions/9287194/java-repaintx-y-w-h-doesnt-call-paintcomponent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!