简体   繁体   English

如何在Swing中仅重绘脏区?

[英]How to repaint only dirty region in Swing?

  1. Does repainting dirty region only improve performance? 重新绘制脏区只能提高性能吗?

  2. If the answer is yes, how to do it? 如果答案是肯定的,怎么做? Originally I use repaint() to call paintComponent(Graphics); 最初我使用repaint()来调用paintComponent(Graphics); however, recently I aware that repaint() is actually repaint(0,0,width,height), which repaints everything each time. 但是,最近我意识到repaint()实际上是重绘(0,0,宽度,高度),每次重绘一切。 If I use repaint() with parameters to specify the dirty region, in what way will it pass such data to paintComponent(Graphics)? 如果我使用带参数的repaint()指定脏区域,它会以什么方式将这些数据传递给paintComponent(Graphics)?

If I use repaint() with parameters to specify the dirty region, in what way will it pass such data to paintComponent(Graphics)? 如果我使用带参数的repaint()指定脏区域,它会以什么方式将这些数据传递给paintComponent(Graphics)?

The "clip bounds" or the Graphics object will be set to the specified region “剪辑边界”或Graphics对象将设置为指定的区域

1.Does repainting dirty region only improve performance? 1.重新粉刷脏区只会提高性能吗?

Why? 为什么? Do you have a painting problem. 你有绘画问题吗? Don't micro optimize the code, unless you have a reason to do so. 除非您有理由这样做,否则不要对代码进行微观优化。 The code will be harder to maintain and debug. 代码将难以维护和调试。 You will be adding extra logic to determine which regions need to be repainted. 您将添加额外的逻辑来确定需要重新绘制哪些区域。

Also, remember that multiple repaint requests get merged into a single request. 此外,请记住,多个重绘请求会合并到一个请求中。 So if you make a request to repaint the top/left corner of a component and the immediately request a repaint of the bottom/right, these two request will be merged into an area that includes both regions, which means the entire component will be repainted. 因此,如果您请求重新绘制组件的左上角并立即请求重新绘制底部/右侧,则这两个请求将合并到包含两个区域的区域,这意味着整个组件将被重新绘制。 So you have done extra work for nothing. 所以你没有做任何额外的工作。

To repaint only "dirty" regions of a large JComponent you will need to use the RepaintManager . 要重新绘制大型JComponent “脏”区域,您需要使用RepaintManager You can get the current RepaintManager with: 您可以使用以下命令获取当前的RepaintManager

RepaintManager rm = RepaintManager.currentManager(component);

You can even replace the RepaintManager with your own custom version. 您甚至可以用自己的自定义版本替换RepaintManager Additional details can be found on Oracle 's website. 其他详细信息可以在Oracle的网站上找到。

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

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