Java - repaint(x, y, w, h) 不调用paintComponent?
我正在尝试这个 neato 性能技巧 repaint(x, y, w, h)
,它确实对性能有很大帮助。
不幸的是,我放入同一个类的 paintComponent
中的特殊附加内容现在没有被绘制。我在 paintComponent
的开头放置了一个 System.out.println()
测试,结果发现它甚至没有被调用(正如我们精明的读者可能从一开始就想到的那样)本段)。当我使用普通的 repaint()
时,调用 paintComponent()
,没有问题。
具体来说,我有一个带有 mouseListener
的 JLabel,它在 mouseEnter
上重新绘制标签。
这是怎么回事?我希望我错过了一些东西,这仍然有可能吗?这种额外的表现确实很好......
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
repaint()
实际上是一个调用repaint(0, 0, width, height)
的单行代码,所以这里的基本论点是repaint()带参数的
与不带 -- 的repaint()
根本不同,可以证明是错误的。如果我是一个打赌的人,我敢打赌您传递给此方法的参数描述了一个面积为零的区域(即宽度 <= 0),因此RepaintManager
会简单地忽略的请求。您可以通过将参数更改为已知的良好常量值来演示这一点,或者只是在传递参数之前
println()
更改参数的值。repaint()
is actually a one-liner that callsrepaint(0, 0, width, height)
, so your basic thesis here -- thatrepaint()
with arguments is fundamentally different fromrepaint()
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 theRepaintManager
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.您可以使用 JComponent#paintImmediately
1) 确保使用 / 换行来测试 Swing GUI 的输出,
否则您可能会得到 来自 RepaintManager 的异常
2) 可以调用
repaint()
默认情况下为 EDT,直到第一个Thread#sleep(int)
被调用3) 从 Swing Timer 和输出将在 EDT,但 Timer 不会如果调用
Thread#sleep(int)
也可以工作you can use JComponent#paintImmediately
1) be sure by testing your output to the Swing GUI by using / wrapping to the
otherwise you could get an exception from RepaintManager
2)
repaint()
could be invoke EDT by default, untill firstThread#sleep(int)
called3) invoke code for painting from Swing Timer and output will be on EDT, but Timer doesn't works too if
Thread#sleep(int)
called