Ich möchte ein Polygon mit 8-verbundener Grenzfüllung füllen. Der Code für die 4-verknüpfte Grenzfüllung funktioniert. Wenn ich jedoch die vier zusätzlichen Anweisungen hinzufüge, um diagonale Positionen für die 8-verbundene Grenzfüllung zu testen, bekomme ich ArrayIndexOutOfBoundsException: Coordinate out of bounds! Fehler. Was ist das Problem und wie löst man es?Koordinaten außerhalb der Grenze in 8-zusammenhängender Grenzfüllung
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));*/
}
}
}
bearbeiten: gpasch Antwort Folgen, I der Grenzen zu überprüfen. Das Programm läuft jedoch endlos. Was stimmt nicht mit der Überprüfung der Grenzen?
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));
}
}
}
Können Sie bitte das Stacktrace der Ausnahme hinzufügen, die Sie erhalten, ein Push-Aufruf kann keine ArrayIndexOutOfBoundsException auslösen. – Bax