Reputation: 1413
I have a custom class vtools.Image that extends BufferedImage.
Then I am reading a picture from a url using ImageIO.read and sacing this as BufferedImage.
BufferedImage bfImg = ImageIO.read(imageUrl);
but then I would need to convert this bfImg into vtools.Image but when I try to cast it I am getting a ClassCastException error. What is the other way of converting these two?
Upvotes: 1
Views: 132
Reputation: 17525
EDIT: At first I thought, that you could simply write a special constructor, that takes a BufferedImage
. But that is not that easy. So I would recommend to write a wrapper class like this:
package vtools;
import java.awt.image.BufferedImage;
public class Image {
BufferedImage image;
public Image(BufferedImage image) {
this.image = image;
}
// Add your methods, that modify the image or return the result.
}
Upvotes: 2
Reputation: 1932
ImageIO is from the core Java classes. It will have no idea how to read an image into your subclass. What you would need to do if you really want this is to have an extension of ImageIO (ie, you would have to subclass it, which I don't know is possible). Or you would have to have a vtools.Image constructor that takes a BufferredImage as parm and then pass that in.
Upvotes: 1