0

我有这种方法,可以制作渐变,但由于某种原因,我无法让渐变具有任何不透明度,例如60% opaque.

public static int[] linear(int x1, int y1, int x2, int y2, Color color1, Color color2, int width, int height){
    BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    int[] pixels = new int[width * height];
    Graphics2D g = bimg.createGraphics();
    g.setPaint(new GradientPaint(x1, y1, color1, x2, y2, color2, false));
    g.fillRect(0, 0, width, height);
    bimg.getRGB(0, 0, width, height, pixels, 0, width);
    return pixels;

}

然后我这样称呼它:

int pink = Colors.rgba(187, 61, 186, 153);
int yellow = Colors.rgba(209, 192, 8, 153);
this.spixels = Gradient.linear(0, 0, img.getWidth(), 0, pink, yellow, img.getWidth(), img.getHeight());

我不能为我的生活得到渐变60% opaque。我该怎么做才能让它变成那样?

这里有一些更多的背景:

我有一个图像,然后创建一个与该图像大小相同的渐变(使用上面的代码)。接下来,我使用以下方法将两个图像混合在一起lighten

public static int lighten(int bg, int fg){
    Color bgc = new Color(bg);
    Color fgc = new Color(fg);
    int r = Math.max(bgc.getRed(), fgc.getRed());
    int g = Math.max(bgc.getGreen(), fgc.getGreen());
    int b = Math.max(bgc.getBlue(), fgc.getBlue());
    int a = Math.max(bgc.getTransparency(), fgc.getTransparency());
    Color f = new Color(r, g, b, a);
    return f.getRGB();
}

无论我使渐变变得多么透明,lighten 似乎都没有捕捉到它,并将它与全色混合,并忽略渐变的透明度。

4

1 回答 1

0

Definen a Composite object like this

private static Composite comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f);

In linear() method, just set the composite before g.fillRect()

The following code snippet, demonstrates some thing like this in painting method of one of my codes...

        gg.setComposite(comp);
        Color ec = gg.getColor();

        gg.setColor(Color.darkGray);

        Shape s = gg.getClip();
        if (s != null)
            gg.fill(s);

        gg.setComposite(existing);
        gg.setColor(ec);
于 2013-05-18T22:05:25.200 回答