找不到适用于Color(int,int,int、double)的构造函数



我正在尝试编辑一个图像,以便对其进行"彩虹"编辑。要做到这一点,我只想填充单独的行,用彩虹的颜色填充图像。然而,如果我这样做,颜色会覆盖图像。所以,我想给我的颜色增加透明度。然而,当我尝试这样做时,我遇到了标题中提到的错误消息。这是我的代码:

 /**
 * This program takes a picutre, and places a special effect on it that colors it similar to how a rainbow would be.
 * 
 * @author Dylan Hubbs
 * @version 08/03/16
 */
import java.awt.*;    
class Rainbow
{
    Rainbow()
    {
    }
    public void rainbowEffect(Picture pictureObj)
    {
        int redValue = 0; int greenValue = 0; int blueValue = 0;
        Pixel rainbowTargetPixel = new Pixel(pictureObj, 0,0);               
        Color rainbowPixelColor = null;
        Color [] rainbowPalette = {new Color(255, 0, 0, .5), new Color(255, 200, 0, .5), Color(255, 255, 0, .5), Color(0, 255, 0, .5), Color(0, 0, 255, .5), new Color(138, 43, 226, .5), new Color(75, 0, 130, .5)};
        for(int y=0; y < pictureObj.getHeight(); y++)
        {
            for(int x = 0; x < pictureObj.getWidth(); x++)
            {
                rainbowTargetPixel = pictureObj.getPixel(x,y);                
                if(y >= 0 && y <= 50)
                rainbowTargetPixel.setColor(rainbowPalette[0]);
                if(y >= 51 && y <= 100)
                rainbowTargetPixel.setColor(rainbowPalette[1]);
                if(y >= 101 && y <= 150)
                rainbowTargetPixel.setColor(rainbowPalette[2]);
                if(y >= 151 && y <= 200)
                rainbowTargetPixel.setColor(rainbowPalette[3]);
                if(y >= 201 && y <= 250)
                rainbowTargetPixel.setColor(rainbowPalette[4]);
                if(y >= 251 && y <= 300)
                rainbowTargetPixel.setColor(rainbowPalette[5]);      
            }
        }
        pictureObj.explore();                                           
        pictureObj.write("RainbowWashingtonMonument.jpg");                 
        pictureObj.show();                                              
    }
}
public class RainbowTester
{
    public static void main(String[] args)
    {
        Picture pictureObj = new Picture("washingtonmonument.jpg");
        pictureObj.explore();
        Rainbow rb = new Rainbow();
        rb.rainbowEffect(pictureObj);
    }
}

因此,错误出现在

{新颜色(255,0,0,.5)

而且,我相信对于数组中的其他颜色,错误还会继续。当我尝试在每个方法的末尾添加浮动值时,不会发生此错误。。。然而,我想我可以添加浮点数来提高透明度吗?请帮忙!

https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html

显示了Color的多个构造函数。一个可能适合你的:

颜色(int r,int g,int b,int a)

您只需输入128(integer),而不是0.5(double

Java无法确定要调用哪个构造函数。由于alpha的值是0.5,所以您可能想要一个取四个float的值:

new Color(1F, 0, 0, .5F)

请注意使用1代替255,因为此构造函数的取值范围为0..1。

演示。

构造函数占用四个int是另一种可能性,在这种情况下,您需要为alpha参数传递128

new Color(255, 0, 0, 128)

演示。

构造函数很少:

  • public Color(int r, int g, int b)
  • public Color(int r, int g, int b, int a)
  • public Color(int rgb)
  • public Color(int rgba, boolean hasalpha)
  • public Color(float r, float g, float b)
  • public Color(float r, float g, float b, float a)
  • public Color(ColorSpace cspace, float components[], float alpha)

您可以使用

public Color(int r, int g, int b, int a)(创建一种sRGB颜色,指定的红色、绿色、蓝色和alpha值在(0-255)范围内。)

public Color(float r, float g, float b, float a)(创建一种sRGB颜色,指定的红色、绿色、蓝色和alpha值在(0.0-1.0)范围内。渲染中使用的实际颜色取决于在给定特定输出设备可用的颜色空间的情况下找到最佳匹配。)

最新更新