8連結境界塗りを使用してポリゴンを塗りたい。 4連結境界塗りのコードは動作します。ただし、8連結境界フィルの対角位置をテストするための4つの追加ステートメントを追加すると、ArrayIndexOutOfBoundsExceptionが発生します。境界外の座標!エラー。問題は何ですか?解決する方法は?8連結境界塗りの範囲外の座標
private void bfill(int x, int y, Color fillColour, Color borderColour){
Stack<Point> points = new Stack<>();
points.add(new Point(x, y));
while(!points.isEmpty()) {
Point currentPoint = points.pop();
x = currentPoint.x;
y = currentPoint.y;
Color interiorColour = new Color(bi.getRGB(x, y));
if (!interiorColour.equals(borderColour) && !interiorColour.equals(fillColour)){
setPixel(x, y); //draw pixel
points.push(new Point(x+1, y));
points.push(new Point(x-1, y));
points.push(new Point(x, y+1));
points.push(new Point(x, y-1));
//Error occurs when the next four lines are uncommented for 8-connected boundary fill
/*points.push(new Point(x+1, y+1));
points.push(new Point(x+1, y-1));
points.push(new Point(x-1, y-1));
points.push(new Point(x-1, y+1));*/
}
}
}
編集:gpaschの答えの後、私は境界チェックが含まれています。しかし、プログラムは無限に実行されます。境界チェックで何が問題になっていますか?
if (!interiorColour.equals(borderColour) && !interiorColour.equals(fillColour)){
if (x > -1 && y > -1 && x < getWidth() && y < getHeight()){
setPixel(x, y); //draw pixel
if (x+1 < getWidth()) points.push(new Point(x+1, y));
if (x-1 > -1) points.push(new Point(x-1, y));
if (y+1 < getHeight()) points.push(new Point(x, y+1));
if (y-1 > -1) points.push(new Point(x, y-1));
if (x+1 < getWidth() && y+1 < getHeight()) points.push(new Point(x+1, y+1));
if (x+1 < getWidth() && y-1 > -1) points.push(new Point(x+1, y-1));
if (x-1 > -1 && y-1 > -1) points.push(new Point(x-1, y-1));
if (x-1 > -1 && y+1 > getHeight()) points.push(new Point(x-1, y+1));
}
}
}
例外のスタックトレースを追加してください。プッシュ呼び出しでArrayIndexOutOfBoundsExceptionをスローすることはできません。 – Bax