/* Drawing in applets is almost always done with
This means that drawing is first done to an offscreen
image, and when all
is done, the offscreen image is drawn on the
This reduces the nasty flickering applets otherwise
Above you see:
No Double-buffering : It flickers because everything
is draw straight to the screen.
Bad double-buffering: Same as this below but
without the update() method
Double Buffering: Example of this source.
public class DoubleBuffering extends Applet implements MouseMotionListener
// The object we will
use to write with instead of the standard screen graphics
// The image that will
contain everything that has been drawn on
// To get the width
and height of the applet.
int curX, curY;
public void init()
We'll ask the width and height by this
dim = getSize();
// We'll redraw the applet eacht time the mouse has moved.
// Create an offscreen image to draw on
// Make it the size of the applet, this is just perfect larger
// size could slow it down unnecessary.
// by doing this everything that is drawn by bufferGraphics
// will be written on the offscreen image.
public void paint(Graphics g)
Wipe off everything that has been drawn before
// Otherwise previous drawings would also be displayed.
// draw the rect at the current mouse position
// to the offscreen image
draw the offscreen image to the screen like a normal image.
// Since offscreen is the screen width we start at 0,0.
// Always required for
// This will cause the
applet not to first wipe off
// previous drawings
but to immediately repaint.
// the wiping off also
// Update is called
automatically when repaint() is called.
public void update(Graphics g)
// Save the current mouse
position to paint a rectangle there.
// and request a repaint()
public void mouseMoved(MouseEvent evt)
curX = evt.getX();
curY = evt.getY();
// The necessary methods.
public void mouseDragged(MouseEvent evt)
This is all about double-buffering. It's easy
to use and recommended to use always.
There is one dangerous pitfall here, when you
create an offscreen image that's very large
the applet might run slow because it takes a
lot of resources and effort.
I would not recommend offscreen images larger
than 500*500 when redrawn at 30FPS.