Java: I get a class cast exception error pointing to class I didn't even import -


My problem is simple, I already have the code written to scale the image and then crop for the desired dimensions (By receiving Constant class).

  if (image! = Null) {Image originalImage = image.getImage (); Int width = stable. Wide; // Algorithm: Get the original width and divide it with the desired width int height = Original image. Gatehit (empty) / (Original image. Gatewath (empty) / width); Image scale image = Original image Gate Sensational Instance (width, height, JavaA.T. image. SCALE_SMOOTH); // Now to crop it to specified dimensions, BufferedImage imageToCrop = (BufferedImage) (java.awt.Image) scaledImage; Height = Stable height; ImageToCrop = imageToCrop.getSubimage (imageToCrop.getWidth () - width, imageToCrop.getHeight () - height, width, height); Image.setImage (imageToCrop); }   

When driving, I get an error:

  java.lang.ClassCastException: sun.awt.image.ToolkitImage not included in Java .awt.image.BufferedImage   

and this line corresponds to:

  BufferedImage imageToCrop = (BufferedImage) (java.awt.Image) ScaledImage;   

Now I've never imported the sun, in fact there is a list of imported items for this class:

  import java.awt.Image ; Import java.awt.image.BufferedImage; Import java.io.file; Import javax.swing.ImageIcon;   

So why is this error? I just dont get it! As you can see, I have tried several ways to force Cast to manually, but still it has no use: (

Any help really appreciated! Thank you!

You have not imported it, but this is a sub class of image Is not a subclass of BufferedImage , so you can not cast it.

A given image To create a BufferedImage , you must specify the target image <

one example:

  public BufferedImage buffer image (Image Image, Int Type) {BufferedImage bufferedImage = new BufferedImage (image);); (), (Empty, empty) ;;);); 

Comments