简体   繁体   English

Java图形重绘方法实际上如何工作

[英]how java graphics repaint method actually works

I've just started working with java 2d graphics applications, on my studies repaint is redrawing our graphics wasting a lot of resources. 我刚刚开始使用Java 2d图形应用程序,在我的研究中,重绘正在重绘我们的图形,从而浪费了大量资源。 but I want to know what repaint is, does and how to use it efficiently, thread safely and fast for many movable dynamic objects on my canvas? 但是我想知道什么是重绘,重绘是什么以及如何有效地重绘,安全快速地对画布上的许多可移动动态对象进行穿线?

I would start by having a read through Performing Custom Painting and Painting in AWT and Swing 我将从阅读《 在AWT和Swing中 执行自定义绘画绘画》开始

repaint makes a request to the RepaintManager to paint part or all of a component. repaintRepaintManager发出请求,以绘制部分或全部组件。 The RepaintManager will decide what and how much will be painted, possible consolidating repaint requests into as small a number of updates as possible (so repeatedly calling repaint could actually slow your paint process). RepaintManager将决定要绘制的内容和数量,并可能将重新绘制请求合并为尽可能少的更新(因此,重复调用repaint实际上可能会减慢绘制过程)。

The RepaintManager then pushes a paint event onto the Event Dispatching Thread. 然后, RepaintManager将一个paint事件推送到事件调度线程上。 This ensures that the paint event is handle within the context of the EDT. 这样可以确保在EDT上下文中处理paint事件。

There are many possible solutions for improving speed and resource management when it comes to painting in Swing. 在Swing中绘画时,有许多可能的解决方案可以提高速度和资源管理。

You could consider implementing your own double buffering strategy, painting your updates to an off screen buffer and when ready, switching to the active buffer, which will get painted. 您可以考虑实现自己的双重缓冲策略,将更新涂到屏幕外缓冲区,并在准备好后切换到活动缓冲区,该缓冲区将被绘制。

This means that the paint is quick as all the work has already being done (presumably in a background thread). 这意味着绘制很快,因为所有工作都已经完成(大概在后台线程中)。

For examples... 举些例子...

You could also take a look at Passive vs. Active Rendering , but I'd be very sure that you know what you're getting yourself in for... 您也可以看一下Passive vs. Active Rendering ,但是我非常确定您知道自己要做什么...

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

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