简体   繁体   English

如何取消JPanel的重绘?

[英]How to cancel a repaint of a JPanel?

I know that JPanel is, by default, automatically double-buffered. 我知道JPanel默认是自动双缓冲的。 However, I have a particularly time-intensive painting operation in my panel, but the panel only needs to be repainted when the underlying data changes, which is rare. 但是,我的面板中有一个特别耗时的绘画操作,但是当底层数据发生变化时,面板只需要重新绘制,这种情况很少见。 Therefore, I'd like to reuse the JPanel buffer instead of having it clear after every call to repaint() . 因此,我想重用JPanel缓冲区,而不是在每次调用repaint()后都清除它。

I've manually implemented a "dirty" flag on my JPanel subclass, but I have no idea how to cancel a paint operation once it's been started. 我在我的JPanel子类上手动实现了一个“脏”标志,但我不知道如何在启动后取消绘制操作。 I can't avoid the call to repaint in the first place, since my panel is inside a JScrollPane, which is being repainted every time it's resized (which does happen frequently), which causes my custom panel to be repainted. 我无法避免通话摆在首位,以重画,因为我的面板是一个JScrollPane,正在重新粉刷每它的调整时间( 经常出现),这将导致我的自定义面板进行重新绘制内。

Is there any way to do this without manually buffering the panel? 如果没有手动缓冲面板,有没有办法做到这一点? If not, what's the recommended method for implementing a manual buffer in conjunction with a JPanel? 如果没有,那么与JPanel一起实现手动缓冲区的推荐方法是什么?

I have a particularly time-intensive painting operation .. only needs to be repainted when the underlying data changes, which is rare.. 我有一个特别耗时的绘画操作..只需要在基础数据发生变化时重新绘制,这很少见。

Paint the data to a BufferedImage , display it in a JLabel . 将数据绘制到BufferedImage ,将其显示在JLabel Call label.repaint() if it changes. 如果更改,请调用label.repaint() EG as seen in this answer . EG在这个答案中看到了。

Bonus Showing an image in a label is a way to get a GUI with a preferred size, that does not need to extend anything. 奖金在标签中显示图像是一种获得具有首选大小的GUI的方法,不需要扩展任何内容。 To get the perfect size for the frame or dialog that displays it, call pack() . 要获得显示它的框架或对话框的完美大小,请调用pack()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM