对于双缓冲的分析是在坦克大战游戏的设计时开始的,由于当时忙于游戏的整体设计,所以对这一个问题没有进行详细的研究,现在就这个问题来谈谈自己的一些看法。( y6 G; q# a6 G. |- q
分析前提出几个问题:
- a; a' m+ i6 G" k 1、为什么当想屏幕上添加图片之后会有明显的闪烁现象?
% T) [+ ? `: i6 [" d# a$ }6 C4 v 2、在awt中如何实现双缓冲?
" I: c: x: a8 l 3、如何理解swing内置双缓冲以及比较他与awt中消除闪烁的方法区别在哪里?) x! R0 v& y. x& i
首先我们来解答第一个问题:
. \8 c+ d2 ~ c- p, C4 C. C v 我们在屏幕上自绘图形或者是添加图片都是要通过所在画布的重绘来实现的,因此闪烁的出现必然与重绘机制有着一些关联。在awt中对于窗体画布的重绘其条用顺序是repaint() —>update()—>paint();我们来看看update()的源码:5 c' Z$ N0 y. f( v( S9 i8 d4 Q
Java代码) j; e; N% V& Y4 x$ F
/**
9 Q0 A' K" b7 T' H+ C * Updates the container. This forwards the update to any lightweight
- U: H4 j) ~6 `1 @) T * components that are children of this container. If this method is
1 n1 ?8 E! D( m1 D1 @* |# |) c * reimplemented, super.update(g) should be called so that lightweight" [" h4 e1 b5 e3 D _" I+ A: D$ `
* components are properly rendered. If a child component is entirely$ c3 S) w0 ^. g# n: v
* clipped by the current clipping setting in g, update() will not be
" v9 f' x8 n4 `5 j* t; P7 i7 \* p0 N * forwarded to that child.. M7 r) B/ `8 Z q6 |
*) x! @4 Y0 m3 B/ R! ~7 [
* @param g the specified Graphics window
8 s. Q8 ?, h$ X- n+ ~ * @see" z. R7 N& ^- s" K! x$ _2 s& @ ]+ B
Component#update(Graphics)
+ Z8 ^; S7 W- o7 m3 X- I& f* e( c */
* u6 k& g1 c5 x public void update(Graphics g) {
- [" A5 h% |, P5 B7 ?, G if (isShowing()) {
* V8 l+ w4 b2 t! X' R8 l if (! (peer instanceof LightweightPeer)) {, s* l t+ X& X8 O, F
g.clearRect(0, 0, width, height);
9 Y+ C3 M, i0 h. k6 x) U* {* f* | }$ e, a. Q6 _4 M1 ]
paint(g);2 ^. J, n! ^" M$ c7 p. P
}/ F q: q$ e# ?8 Y
}
( n4 X( U' E2 s3 p/ i* U( Z 从这里我们可以清晰的看到,update中有一个清屏的作用,即g.clearRect(0, 0, width, height);然后再在下面调用paint(g),函数进行重绘。因此到这里的话我们可以在一定程度上对底层的重绘机制有一个了解了。 |