双缓冲原理在实现消除闪烁的方法

2012-11-12
    对于双缓冲的分析是在坦克大战游戏的设计时开始的,由于当时忙于游戏的整体设计,所以对这一个问题没有进行详细的研究,现在就这个问题来谈谈自己的一些看法。
    分析前提出几个问题:
    1、为什么当想屏幕上添加图片之后会有明显的闪烁现象?
    2、在awt中如何实现双缓冲?
    3、如何理解swing内置双缓冲以及比较他与awt中消除闪烁的方法区别在哪里?
    首先我们来解答第一个问题:
    我们在屏幕上自绘图形或者是添加图片都是要通过所在画布的重绘来实现的,因此闪烁的出现必然与重绘机制有着一些关联。在awt中对于窗体画布的重绘其条用顺序是repaint() —>update()—>paint();我们来看看update()的源码:
    Java代码
    /**
    * Updates the container.  This forwards the update to any lightweight
    * components that are children of this container.  If this method is
    * reimplemented, super.update(g) should be called so that lightweight
    * components are properly rendered.  If a child component is entirely
    * clipped by the current clipping setting in g, update() will not be
    * forwarded to that child.
    *
    * @param g the specified Graphics window
    * @see
    Component#update(Graphics)
    */
    public void update(Graphics g) {
    if (isShowing()) {
    if (! (peer instanceof LightweightPeer)) {
    g.clearRect(0, 0, width, height);
    }
    paint(g);
    }
    }
    从这里我们可以清晰的看到,update中有一个清屏的作用,即g.clearRect(0, 0, width, height);然后再在下面调用paint(g),函数进行重绘。因此到这里的话我们可以在一定程度上对底层的重绘机制有一个了解了。
分享到:
0
相关阅读
友情链接
© 2018 我考网 http://www.woexam.com 中国互联网举报中心 湘ICP备18023104号 京公网安备 11010802020116号
违法和不良信息举报:9447029@qq.com