Some problems about GIF images...

Hi there! I got some problems with GIF images format.
In my J2D application (a simple game) I load some PNG images (24 bit colors), but since the execution was slow I switched the format of images in GIF format (8 bit colors). Here I met the problems. Before the switching, all the PNG images were loaded on the screen without problems, now the GIF images are loaded sometimes yes sometimes no. And I do not understand why. The method to load the images is the same:
`
public static Image loadImage(URL path) {
try {
return new ImageIcon(path).getImage();
} catch (Exception e) {
Log.errln(ERROR + "exception: " + e);
}

	return null;
}

public static BufferedImage toBufferedImage(Image image) {
	BufferedImage bi = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
	bi.getGraphics().drawImage(image, 0, 0, null);
	bi.getGraphics().dispose();
	
	return bi;
}

`

I load the image in Image Object after I convert it in BufferedImage. Are the methods right or not?

I’m not sure it would cause that sort of problem, but I believe your toBufferdImage code should be:

public static BufferedImage toBufferedImage(Image image) {
      BufferedImage bi = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
      Graphics g = bi.createGraphics();
      g.drawImage(image, 0, 0, null);
      g.dispose();
      
      return bi;
   }

The main thing is that I don’t think you count on getGraphics to return the same Graphics object every time. Hence, you should get the object once. I also used createGraphics instead of getGraphics because that’s the more “normal” method to call for a BufferedImage.

This is the code I’ve been using for loading images:

public static BufferedImage bufferImage(final Image image) {
	if(image == null)
		return null;

	//this enables transparency and makes the Image more likely to be a managed image
	BufferedImage buffer = createImage(image.getWidth(null), image.getHeight(null));
	Graphics2D g2 = buffer.createGraphics();
	g2.drawImage(image, 0, 0, null);
	g2.dispose();

	return buffer;
} //end bufferImage

public static BufferedImage createImage(final int width, final int height) {
	return createImage(width, height, true);
} //end createImage

public static BufferedImage createImage(final int width, final int height,
	final boolean bAllowTransparency)
{
	//get the GraphicsConfiguration
	GraphicsConfiguration graphicsConfiguration = GraphicsEnvironment.
		getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();

	//this enables/disables transparency and makes the Image more likely to be managed
	return graphicsConfiguration.createCompatibleImage(width, height,
		bAllowTransparency ? Transparency.BITMASK : Transparency.OPAQUE);
} //end createImage

public static BufferedImage loadImage(final InputStream inputStream) {
	//load the file
	BufferedImage image = null;
	try {
		image = ImageIO.read(inputStream);
	} catch(Exception exception) {
		if(image == null)
			return null;
	}

	return bufferImage(image);
} //end loadImage

It’s a bit more complicated than it has to be because I use the buffer/create methods for creating images too.

Understood.
I will try, thanks man! :wink: