Tutorials / Index / Next /
Ever try making an animation and the flickering of it really make you mad! Their is a simple way of overcoming this. It's called buffering. Buffering allows the objects to be drawn to an image and then the image being drawn to the canvas in one move, instead of having many seperate drawings on the canvas. This eliminates the flickering and makes the program look a 100 times better.

THIS IS THE CODE FOR THE BUFFER:

// Dy Derek Jones 2000

//-----------------------------------------------------------------------------
// Import Files
//-----------------------------------------------------------------------------

import java.awt.*;
import java.applet.Applet;

//-----------------------------------------------------------------------------
// TrivialApplet, showing buffered and non-buffered graphics
//-----------------------------------------------------------------------------

public class TrivialApplet extends Applet
{	
	//-------------------------------------------------------------------------
	// Variables
	//-------------------------------------------------------------------------
	
	// Font
	Font font = new Font("ARIAL", Font.BOLD, 20);
	
	// Buffered Graphics
	Dimension offDimension;	// Width and height of buffer
	Image offImage;		// Image to be drawn to and then drawn on the surface
	Graphics offGraphics;	// Context for drawing to the image
	
	//-------------------------------------------------------------------------
	// Program
	//-------------------------------------------------------------------------
	
	///////////////////////////////////////////////////////////
	// Name: paint()
	// Desc: paints shapes and text using buffer and without
	// using buffer, to show difference
	///////////////////////////////////////////////////////////

	public void paint(Graphics g)
	{
	        // Grab the width and height of the canvas
	        Dimension d = size();
	
	        // Create the offscreen graphics, if no good one exists
	        if (offGraphics == null || d.width != offDimension.width || d.height != offDimension.height)
	        {
	                offDimension = d;
	                offImage = createImage(d.width, d.height);
	                offGraphics = offImage.getGraphics();
	        }
	        
	        float x = 0f;
	        boolean movingRight = true;
	        
	        // Drawing Loop
	        while(true)
	        {
	        	// Update Location
			if(movingRight)
			{
				x+=0.5f;
			}
			else	
			{
				x-=0.5f;
			}
	        		
			//Clear the screen
			offGraphics.setColor(Color.white);
			offGraphics.fillRect(0,0,d.width,d.height); 
			// not clearRect() because its an offscreen image

			// Draw Items
			offGraphics.setColor(Color.black);
			offGraphics.setFont(font);
			offGraphics.drawString("GRAPHICS BUFFERS",10,25);
			offGraphics.setColor(Color.blue);
			offGraphics.fillOval((int)x,0,30,30);
	                        
			// Copy the off screen image buffer to the screen.
			g.drawImage(offImage,0,0,this);
	                
			// Bounce
			if( x >= d.width - 30f)
			{
				movingRight = false;
			}
			else  if( x <= 0f)
			{
				movingRight = true;
			}
		}
	}
}
Hosted by www.Geocities.ws

1