On Friday, April 21, 2017 at 2:13:45 PM UTC-4, Rick C. Hodgin wrote:
>
> The way I have applied it (using another Stack Overflow link, and one
> of several that I've looked at) doesn't work.
>
> Thank you,
> Rick C. Hodgin
Well, if you Google that problem, the first result looks like they did the same thing.
http://stackoverflow.com/questions/15839223/java-awt-image-databufferbyte-cannot-be-cast-to-java-awt-image-databufferint
So I test your code with a transparent PNG and it does show the ImageIO.read() statement returned a type 6.
http://docs.oracle.com/javase/8/docs/api/constant-values.html#java.awt.image.BufferedImage.TYPE_3BYTE_BGR
So if you want to manipulate ints it seems you need a TYPE_INT (1,2,3,4).
So what do you get if you convert it?
Insert this code right after the ImageIO.read().
final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
final GraphicsDevice gd = ge.getDefaultScreenDevice();
final GraphicsConfiguration gc = gd.getDefaultConfiguration();
final BufferedImage newimg = gc.createCompatibleImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
// Draw the image on to the buffered image
final Graphics2D myImageGraphics = newimg.createGraphics();
myImageGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
myImageGraphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
myImageGraphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
myImageGraphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
myImageGraphics.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
myImageGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
myImageGraphics.fillRect(0, 0, img.getWidth(), img.getHeight());
myImageGraphics.setComposite(AlphaComposite.SrcOver);
myImageGraphics.drawImage(img, 0, 0, null);
myImageGraphics.dispose();
img = newimg;
and of course modify your imports
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;