递归图像画家

时间:2017-05-02 16:59:38

标签: java recursion paint

我正在尝试创建一个递归函数,用作填充特定区域的油漆桶。这个绘制了一个用户点击的正方形:

 public void drawSquare(int x, int y, Color paintColor) {
    int paintColorRGB = clearAlphaChannel(paintColor.getRGB());

    image.setRGB(x - 1, y - 1, paintColorRGB);
    image.setRGB(x + 0, y - 1, paintColorRGB);
    image.setRGB(x + 1, y - 1, paintColorRGB);
    image.setRGB(x - 1, y + 0, paintColorRGB);
    image.setRGB(x + 0, y + 0, paintColorRGB);
    image.setRGB(x + 1, y + 0, paintColorRGB);
    image.setRGB(x - 1, y + 1, paintColorRGB);
    image.setRGB(x + 0, y + 1, paintColorRGB);
    image.setRGB(x + 1, y + 1, paintColorRGB);
}

这里调用递归函数:

public void transformImage(int x, int y, Color paintColor) {
    int paintColorRGB = clearAlphaChannel(paintColor.getRGB());

    transformPoint(x, y, getPixelColor(x, y), paintColorRGB);
}

getPixelColor计算3x3像素正方形的平均颜色,在这种情况下,阈值由用户定义,treshold = 4:

private int getPixelColor(int x, int y) {
        int pixelColor = clearAlphaChannel(image.getRGB(x, y));
        int i= 9;
        int cor0 = clearAlphaChannel(image.getRGB(x,y));
        int cor1 = clearAlphaChannel(image.getRGB(x+1,y));
        int cor2 = clearAlphaChannel(image.getRGB(x,y+1));
        int cor3 = clearAlphaChannel(image.getRGB(x-1,y));
        int cor4 = clearAlphaChannel(image.getRGB(x,y-1));
        int cor5 = clearAlphaChannel(image.getRGB(x+1,y+1));
        int cor6 = clearAlphaChannel(image.getRGB(x-1,y-1));
        int cor7 = clearAlphaChannel(image.getRGB(x+1,y-1));
        int cor8 = clearAlphaChannel(image.getRGB(x-1,y+1));

        if(cor0<threshold){
            i = i-1;
            cor0 = 0;
        }
        if(cor1<threshold){
            i = i-1;
            cor1 = 0;
        }
        if(cor2<threshold){
            i = i-1;
            cor2 = 0;
        }
        if(cor3<threshold){
            i = i-1;
            cor3 = 0;
        }
        if(cor4<threshold){
            i = i-1;
            cor4 = 0;
        }
        if(cor5<threshold){
            i = i-1;
            cor5 = 0;
        }
        if(cor6<threshold){
            i = i-1;
            cor6 = 0;
        }
        if(cor7<threshold){
            i = i-1;
            cor7 = 0;
        }
        if(cor8<threshold){
            i = i-1;
            cor8 = 0;
        }


        pixelColor= (cor1+cor2+cor3+cor4+cor5+cor6+cor7+cor8+cor0)/i;

        System.out.println(pixelColor);



    return pixelColor;
}

这个递归函数应该绘制黑色区域内的区域,但它会给我一个堆栈溢出:

private void transformPoint(int x, int y, int refColorRGB, int paintColorRGB) {
    int pixelRGB = clearAlphaChannel(image.getRGB(x, y));
    if(refColorRGB!=pixelRGB){
        return;     }
    // Image width: 0 to imageWidth -1
    // Image height: 0 to imageHeight -1
    // Threshold defined in GUI: threshold


    transformPoint(x+1, y,  refColorRGB,  paintColorRGB);//1
    transformPoint(x, y-1,  refColorRGB,  paintColorRGB);//2
    transformPoint(x, y+1,  refColorRGB,  paintColorRGB);//3
    transformPoint(x-1, y,  refColorRGB,  paintColorRGB);//4


    drawSquare(x, y, new Color(paintColorRGB));

}

现在只调用第一个transformPoint,但是当我尝试添加其他的时,它不起作用。 butterfly

0 个答案:

没有答案