CGのダブルバッファ

public void run() {
while (true) {
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
}
}
paint()では、loop_index の値を増加させながら、黒の塗りつぶし楕円を描画します。黒の塗りつぶしをすると、背景を白にする消去を見やすくなり、「ちらつき」が観測できます。 public void update(Graphics g) {
paint(g);
}
ただし、バッファの消去は必要ですから、paint() の中で、import java.awt.*;
import java.applet.*;
public class anima extends Applet implements Runnable {
Image image1;
Graphics offgraphics;
Thread thread1;
int loop_index = 0;
Checkbox checkbox = new Checkbox();
public void init() {
checkbox.setLabel("DoubleBuffer");
checkbox.setBounds(new Rectangle(20, 220, 60, 20));
this.setLayout(null);
this.add(checkbox, null);
}
public void start() {
image1 = createImage(getSize().width, getSize().height);
offgraphics = image1.getGraphics();
//System.out.println("image size="+image1.getWidth(this));
thread1 = new Thread(this);
thread1.start();
}
public void run() {
while (true) {
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
}
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
Graphics graphics = g;
if (checkbox.getState())
graphics = offgraphics;
graphics.clearRect(0, 0, getSize().width, getSize().height);
Color c1 = new Color(100, 100, 100);
Color c2 = new Color(10, 50, 50);
loop_index += 2;
if (loop_index >= 200)
loop_index = 5;
graphics.setColor(c1);
graphics.fillOval(
loop_index);
100 - loop_index / 2,
100 - loop_index / 2,
loop_index,
graphics.setColor(c2);
if (checkbox.getState())
g.drawImage(image1, 0, 0, this);
}
}