BufferedImage 클래스를 확장하여 getRed, getBlue, getGreen과 같은 메서드를 추가하여 픽셀 색상을 가져옵니다. 문제는 내 원본 이미지가 확장 된 객체가 아닌 BufferedImage 객체라는 점입니다. 확장 된 데이터 형식으로 변환하려고하면 작동하지 않습니다. 내 영어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;
}
}
단순히 개체를 아닌 유형으로 캐스팅 할 수 없습니다. 그것은 결코 작동하지 않을 것입니다. 대신에 클래스에 BufferedImage 객체를 받아들이고 전달 된 BufferedImage를 기반으로 객체의 인스턴스를 만드는 생성자를 제공하는 것이 좋을까요? –