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);

}

[3456 byte] By [Glena] at [2007-11-26 23:58:48]
# 1

You shouldn't really override paint in the Frame class. Instead paint to a panel and put the panel in a frame. You also might want to look at using javax.swing classes JFrame and JPanel instead of java.awt. A basic JPanel is double buffered and should solve your problem if you paint to that instead of a java.awt.Frame.

robtafta at 2007-7-11 15:46:58 > top of Java-index,Java Essentials,Java Programming...