我正在扩展 BufferedImage 类,以添加一些方法,如 getRed、getBlue、getGreen 来获取像素颜色。问题是我的原始图像是 BufferedImage 对象而不是我的扩展对象。当我尝试转换为扩展数据类型时,它不起作用。对不起我的英语不好
我收到这个错误
Exception in thread "main" java.lang.ClassCastException: java.awt.image.BufferedImage cannot be cast to asciiart.EBufferedImage
我试图从父类转换的代码
EBufferedImage character = (EBufferedImage)ImageClass.charToImage(letter, this.matrix_x, this.matrix_y);
我的扩展课
public class EBufferedImage extends BufferedImage
{
public EBufferedImage(int width, int height, int imageType)
{
super(width,height,imageType);
}
/**
* Returns the red component in the range 0-255 in the default sRGB
* space.
* @return the red component.
*/
public int getRed(int x, int y) {
return (getRGB(x, y) >> 16) & 0xFF;
}
/**
* Returns the green component in the range 0-255 in the default sRGB
* space.
* @return the green component.
*/
public int getGreen(int x, int y) {
return (getRGB(x, y) >> 8) & 0xFF;
}
/**
* Returns the blue component in the range 0-255 in the default sRGB
* space.
* @return the blue component.
*/
public int getBlue(int x, int y) {
return (getRGB(x, y) >> 0) & 0xFF;
}
}