找不到符号-方法setColor(java.awt.Color)?
我试图为变为灰度的图像创建一个新的调色板,然后将调色板应用于灰度图像。我开始使用我想用来应用调色板的方法,但是我遇到了标题中提到的错误。我已经在代码中使用了“java.awt.Color”,所以我不确定为什么会出现错误。另外,正如您将看到的,我在括号内放置了一种颜色找不到符号-方法setColor(java.awt.Color)?,java,Java,我试图为变为灰度的图像创建一个新的调色板,然后将调色板应用于灰度图像。我开始使用我想用来应用调色板的方法,但是我遇到了标题中提到的错误。我已经在代码中使用了“java.awt.Color”,所以我不确定为什么会出现错误。另外,正如您将看到的,我在括号内放置了一种颜色 /** * This program takes an image, converts it to grayscale, and uses a color palette to create new colors for the
/**
* This program takes an image, converts it to grayscale, and uses a color palette to create new colors for the image.
*
* @author Dylan Hubbs
* @version 08/02/16
*/
import java.awt.Color ;
class ColorPalette
{
public void grayscaleEffect(Picture pictureObj)
{
int redValue = 0; int greenValue = 0; int blueValue = 0;
Pixel grayscaleTargetPixel = new Pixel(pictureObj, 0,0);
Color grayscalePixelColor = null;
for(int y=0; y < pictureObj.getHeight(); y++)
{
for(int x = 0; x < pictureObj.getWidth(); x++)
{
grayscaleTargetPixel = pictureObj.getPixel(x,y);
grayscalePixelColor = grayscaleTargetPixel.getColor(); //gets the color of the target pixel
grayscalePixelColor = new Color((grayscaleTargetPixel.getRed() + grayscaleTargetPixel.getGreen() + grayscaleTargetPixel.getBlue()) / 3, (grayscaleTargetPixel.getRed() + grayscaleTargetPixel.getGreen() + grayscaleTargetPixel.getBlue()) / 3, (grayscaleTargetPixel.getRed() + grayscaleTargetPixel.getGreen() + grayscaleTargetPixel.getBlue()) / 3);
grayscaleTargetPixel.setColor(grayscalePixelColor); //sets the new color of the target pixel
}//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("grayscaleWashingtonMonument.jpg"); //write the altered Picture object to a new file
pictureObj.show();
}
public void paletteEffect(Picture pictureObj)
{
int redValue = 0; int greenValue = 0; int blueValue = 0;
Pixel paletteTargetPixel = new Pixel(pictureObj, 0,0);
Color palettePixelColor = null;
Color [] palette = {Color.RED, Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW, Color.GRAY, Color.PINK, Color.ORANGE};
for(int y=0; y < pictureObj.getHeight(); y++)
{
for(int x = 0; x < pictureObj.getWidth(); x++)
{
paletteTargetPixel = pictureObj.getPixel(x,y);
palettePixelColor = paletteTargetPixel.getColor();
if(paletteTargetPixel.getRed() >= 1 && paletteTargetPixel.getRed() <= 31)
palettePixelColor.setColor(palette[0]);
else if(paletteTargetPixel.getRed() >= 32 && paletteTargetPixel.getRed() <= 62)
palettePixelColor.setColor(palette[1]);
else if(paletteTargetPixel.retRed() >= 63 && paletteTargetPixel.getRed() <=93)
palettePixelColor.setColor(palette[2]);
}
}
}
}
public class ColorPaletteTester
{
public static void main(String[] args)
{
Picture pictureObj = new Picture("washingtonmonument.jpg"); //creates a new Picture object representing the file in the parameter list
pictureObj.explore();
ColorPalette cp = new ColorPalette();
cp.grayscaleEffect(pictureObj);
cp.paletteEffect(pictureObj);
}
}
有人知道为什么会发生这种情况吗?palettePixelColor被声明为,它恰好是一个不带设置器的不可变类。根据什么是
像素
,它可能有这样一种方法
你可能正在尝试做类似的事情
palettePixelColor = palette[0];
或
您可能想编写
palettePixelColor=palette[0]
我要冒险在这里说,Color
类没有名为setColor()
的方法,我以前使用过setColor方法,但是将变量分配到数组的索引位置更有意义。谢谢,请包括您丢失的导入内容。例如,像素、图片等。是的,我想我的意思是放置目标像素。非常感谢。SOP是选择(并在获得代表后进行投票)。
palettePixelColor = palette[0];
paletteTargetPixel.setColor(palette[0]);