How to move images without flickering.
Hi i developed a java awt program which moves a image in the screen.
But when i move the image there is flickering. this is because i clear the screen to white every time i redraw the screen. How can i overcome this flickering?
My code is as follows
import java.awt.*;
import java.awt.event.*;
publicclass AwtImageextends Frame
{
Image img;
boolean gameover;
int x=0,y=0;
publicstaticvoid main(String[] args)
{
AwtImage ai=new AwtImage();
}
public AwtImage()
{
super("Image Frame");
MediaTracker mt =new MediaTracker(this);
img = Toolkit.getDefaultToolkit().getImage("glen.jpg");
mt.addImage(img,0);
setSize(400,400);
setVisible(true);
addWindowListener(new WindowAdapter(){
publicvoid windowClosing(WindowEvent we){
dispose();
}
});
}
publicvoid update(Graphics g){
paint(g);
}
publicvoid updateGame()
{
if(x<400 && y<400)
{
x=x+3;
y=y+3;
}
else
{
x=0;y=0;
}
}
publicvoid paint(Graphics g){
if(img !=null)
{
while(gameover==false)
{
g.setColor(Color.white);
g.fillRect(0, 0, 400,400);
updateGame();
g.drawImage(img, x, y,this);
try{
Thread.sleep(50);
}catch (InterruptedException e){}
}
}
else
g.clearRect(0, 0, getSize().width, getSize().height);
}

