Graphic PIZiadas

Graphic PIZiadas

My world is in..

Graphical Programming: Double buffering : Draw in the background [JAVA]

doblebufferWhen creating the animation engine we have seen that the screen refresh flicker occurs undesirable. This effect is due to the time required to erase the image and generate a new updated, result of animate objects.

One technique to avoid this is called doble buffer o “doublebuffered, so we use two images (graphics memory) both. While drawing on a, see the other and minimize changeover time between images eliminating flicker.

In computing, multiple buffer storage is the use of more of a buffer to hold a block of data, so that a “reader” you will see a complete version of the data, instead of a partially updated version of data that are created by a “writer”. It is also used to avoid the need to use the dual-port RAM, when readers and writers are different devices.(W)

Modify the class “Blackboard” we have modified to incorporate the animation engine to implement this technique. We will need to know the dimensions of the graphic display enough memory to store, and define an object “Image” will be on which we draw the graphs.

	Dimension d;
	private Image mImage = null;

The method “paint” drawing first get the image size to generate. The function call “checkOffScreenImage()” sure will handle an object “mImage” on which to draw.

To draw on this object request your graphic direction with the function “getGraphics“. Then I'll fill color we use for background on this drawing a filled rectangle element.

We call routines drawn image, we have implemented in the function “paintOffScreen” passing this graphic object as a parameter for you to use as a canvas.

The last function will draw on the canvas or screen image generated.

	public synchronized void paint(Graphics g) {
	   d = getSize();
	   checkOffScreenImage();
	   Graphics offG = mImage.getGraphics();
	   offG.setColor(getBackground());
	   offG.fillRect(0, 0, d.width, d.height);
	   paintOffScreen(mImage.getGraphics());
	   g.drawImage(mImage, 0, 0, zero);
	}

Function “checkOffScreenImage()” oversee it if you have changed the size of the window used to display graphical information and generate an object on which to draw, using the function “createImage()

	private void checkOffScreenImage() {
	   Dimension d = getSize();

	   if (d.width == 0 || d.height == 0) {
		return;
	   }
	   if (mImage == null || mImage.getWidth(zero) != d.width
	      || mImage.getHeight(zero) != d.height) {
		mImage = createImage(d.width, d.height);
	   }
	}

Function “paintOffScree” It is the old function or method “paint” our drawing graphic objects, so do not deserve additional comments. We have simply changed your name.

	public void paintOffScreen(Graphics g){
	   Enumeration enum=bd.elements();
	   Object ob;
	   while(enum.hasMoreElements()){
		ob = enum.nextElement();
		if(ob instanceof SimpleDrawable){
			((SimpleDrawable) whether).paint(g);
		}
	   }
	}

Running the application will see the image flicker is gone.

JAVA

JAVA Course