简体   繁体   English

如何在半透明的小程序中绘制一个填充的矩形?

[英]How can I draw a filled rectangle in an applet with half opacity?

如何在半透明/半透明的小程序中绘制一个填充的矩形?

Yup. 对。

void foo(Graphics g) {
  g.setColor(new Color(.3f, .4f, .5f, .6f);
  g.fillRect(50, 50, 100, 100);
}

What API are you using? 您正在使用什么API? If you use Graphics from Java2D, when you create Color objects, you can add transparency to them as an alpha between 0 and 1. 如果使用Java2D中的Graphics,则在创建Color对象时,可以向它们添加透明度,以0到1之间的alpha值表示。

Here's an old article on Java2D that has some examples 这是有关Java2D的旧文章,其中包含一些示例

Paul Murray's answer is exactly right. 保罗·默里(Paul Murray)的回答完全正确。 But to clarify, the mixing and matching of the 4 color switches is what makes different colors. 但是需要澄清的是,这四个颜色开关的混合和匹配是造成不同颜色的原因。 Here are some of the basics to help you get started. 以下是一些可以帮助您入门的基础知识。

g.setColor(new Color(1f, 0f, 0f, 1f)); //RED
g.setColor(new Color(0f, 1f, 0f, 1f)); //GREEN
g.setColor(new Color(0f, 0f, 1f, 1f)); //BLUE
g.setColor(new Color(0f, 0f, 0f, 1f)); //BLACK
g.setColor(new Color(1f, 1f, 1f, 1f)); //WHITE

and if you use decimal numbers you can make the color opaque. 如果使用十进制数字,则可以使颜色不透明。 so: 所以:

g.setColor(new Color(.5f, 0f, 0f, .5f)); //RED

would be 50% opaque and red. 将为50%不透明和红色。 then you can draw whatever with that opaque color. 然后您就可以用不透明的颜色绘制任何东西。 such as: 如:

g.fillRect(50, 50, 100, 100);

as Paul stated 如保罗所说

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

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