【Swing】 paintImmediately 的效率问题若干

raistlic 2013-01-30 07:17:01

在一个游戏里用了 paintImmediately 来刷新某 JComponent c

c 为 opaque,空布局,没有子控件

用 System.currentTimeMillis() 取时间差测得 c 的 paintComponent() 方法耗时为 0 ms

c.paintImmediatly 的每次调用却耗时 15 ~ 16 ms 不等,这正常吗?


我测量的方法正确吗?


有没有什么办法改进 paintImmediatly 的效率?

如果不用 paintImmediatly,有什么办法可以安全的得到一个往屏幕上绘制 c 的 Graphics 对象?
...全文
320 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
raistlic 2013-03-13
  • 打赏
  • 举报
回复
发这贴本意是想了解一下影响 paintImmediately 效率的因素可能有哪些, 问题本身也许问的有问题, 总之是没有得到想要的答案…… 感谢楼上三位的关注,结贴了分数平分了吧
  • 打赏
  • 举报
回复
getGraphics public Graphics getGraphics() Returns this component's graphics context, which lets you draw on a component. Use this method to get a Graphics object and then invoke operations on that object to draw on the component. Overrides: getGraphics in class Component Returns: this components graphics context 这个方法没有抛异常。
raistlic 2013-01-31
  • 打赏
  • 举报
回复
引用 7 楼 fangmingshijie 的回复:
你说的“安全的得到一个往屏幕上绘制 c 的 Graphics 对象”是什么意思?完整的画完一个图?
其实我是想问,直接 c.getGraphics() 然后绘制是不是安全,因为我大概看了一下 painImmediately 里面调用的 safelyGetGraphics 方法,没看懂,不知道它跟直接 c.getGraphics() 有什么区别。
  • 打赏
  • 举报
回复
你说的“安全的得到一个往屏幕上绘制 c 的 Graphics 对象”是什么意思?完整的画完一个图?
raistlic 2013-01-31
  • 打赏
  • 举报
回复
先谢谢楼上各位。
引用 2 楼 fangmingshijie 的回复:
多线程机制。
多线程是不行的。
引用 3 楼 brightyq 的回复:
测试时间的方法可以。刷新方法可以用repaint方法,paintimmediately很少使用了。
谢谢。 repaint是不行的,因为它不是同步调用,对EDT的时间占用不可控,而且最终repaint所导致的每一次重绘对EDT的占用时间更长,实测多于15~16毫秒。
引用 4 楼 fangmingshijie 的回复:
paintimmediately貌似是个终极方法。
paintImmediately 是同步调用,内部好像对绘制区域有优化,同时实现了双缓冲。
引用 5 楼 dracularking 的回复:
看painImmediately的介绍 Paints the specified region in this component and all of its descendants that overlap the region, immediately. 如果没有子控件,也许可以考虑不采用这方法,但可能会得不到“立即”的效果。虽然painImmediately方法最终也是调用paint方法的。
如果实在不行,再考虑绕开painImmediately,见楼顶最后一个问题。
dracularking 2013-01-30
  • 打赏
  • 举报
回复
看painImmediately的介绍 Paints the specified region in this component and all of its descendants that overlap the region, immediately. 如果没有子控件,也许可以考虑不采用这方法,但可能会得不到“立即”的效果。虽然painImmediately方法最终也是调用paint方法的。
  • 打赏
  • 举报
回复
paintimmediately貌似是个终极方法。
brightyq 2013-01-30
  • 打赏
  • 举报
回复
测试时间的方法可以。刷新方法可以用repaint方法,paintimmediately很少使用了。
  • 打赏
  • 举报
回复
多线程机制。
raistlic 2013-01-30
  • 打赏
  • 举报
回复
补充系统和硬件信息: AMD X2 3600+ WIN7 Oracle JDK 6

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧