【发布时间】:2018-09-03 00:07:26
【问题描述】:
我目前正在为游戏中的角色开发动态动画加载器,为此我需要检测当前帧是否完全空白以停止加载更多精灵。 这是我目前用来确定当前图像是否为空白的:
public static boolean isBlankImage(BufferedImage b) {
byte[] pixels1 = getPixels(b);
byte[] pixels2 = getPixels(getBlankImage(b.getWidth(), b.getHeight()));
return Arrays.equals(pixels1, pixels2);
}
private static BufferedImage getBlankImage(int width, int height) {
return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}
private static byte[] getPixels(BufferedImage b) {
byte[] pixels = ((DataBufferByte) b.getRaster().getDataBuffer()).getData();
return pixels;
}
但是,一旦我运行它,我就会收到这个烦人的错误:
Exception in thread "Thread-0" java.lang.ClassCastException:
java.awt.image.DataBufferInt cannot be cast to java.awt.image.DataBufferByte
我尝试过切换投射类型,但得到的回报是:
Exception in thread "Thread-0" java.lang.ClassCastException:
java.awt.image.DataBufferByte cannot be cast to java.awt.image.DataBufferInt
我到处寻找答案都无济于事,所以这是我的问题:是否有更好的功能性方法来检查图像是否完全透明?
任何帮助将不胜感激。
【问题讨论】:
-
什么是“空”图像?全白的东西,100% 透明的东西?
-
谢谢@AndrewThompson,我会改变的。
-
@Kwright02。后者(正如对问题的编辑中所阐明的那样)。
标签: java image classcastexception