在一个游戏里用了 paintImmediately 来刷新某 JComponent cc 为 opaque,空布局,没有子控件用 System.currentTimeMillis() 取时间差测得 c 的 paintComponent() 方法耗时为 0 msc.paintImmediatly 的每次调用却耗时 15 ~ 16 ms 不等,这正常吗?
我测量的方法正确吗?
有没有什么办法改进 paintImmediatly 的效率?如果不用 paintImmediatly,有什么办法可以安全的得到一个往屏幕上绘制 c 的 Graphics 对象?swing游戏jcomponent

解决方案 »

  1.   

    补充系统和硬件信息:AMD X2 3600+WIN7Oracle JDK 6
      

  2.   

    测试时间的方法可以。刷新方法可以用repaint方法,paintimmediately很少使用了。
      

  3.   

    paintimmediately貌似是个终极方法。
      

  4.   

    看painImmediately的介绍Paints the specified region in this component and all of its descendants that overlap the region, immediately.如果没有子控件,也许可以考虑不采用这方法,但可能会得不到“立即”的效果。虽然painImmediately方法最终也是调用paint方法的。
      

  5.   

    先谢谢楼上各位。多线程是不行的。谢谢。 
    repaint是不行的,因为它不是同步调用,对EDT的时间占用不可控,而且最终repaint所导致的每一次重绘对EDT的占用时间更长,实测多于15~16毫秒。paintImmediately 是同步调用,内部好像对绘制区域有优化,同时实现了双缓冲。如果实在不行,再考虑绕开painImmediately,见楼顶最后一个问题。
      

  6.   

    你说的“安全的得到一个往屏幕上绘制 c 的 Graphics 对象”是什么意思?完整的画完一个图?
      

  7.   


    其实我是想问,直接 c.getGraphics() 然后绘制是不是安全,因为我大概看了一下 painImmediately 里面调用的 safelyGetGraphics 方法,没看懂,不知道它跟直接 c.getGraphics() 有什么区别。
      

  8.   

    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这个方法没有抛异常。
      

  9.   

    发这贴本意是想了解一下影响 paintImmediately 效率的因素可能有哪些,
    问题本身也许问的有问题,
    总之是没有得到想要的答案……感谢楼上三位的关注,结贴了分数平分了吧