I'm not sure it would cause that sort of problem, but I believe your toBufferdImage code should be:
1 2 3 4 5 6 7 8
| 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| public static BufferedImage bufferImage(final Image image) { if(image == null) return null;
BufferedImage buffer = createImage(image.getWidth(null), image.getHeight(null)); Graphics2D g2 = buffer.createGraphics(); g2.drawImage(image, 0, 0, null); g2.dispose();
return buffer; }
public static BufferedImage createImage(final int width, final int height) { return createImage(width, height, true); }
public static BufferedImage createImage(final int width, final int height, final boolean bAllowTransparency) { GraphicsConfiguration graphicsConfiguration = GraphicsEnvironment. getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
return graphicsConfiguration.createCompatibleImage(width, height, bAllowTransparency ? Transparency.BITMASK : Transparency.OPAQUE); }
public static BufferedImage loadImage(final InputStream inputStream) { BufferedImage image = null; try { image = ImageIO.read(inputStream); } catch(Exception exception) { if(image == null) return null; }
return bufferImage(image); } |
It's a bit more complicated than it has to be because I use the buffer/create methods for creating images too.