how java graphics repaint method actually works

穿精又带淫゛_ 提交于 2019-12-23 02:04:37

问题


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?


回答1:


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...

  • Swing animation running extremely slow
  • the images are not loading
  • How to make line animation smoother?
  • Java Bouncing Ball

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...



来源:https://stackoverflow.com/questions/17737555/how-java-graphics-repaint-method-actually-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!