Java - repaint(x, y, w, h) 不调用paintComponent?

发布于 2025-01-05 20:53:37 字数 702 浏览 1 评论 0原文

可能的重复:
Java - repaint(x, y, w, h) 不调用油漆组件? (与 SSCCE)

我正在尝试这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岛歌少女 2025-01-12 20:53:37

repaint() 实际上是一个调用 repaint(0, 0, width, height) 的单行代码,所以这里的基本论点是 repaint()带参数的 与不带 -- 的 repaint() 根本不同,可以证明是错误的。如果我是一个打赌的人,我敢打赌您传递给此方法的参数描述了一个面积为零的区域(即宽度 <= 0),因此 RepaintManager 会简单地忽略的请求。

您可以通过将参数更改为已知的良好常量值来演示这一点,或者只是在传递参数之前 println() 更改参数的值。

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.

甜味超标? 2025-01-12 20:53:37

您可以使用 JComponent#paintImmediately

1) 确保使用 / 换行来测试 Swing GUI 的输出,

if (SwingUtilities.isEventDispatchThread()) {

否则您可能会得到 来自 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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文