import java.applet.*; import java.awt.event.*; import java.awt.*; public class applet_doubleBuffering extends Applet implements MouseMotionListener { Graphics bufferGraphics; // The object we will use to write with instead of the standard screen graphics Image offscreen; // The image that will contain everything that has been drawn on bufferGraphics. Dimension dim; int curX, curY; public void init() { dim = getSize(); addMouseMotionListener(this); setBackground(Color.blue); offscreen = createImage(dim.width,dim.height); // Create an offscreen image to draw on bufferGraphics = offscreen.getGraphics(); // everything that is drawn by bufferGraphics will be written on the offscreen image. } public void paint(Graphics g) { bufferGraphics.setFont(new Font("Arial",Font.BOLD,28)); bufferGraphics.clearRect(0,0,dim.width,dim.width); bufferGraphics.setColor(Color.white); bufferGraphics.drawString("Double-buffered",10,35); bufferGraphics.setFont(new Font("Arial",Font.BOLD,18)); bufferGraphics.setColor(Color.yellow); bufferGraphics.drawString("Drawing is first done to an",10,70); bufferGraphics.drawString("offscreen image, and when all",10,95); bufferGraphics.drawString("is done, the offscreen image",10,120); bufferGraphics.drawString("is drawn on the screen.",10,145); bufferGraphics.setColor(Color.red); bufferGraphics.fillRect(curX,curY,20,20); g.drawImage(offscreen,0,0,this); // draw the offscreen image to the screen like a normal image. } public void update(Graphics g) { paint(g); } public void mouseMoved(MouseEvent evt) { curX = evt.getX(); curY = evt.getY(); repaint(); } public void mouseDragged(MouseEvent evt) { } }