Java - 当预期参数未被传递到函数中时

时间:2016-01-27 21:48:17

标签: java function parameters rgb

我正在尝试运行一个方法rgbScore(),该方法传入以前方法中的参数来计算图像的两个像素之间的差异。我希望结果是两个像素之间的差异,但我得到的是(0,0,0)......

public void TypeButtonPressed()
{

    filteredImage(GetType());
            int type = GetType();

            filteredrgbValue();
            originalrgbValue();
            rgbScore(originalred, filteredred,  originalgreen, filteredgreen, originalblue, filteredblue); //THIS METHOD

            JOptionPane.showMessageDialog(null, "Original RGB Value:" + oRGBValue + "\nFiltered RGB Value:" + fRGBValue + "\nScore:" + rgbDiff, Options[type], JOptionPane.INFORMATION_MESSAGE);


}

...

public String rgbScore(int originalred, int filteredred, int originalgreen, int filteredgreen, int originalblue, int filteredblue){

        int redDiff = originalred - filteredred;
        int greenDiff = originalgreen - filteredgreen;
        int blueDiff = originalblue - filteredblue;

        return rgbDiff = "(" + redDiff + "," + greenDiff + "," + blueDiff + ")";


     }

我已经在类的顶部声明了变量,如下所示:

public int filteredred; public int filteredgreen; public int filteredblue;
public int originalred; public int originalgreen; public int originalblue;

<其他信息>

filteredrgbValue();

public String filteredrgbValue(){ 
        BufferedImage filtered = filteredImage;

        Color filteredRGBValue = new Color(filtered.getRGB(125, 125));

        int filteredred = filteredRGBValue.getRed();
        int filteredgreen = filteredRGBValue.getGreen();
        int filteredblue = filteredRGBValue.getBlue();

        return fRGBValue = "(" + filteredred + "," + filteredgreen + "," + filteredblue + ")";        

    }

originalrgbValue();

public String originalrgbValue(){
        BufferedImage original = unfilteredImage;

        Color originalRGBValue = new Color(original.getRGB(125, 125));

        int originalred = originalRGBValue.getRed();
        int originalgreen = originalRGBValue.getGreen();
        int originalblue = originalRGBValue.getBlue();

        return oRGBValue = "(" + originalred + "," + originalgreen + "," + originalblue + ")";     

    }

2 个答案:

答案 0 :(得分:1)

看起来你从未初始化变量,你刚刚宣布了变量。 int的defualt值为0,直到你没有初始化你的变量,它将具有默认值。

答案 1 :(得分:1)

您正在方法中创建隐藏成员变量的新变量。删除类型声明,例如:

public String filteredrgbValue(){ 
        BufferedImage filtered = filteredImage;

        Color filteredRGBValue = new Color(filtered.getRGB(125, 125));

        // you are declaring new variables here, hiding your member variables...
        //int filteredred = filteredRGBValue.getRed();
        //int filteredgreen = filteredRGBValue.getGreen();
        //int filteredblue = filteredRGBValue.getBlue();

        filteredred = filteredRGBValue.getRed();
        filteredgreen = filteredRGBValue.getGreen();
        filteredblue = filteredRGBValue.getBlue();

        return fRGBValue = "(" + filteredred + "," + filteredgreen + "," + filteredblue + ")";        
    }