Sign in to follow this  

Java: Buffered images still flickering

This topic is 4690 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have a game up and running in Java which uses double buffering. However the first time each image of a sprite animation is shown it flickers. For instance when I walk the main character to the left for the first time the character flickers, but when I walk left from then on the animation is smooth. Why is this ? Could it be something do do with the way I obtain my images... <CODE> Image img; img = Toolkit.getDefaultToolkit().getImage("sprites/up" + currentFrame + ".png"); BufferedImage bi = new BufferedImage(50, 100, BufferedImage.TYPE_INT_ARGB); bi.getGraphics().drawImage(img, 0,0, null); g.drawImage(bi,x,y, null); </CODE> Any ideas ?

Share this post


Link to post
Share on other sites
Couldn't really say for certain without looking at your other code, but ..

There are at least two better ways of loading your images.

One is through ImageIcon (from which you can get an instance of Image)
Other is through ImageIO (which returns BufferedImage)

Now, to make as sure as possible, that your images will be
accellerated, you should make sure that they fit your
current GraphicsConfiguration.

One solution:



public BufferedImage loadImage(String file)
{
BufferedImage initial = null;

try
{
initial = ImageIO.read(file);
}

catch(IOException e) { ... }

if(initial!=null)
{
GraphicsConfiguration gC = screenFrame.getGraphicsConfiguration();
BufferedImage result =
gc.createCompatibleImage(initial.getWidth(),initial.getHeight(),
initial.getColorModel().getTransparency());
Graphics2D g = result.createGraphics();
g.drawImage(initial,0,0,null);
g.dispose();
return result;
}

return null;
}



screenFrame is here your JFrame.

(I only typed this directly in here, so forgive me if it does not compile.
However, it shouldn't be far off).

But like I said, would need to see the rest of your code to help you better.

Share this post


Link to post
Share on other sites
This happens frequently. In Brackeens book he recommends to draw all your graphics with necessarily displaying them before the game starts. Seems to work.

PS; this would probably get alot more attention and intelligible responses in the Java forum on this site.

Share this post


Link to post
Share on other sites

This topic is 4690 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this