Java 字节帧缓冲区数组填充命令出现堆栈溢出问题

Java 字节帧缓冲区数组填充命令出现堆栈溢出问题,java,stack-overflow,Java,Stack Overflow,嘿,伙计们,我现在正在开发自己的自定义帧缓冲区,并在其中绘制基本体。这很好用 然而,我的问题是在屏幕上以纯色填充原语。例如,在本例中为圆形 问题是,我试图填补它,我得到了堆栈溢出错误(这意味着递归函数永远不会结束) 这是我用来填充区域的递归函数 private void filler(int position, byte r, byte g, byte b) { buffer[position] = r; buffer[position + 1] = g; buffer

嘿,伙计们,我现在正在开发自己的自定义帧缓冲区,并在其中绘制基本体。这很好用

然而,我的问题是在屏幕上以纯色填充原语。例如,在本例中为圆形

问题是,我试图填补它,我得到了堆栈溢出错误(这意味着递归函数永远不会结束)

这是我用来填充区域的递归函数

private void filler(int position, byte r, byte g, byte b)
{

    buffer[position] = r;
    buffer[position + 1] = g;
    buffer[position + 2] = b;
    int northPosition = position - rowLength;
    int southPosition = position + rowLength;
    int westPosition = position - 3;
    int eastPosition = position + 3;
    //fills squares west of the current
    /**/
    System.out.println(position);
    //fills squares to the east of the current 
    if(buffer[eastPosition] != r && buffer[eastPosition + 1] != g && buffer[eastPosition + 2] != b)
    {
        System.out.println("runs");
        filler(eastPosition, r, g, b);
    }
    System.out.println(position);
    //fills squares north of the current
    if(buffer[northPosition] != r && buffer[northPosition + 1] != g && buffer[northPosition + 2] != b)
    {
        filler(northPosition,r,g,b);
    }
    //fills squares south of current
    if(buffer[southPosition] != r && buffer[southPosition + 1] != g && buffer[southPosition + 2] != b)
    {
        filler(southPosition,r,g,b);
    }
    //fills squares  west of current
    if(buffer[westPosition] != r && buffer[westPosition + 1] != g && buffer[westPosition + 2] != b)
    {   
        filler(westPosition, r, g, b);
    }
}
请注意,代码的北部和南部工作得非常好,只有当代码的东部和西部一起工作时,才会发生错误(但是如果我从函数中删除其中一个,它们自己工作得很好)。如果有人发现了问题,你能解释一下为什么东西方会相互影响吗


非常感谢

我认为您正在尝试实现泛洪填充算法(请参阅,以获得一个很好的伪代码实现)。显然,您使用的是一维数组,因此必须确保不跨行(在东/西情况下)或数组边界(在所有情况下)

如果下一步与当前像素位于同一行,并且位于阵列的边界内,则只希望向东/西方向前进。所以我会像这样更改您的代码(我没有测试它):

private void filler(int位置,字节r,字节g,字节b)
{
缓冲器[位置]=r;
缓冲器[位置+1]=g;
缓冲器[位置+2]=b;
int northPosition=位置-行长度;
int southPosition=位置+行长;
int westPosition=位置-3;
int eastPosition=位置+3;
//填充水流以西的正方形
/**/
系统输出打印项次(位置);
//填充水流以东的正方形
如果(eastPosition=0&&buffer[northPosition]!=r&&buffer[northPosition+1]!=g&&buffer[northPosition+2]!=b)
{
填料(北侧位置、r、g、b);
}
//填充水流以南的正方形
如果(southPosition=0&&westPosition/rowLength==position/rowLength&&buffer[westPosition]!=r&&buffer[westPosition+1]!=g&&buffer[westPosition+2]!=b)
{   
填料(西侧位置、r、g、b);
}
}

您的方法永远不会返回。填充的每个点旁边通常至少有一个像素,因此该方法暂时不会返回,而是沿着调用堆栈填充该像素。这导致调用堆栈非常深,stackoverflow限制了要填充的区域的大小。所以如果你想使用java并填充大面积,你应该考虑另一个算法。

也添加边界条件:有南/北/东/西的位置吗?< /P> 否则向西将向北跳跃,以此类推,从而实现简单的递归。


同样,如果没有递归,索引也会越界。

什么是
filler()
的示例输入?因为它以字节为单位,并且它是一个特定位置的图片。可能是23345200,0,0没有南/北的情况下你是东/西吗?有趣的是,现在刚刚尝试过,东/西都很好。删除了南北代码,这就是。是的,这看起来就是我试图使用的。为了确保我不这样做,我在移动到下一个位置之前,检查我放置的内容是否有相同的颜色,从而检查if语句。在这一点上,我假设它变得如此递归,达到堆栈跟踪限制。。当我看到它时,它看起来越来越像我第一个想到的是跨越行,但是整个事情只有在一个完整的边界上才能工作,否则它就会陷入数组索引边界外的概念。嗯,我想我明白你在那里尝试的是什么了。然而,西/北/南/东位置只告诉它需要向哪个方向移动。因为在字节数组中,一个位置是3个字节(红、蓝、绿)。我选择使用rbg确定其边界,因为它们与填充颜色相同。无论如何,我都会试试看。*编辑*耶创建了一个索引越界错误=/耶我看得越多,它似乎越接近堆栈跟踪。如果删除其中一条语句,它可以部分填充圆。。。然而,我并不想放弃这个方法。边界条件是if条件。形状的轮廓是相同的r、g、b颜色,使其有效地充当边界。在这种情况下,我只能看到一个问题:
rowLength
不是三重的。可能是
行长*3
private void filler(int position, byte r, byte g, byte b)
{

    buffer[position] = r;
    buffer[position + 1] = g;
    buffer[position + 2] = b;
    int northPosition = position - rowLength;
    int southPosition = position + rowLength;
    int westPosition = position - 3;
    int eastPosition = position + 3;
    //fills squares west of the current
    /**/
    System.out.println(position);
    //fills squares to the east of the current 
    if(eastPosition < buffer.length && eastPosition/rowLength == position/rowLength && buffer[eastPosition] != r && buffer[eastPosition + 1] != g && buffer[eastPosition + 2] != b)
    {
        System.out.println("runs");
        filler(eastPosition, r, g, b);
    }
    System.out.println(position);
    //fills squares north of the current
    if(northPosition >= 0 && buffer[northPosition] != r && buffer[northPosition + 1] != g && buffer[northPosition + 2] != b)
    {
        filler(northPosition,r,g,b);
    }
    //fills squares south of current
    if(southPosition < buffer.length && buffer[southPosition] != r && buffer[southPosition + 1] != g && buffer[southPosition + 2] != b)
    {
        filler(southPosition,r,g,b);
    }
    //fills squares  west of current
    if(westPosition >= 0 && westPosition/rowLength == position/rowLength && buffer[westPosition] != r && buffer[westPosition + 1] != g && buffer[westPosition + 2] != b)
    {   
        filler(westPosition, r, g, b);
    }
}