繁体   English   中英

用洪水填充对象填充Java

[英]Filling objects with flood fill in java

这是我的问题。 我创建了一个四叶草,我想用彩色填充他。 我有这个简单的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像素

当我运行项目时,它向我显示此错误消息:

代码14、30和31的行中的线程“ AWT-EventQueue-0” java.lang.StackOverflowError中的异常

递归显然存在问题,但是我不知道如何更改代码以填充该大对象。

感谢您的任何建议和帮助。

seed_fill()调用seed_fill()为它的4个相邻,它调用seed_fill()为它的4个相邻,它调用seed_fill()为它的4个相邻,它调用seed_fill()为它的4个相邻等

因此,您最终遇到了无穷无尽的递归调用。 例如,为(0,1)调用它会为(1,1)调用它,为(0,1)调用它,会为(1,1)调用它等等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM