这是我的问题。我创造了一个四叶草,我想用颜色填充他。 我有这个简单的seed_fill(flood_fill):
package projekt;
class SeedFill {
private int width;
private int height;
private int[] canvas;
public SeedFill(int width, int height, int[] canvas){
this.width = width;
this.height = height;
this.canvas = canvas;
}
public void seed_fill(int x, int y){
int coord = 3*x + 3*y * width;
if( x < 0 || y < 0 || x >= width || y >= height)return;
if(canvas[coord] == 0 && canvas[coord+1] == 255 && canvas[coord+2] == 0)return;
if(canvas[coord] == 255 && canvas[coord+1] == 255 && canvas[coord+2] == 255)return;
//colors
canvas[coord] = 255;
canvas[coord+1] = 255;
canvas[coord+2] = 255;
//recursive
seed_fill(x,y-1);
seed_fill(x,y+1);
seed_fill(x-1,y);
seed_fill(x+1,y);
}
}
三叶草总共是400x400像素
当我运行项目时,它会显示以下错误消息:
线程中的异常&#34; AWT-EventQueue-0&#34; java.lang.StackOverflowError的 在第14,30和31行代码
显然递归存在问题,但我不清楚如何更改代码以填充该大对象。
感谢您的任何建议和帮助。
答案 0 :(得分:1)
seed_fill()
为其4个邻居调用seed_fill()
,为其4个邻居调用seed_fill()
,为其4个邻居调用seed_fill()
,调用seed_fill()
为其4个邻居等。
所以你最终会有一个无休止的回复电话。例如,调用它(0,1)将其调用为(1,1),调用它为(0,1),调用它为(1,1)等。