gpt4 book ai didi

java - Slick 中 getColor() 方法上的 ArrayIndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-12-02 07:41:36 24 4
gpt4 key购买 nike

我有这段代码来创建一个 ArrayList,其中包含实际存在像素的所有像素位置(alpha != 0)。

代码如下:

public ArrayList<Point> getPixels() {
ArrayList<Point> output = new ArrayList<Point>();
Image frameImage = img.getCurrentFrame();
for (int FIx = 0; FIx <= img.getWidth(); FIx++) {
for (int FIy = 0; FIy <= img.getHeight(); FIy++) {
if (frameImage.getColor(FIx, FIy).getAlpha() != 0.00f) {//<-- Error
output.add(new Point(FIx, FIy));
}
}
}

return output;
}

循环可以很好地完成几次,没有任何错误,但在假定的随机时间运行时,它会给出以下错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 32768
at org.newdawn.slick.Image.getColor(Image.java:1248)
at com.SourCherry.games.KittySniper.Enemy.Kitten.getPixels(Kitten.java:197)

我已经用注释标记了提到的行 (Kitten.java:197)。

如果需要其他任何内容来帮助解决此问题,请在评论中提问。谢谢。

最佳答案

这对我来说似乎是个问题:

for (int FIx = 0; FIx <= img.getWidth(); FIx++) {
for (int FIy = 0; FIy <= img.getHeight(); FIy++) {

您假设它的像素范围包括getWidthgetHeight。我强烈怀疑这些是专有上限:

for (int FIx = 0; FIx < img.getWidth(); FIx++) {
for (int FIy = 0; FIy < img.getHeight(); FIy++) {

例如,宽度为 3 的图像的有效 X 值应为 0、1 和 2 - 而不是 3。

诚然,这取决于 org.newdawn.slick.Image 的作用,这是一个我不熟悉的类 - 但它是一个合理的起点。 (很遗憾,该方法显然没有验证其输入 - 它应该抛出不同的异常,但这仍然是您的编程错误。)

关于java - Slick 中 getColor() 方法上的 ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11522945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com