Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 扩展BuffereImage类_Java_Casting_Bufferedimage_Extending - Fatal编程技术网

Java 扩展BuffereImage类

Java 扩展BuffereImage类,java,casting,bufferedimage,extending,Java,Casting,Bufferedimage,Extending,我扩展了BuffereImage类,添加了一些方法,比如getRed、getBlue、getGreen来获取像素颜色。问题是我的原始图像是BuffereImage对象,而不是我的扩展对象。当我尝试转换为扩展数据类型时,它不起作用。 对不起,我的英语不好 我得到这个错误 Exception in thread "main" java.lang.ClassCastException: java.awt.image.BufferedImage cannot be cast to asciiart.EB

我扩展了BuffereImage类,添加了一些方法,比如getRed、getBlue、getGreen来获取像素颜色。问题是我的原始图像是BuffereImage对象,而不是我的扩展对象。当我尝试转换为扩展数据类型时,它不起作用。 对不起,我的英语不好

我得到这个错误

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;
}
}

您有两个选择:

  • 向扩展类添加一个构造函数,该类接受
    buffereImage
    ,并适当地设置所有内容

    public class ExtendedBufferedImage extends BufferedImage{
    
      public ExtendedBufferedImage(BufferedImage image){
          //set all the values here
      }
    
      //add your methods below
    }
    
    这似乎是一个很大的工作和潜在的问题。如果您忘记设置一些变量,可能会引入一些奇怪的bug,或者丢失所需的信息

  • 创建一个包含
    buffereImage
    实例的包装类,然后在中添加方法

    public class ExtendedBufferedImage{
      private BufferedImage image;  
    
      public ExtendedBufferedImage(BufferedImage image){
         this.image = image;
      }
    
      //add your methods below
    }
    
    这是相当合理的,也不难做到。将
    BufferedImage
    公开或添加一个getter方法,如果需要,您可以从中获取实际的
    BufferedImage

  • 创建一个将方法作为静态的实用程序类,并将
    buffereImage
    作为参数传入

    public class BufferedImageUtil{
    
      public static int getRed(BufferedImage image, int x, int y) {
        return (image.getRGB(x, y) >> 16) & 0xFF;
      }
    
      //add your other methods
    }
    
    有些人不喜欢实用程序类,但我有点喜欢它们。如果你打算在各地使用这些方法,我认为这是一个很好的选择


  • 就我个人而言,我会选择实用程序类路线,但如果您不喜欢这些,那么按照选项2中的方式包装它也同样有用。

    您不能简单地将对象强制转换为它不是的类型。那是行不通的。为什么不给你的类一个构造函数,它接受一个BuffereImage对象,并根据传递给它的BuffereImage创建一个对象的实例呢?你是对的。我将使用第二种解决方案。在第一种解决方案中,要正确设置所有参数是很困难的。非常感谢。