不兼容的类型:void无法转换为int,Pixel相关

时间:2016-07-30 20:10:20

标签: java

编辑:我曾尝试使用其他问题来回答我自己的问题,但更改方法的返回类型并没有帮助。我还是输了。 我对Java很新,所以如果编码很奇怪,我很抱歉。但是,我正在尝试制作一个采用图像的程序,并使用嵌套循环来更改每个像素值。所以,我试图通过从255减去每个像素值来使图像变为“负”。这是我的程序:

import java.awt.*;           
class TrueColors
{
    TrueColors()       
    {
    }

    public void negative()
    {
        Picture pictureObj = new Picture("washingtonmonument.jpg");     //creates a new Picture object representing the file in the parameter list                 
        pictureObj.explore();                                           //explore the Picture object which is currently the unaltered original image
        int redValue = 0; int greenValue = 0; int blueValue = 0;        //declare and initialize the variables that hold the red, green, and blue values (0-255)

        Pixel targetPixel = new Pixel(pictureObj, 0,0);                //set the coordinate for the image origin
        Color pixelColor = null;                                        //declare a Color object and set its initial value to null (or nothing)          

        for(int y=0; y < pictureObj.getHeight(); y++)                   //outside nested loop to traverse the image from top to bottom
        {
            for(int x = 0; x < pictureObj.getWidth(); x++)              //inside nested loop to traverse the image from left to right
            {
                targetPixel = pictureObj.getPixel(x,y);                 //gets the x,y coordinate of the target pixel
                pixelColor = targetPixel.getColor();                    //gets the color of the target pixel

                redValue = pixelColor.getRed();                         //assign the red component (0-255) of the target pixel to the redValue variable 
                greenValue = pixelColor.getGreen();                     //assign the green component (0-255) of the target pixel to the greenValue variable
                blueValue = pixelColor.getBlue();                       //assign the blue component (0-255) of the target pixel to the blueValue variable
                redValue = targetPixel.setRed(255-redValue);
                greenValue = targetPixel.setGreen(255-greenValue);
                blueValue = targetPixel.setBlue(255-blueValue);
                pixelColor = new Color(redValue, greenValue, blueValue);    
                targetPixel.setColor(pixelColor);                           

            }//end of the inner for loop
        }//end of the outer for loop
        pictureObj.explore();                                           //explore the Picture object which is now the altered image
        pictureObj.write("NewWashingtonMonument.jpg");                  //write the altered Picture object to a new file
        pictureObj.show();                                              //show the altered Picture object (not in the explore tool)
    }
}

public class TrueColorsTester                                                 //start of the class
{
    public static void main(String[] args)                              //start of the main method
    {
        TrueColors tc = new TrueColors();

        tc.negative();


    }//end of main method
}//end of class

所以,当我编译这段代码时,我得到错误:“不兼容的类型:void无法转换为int”,它突出显示:

redValue = targetPixel.setRed(255-redValue);

具体是255-redValue。我假设这会发生在我尝试从255中减去该值的每个语句。有人可以帮忙吗?

0 个答案:

没有答案