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中减去该值的每个语句。有人可以帮忙吗?