且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

java图形重绘方法实际上是如何工作的

更新时间:2023-02-06 09:46:35

我首先要阅读执行自定义绘画在AWT和Swing中绘画



重绘 RepaintManager 发出请求以绘制部分或全部一个组件。 RepaintManager 将决定绘制的内容和绘制内容,可能将重绘请求合并为尽可能少的更新(因此重复调用重绘实际上可能会减慢你的绘制过程。)



RepaintManager 然后推送在事件调度线程上绘制事件。这可以确保在EDT的上下文中处理paint事件。



在Swing中进行绘制时,有许多可能的解决方案可以提高速度和资源管理。 / p>

您可以考虑实施自己的双缓冲策略,将更新绘制到屏幕外缓冲区,准备好后,切换到活动缓冲区,这将被绘制。



这意味着油漆很快,因为所有工作都已经完成(大概是在后台线程中)。



例如......





您还可以查看被动与主动渲染,但我非常肯定你知道你是什么让自己为... ...


I've just started working with java 2d graphics applications, on my studies repaint is redrawing our graphics wasting a lot of resources. 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

repaint makes a request to the RepaintManager to paint part or all of a component. 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).

The RepaintManager then pushes a paint event onto the Event Dispatching Thread. This ensures that the paint event is handle within the context of the EDT.

There are many possible solutions for improving speed and resource management when it comes to painting in 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...