如何淡出Java2D Area对象的边缘?

时间:2016-06-23 07:59:18

标签: java gradient shadow java-2d area

我有一种方法可以根据屏幕上的内容创建阴影,该方法的最终产品是一个区域,然后我将其绘制到屏幕上,它包含同一区域中屏幕上的所有阴影。这是因为绘制的阴影是低不透明度的,所以如果它不是一件事就会重叠,不透明度会使它看起来很奇怪。

我希望阴影看起来像淡出的问题,但我完全不知道如何,或者甚至是否可能。有没有办法软化区域的边缘或使它们渐变淡出?我试图做graphics2D.setPaint(新的GradientPaint [渐变效果]),但它什么也没做。

提前致谢

编辑:Here以下是该计划的一个屏幕截图,围绕一座建筑物制作阴影。长方形。绿色矩形用于显示阴影的效果。我想要的最终结果不是阴影突然结束,它应该淡出。

1 个答案:

答案 0 :(得分:0)

我猜你在为渐变设置正确的颜色时遇到了问题:

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    g.setColor(Color.GREEN);            
    g.fillRect(30, 30, 50, 20);

    Graphics2D g2d = (Graphics2D) g;
    Polygon p = new Polygon(new int[]{10,20,50,50,40,10}, new int[]{10,10,40,50,50,20}, 6);
    Area a = new Area(p);

    Color b = new Color(0xFF000000, true); //using colors with transparency!!!
    Color w = new Color(0x00FFFFFF, true); //using colors with transparency!!!

    //try to use proper values for the x/y values - both, start and end
    GradientPaint gradient = new GradientPaint(0, 0, b, 40, 40, w);                     
    g2d.setPaint(gradient);
    g2d.fill(a); // fill your area!
}

结果是带有alpha(透明度)的渐变......

red - no gradient enter image description here

以红色绘制的区域< ==>用渐变

绘制的区域

请勿忘记将100%不透明(0xAARRGGBB为黑色100%不透明)的Alpha值FF设置为0xFF000000为0(0x00FFFFFF白色,100%透明)

相关问题