Got a bit of a problem, trying to play an animated gif in Java2D even though I am using double buffering the animated gif is still tearing every few frames (as if it were only partially drawn).
protected Image gifImage;
protected Image offscreen;
protected Thread animationThread;
public void start() {
animationThread = new Thread() {
public void run() {
while(loaderThread != null) {
repaint();
MyApplet.this.sleep(100);
}
animationThread = null;
}
};
animationThread.start();
}
...
public final void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
// create offscreen if missing
if (offscreen == null) {
offscreen = createImage(getWidth(), getHeight());
}
// draw everything onto an image before drawing to avoid flicker
Graphics og = offscreen.getGraphics();
// set background color
og.setColor(Color.black);
og.fillRect(0, 0, getWidth(), getHeight());
// get logo position so its in the middle of applet
int x = (getWidth() - gifImage.getWidth(this)) / 2;
int y = (getHeight() - gifImage.getHeight(this)) / 2;
// draw animated gif
og.drawImage(gifImage, x, y, null);
og.dispose();
// finally draw it all
g.drawImage(offscreen, 0, 0, null);
}
it doesn’t happen every frame but after random interval.
anyone got any idea’s or fixes for this?