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.