简体   繁体   English

Java在图形对象上设置透明度而不是颜色对象

[英]Java Set Transparency on Graphics object not Color object

Is there any way to set the transparency of a Graphics or Graphics2D object in Java as opposed to a Color object? 有什么方法可以设置Java中Graphics或Graphics2D对象(而不是Color对象)的透明度?

Something like this is what I want: 我想要的是这样的东西:

graphics.setAlpha(0.5);
graphics.setColor(Color.BLACK);
graphics.fillRect(0, 0, 100, 100);
graphics.setAlpha(1);

Any insight? 有见识吗? This is a simplified version of what I'm doing, please don't reply with workarounds, I would just like to know if this is possible. 这是我正在做的事情的简化版本,请不要提供任何解决方法,我只想知道这是否可行。

So after a few irrelevant answers and some more digging I found the solution. 因此,经过一些不相关的答案和更多的挖掘,我找到了解决方案。 The AlphaComposite class. AlphaComposite类。

used like this: 像这样使用:

Composite originalComposite = graphics.getComposite();
AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
graphics.setComposite(alphaComposite);
graphics.setColor(Color.BLACK);
graphics.fillRect(0, 0, 100, 100);
graphics.setComposite(originalComposite);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM