There is already question like this link on StackOverflow and the accepted answer is "casting":
Image image = ImageIO.read(new File(file));
BufferedImage buffered = (BufferedImage) image;
In my program I try:
final float FACTOR = 4f;
BufferedImage img = ImageIO.read(new File("graphic.png"));
int scaleX = (int) (img.getWidth() * FACTOR);
int scaleY = (int) (img.getHeight() * FACTOR);
Image image = img.getScaledInstance(scaleX, scaleY, Image.SCALE_SMOOTH);
BufferedImage buffered = (BufferedImage) image;
Unfortunatelly I get run time error:
sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
Obviously casting does not work.
Question is: What is (or is there) the proper way of converting Image to BufferedImage?
ImageIO.read(File)returns aBufferedImageby its signature. (Reference) There is no need to first assign to anImagevariable then cast to typeBufferedImage. That might confuse people reading your code.