Java颜色未定义

时间:2013-11-22 09:09:44

标签: java colors

我试图制作棕褐色效果,我需要将一些颜色减少到一定的百分比。我的编译器错误是:

Error: The constructor java.awt.Color(double, double, double) is undefined

Error: The constructor java.awt.Color(int, int, double) is undefined

Error: The constructor java.awt.Color(int, int, double) is undefined

这是我的代码:

public void sepiaTint()
  {
    Pixel[] pixelArray = this.getPixels();
    for (int i = 0; i < pixelArray.length; i++)
    {
     Pixel pixelObj = pixelArray[i];
     int amountRed = pixelObj.getRed();
     int amountGreen = pixelObj.getGreen();
     int amountBlue = pixelObj.getBlue();

     if (amountRed < 60)
     {
       Color newColor = new Color(amountRed*0.9, amountGreen*0.9, amountBlue*0.9);
       pixelObj.setColor(newColor);
     }
     if (amountRed >= 60 && amountRed <190)
     {
       Color newColor = new Color(amountRed, amountGreen, amountBlue*0.8);
       pixelObj.setColor(newColor);
     }
     else
     {
       Color newColor = new Color(amountRed, amountGreen, amountBlue*0.9);
       pixelObj.setColor(newColor);
     }
    }
    this.repaint();
  }

3 个答案:

答案 0 :(得分:1)

Color() cunstructor应该是这样的

 new Color(amountRed*0.9F, amountGreen*0.9F, amountBlue*0.9F)

new Color((int)(amountRed*0.9), (int)(amountGreen*0.9), (int)(amountBlue*0.9))
new Color(amountRed, amountGreen, (int)amountBlue*0.8)

0.9被视为doubleColor没有任何具有double参数的cunstructor。因此,0.9应转换为float,如0.9F

答案 1 :(得分:1)

Pixel.getRed()返回一个整数,因此Color(float, float, float)对你没用。你想要的是Color(int, int, int)构造函数。

0.9被解释为double,因此您必须在计算值后再将其强制转换为int。

Color newColor = new Color((int)(amountRed*0.9), (int)(amountGreen*0.9), (int)(amountBlue*0.9));

答案 2 :(得分:0)

试试这个..

Color newColor = new Color(amountRed*0.9F, amountGreen*0.9F, amountBlue*0.9F);