/* A simple Double Buffering example by Jayant Mukherjee (courtesy: Complete Reference) */
/* Another way of doing the same is described by Wayne McKenzie on PSC */
import java.awt.*;
import java.applet.*;
import java.awt.image.*;
import java.awt.event.*;
public class DblBuffr extends Applet implements MouseMotionListener{
int ax,ay;
Dimension dSize;
Image dblBuffImg; //Off Screen
Graphics dblBuffer; //Off Screen Graphics
public void init(){
dSize = this.getSize();
dblBuffImg = this.createImage(dSize.width, dSize.height);
dblBuffer = dblBuffImg.getGraphics();
addMouseMotionListener(this);
this.setBackground(Color.yellow);
}
public void mouseDragged(MouseEvent em){} //Not used, but required
public void mouseMoved(MouseEvent em)
{
ax = em.getX();
ay = em.getY();
this.paint(this.getGraphics()); //Fast repainting
}
public void update(){} //Overriding, for Flicker Free Drawing
public void paint(Graphics g)
{
dblBuffer.clearRect(0, 0, dSize.width, dSize.height);
for(int i=0; i<=dSize.width; i=i+10)
{
dblBuffer.drawLine(ax,ay,i,0); //Top edge
dblBuffer.drawLine(ax,ay,i,dSize.height); //Bottom edge
}
for(int i=0; i<=dSize.height; i=i+10)
{
dblBuffer.drawLine(ax,ay,0,i); //Left edge
dblBuffer.drawLine(ax,ay,dSize.width,i); //Right edge
}
g.drawImage(dblBuffImg, 0, 0, null);
}
}
|