Total Pageviews

Sunday, August 11, 2013

Avoid flickering using Buffered image technique(Java)

In the previous tutorial, there was a little flickering of the image "flyy.jpg" during program execution and obviously the animation was bad. So, in this tutorial i will show you how to avoid the flickering effect through Buffered Image Technique.
The flickering is due to the fact that the "flyy.jpg" is being directly drawn on the graphics context of the Canvas.
                 
         public void draw(Graphics g){
g.setColor(Color.white);
g.fillRect(0, 0, 800, 600); //clears the previous screen
g.drawImage(fly, x, y, null);
}

Here "g" is the graphics context of the Object Screen(extends Canvas).

So, the steps to avoid this are-

1.Create an object of type "Image".
 
     Image offScreen;
     offScreen = createImage(800,600);

2.Create an object of type "Graphics".
   
    Graphics bufferGraphics;

3.Get the graphics context of  "offScreen" in "bufferGraphics".

    bufferGraphics = offScreen.getGraphics();

4.Now replace the "draw( Graphics)" method with this.

   public void draw(Graphics g){
bufferGraphics.setColor(Color.white);
bufferGraphics.fillRect(0, 0, 800, 600); //clears the previous screen
bufferGraphics.drawImage(fly, x, y, null);
g.drawImage(offScreen, 0, 0, this);
}

Now , you can see that you are actually drawing everything on "offScreen" and at last drawing the "offScreen" on "g".

Now the full updated code is here-

                                                  FULL SOURCE CODE
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;


public class Screen extends Canvas implements Runnable {

static Screen screen = new Screen();
Thread t;
Image fly;
boolean animation = true;
int x=400,y=400,dx=4,dy=4;
Image offScreen;
Graphics bufferGraphics;

public void init(){

try {
fly = ImageIO.read(new File("res/flyy.jpg"));
} catch (IOException e){}

offScreen = createImage(800,600);
bufferGraphics = offScreen.getGraphics();

t = new Thread(this);
t.start();

}

public void run(){

while(animation){

flyMoves(); //checks the fly movement
 
draw(screen.getGraphics()); //draws image on the screen

try{
Thread.sleep(40);
}catch(Exception e){}

}

}

public void flyMoves(){
x += dx;
y += dy;

if(x<0 || x>750)
dx = -dx;
if(y<0 || y>550)
dy = -dy;
}

public void draw(Graphics g){
bufferGraphics.setColor(Color.white);
bufferGraphics.fillRect(0, 0, 800, 600); //clears the previous screen
bufferGraphics.drawImage(fly, x, y, null);
g.drawImage(offScreen, 0, 0, this);
}

public static void main(String[] args) {

JFrame frame = new JFrame();
frame.add(screen);
frame.pack();
frame.setTitle("Simple Animation");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setSize(800, 600);
frame.setResizable(false);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
screen.init();

}

}

                                                                        SNAPSHOT


No comments:

Post a Comment