如何使图形中的矩形以透明颜色? [英] How to make a rectangle in Graphics in a transparent colour?

查看:247
本文介绍了如何使图形中的矩形以透明颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的应用程序上绘制一个矩形,在一个红色的阴影,但我需要使它的透明,使它下面的组件仍然会显示。然而我仍然想要一些颜色仍然会显示。我绘制的方法如下:

I'm trying to paint a rectangle on my application in a red shade but I need to make it sort of transparent so that the component under it will still show. However I still want that some colour will still show. The method where I'm drawing is the following:

protected void paintComponent(Graphics g) {
    if (point != null) {
        int value = this.chooseColour(); // used to return how bright the red is needed

        if(value !=0){
            Color myColour = new Color(255, value,value );
            g.setColor(myColour);
            g.fillRect(point.x, point.y, this.width, this.height);
        }
        else{
            Color myColour = new Color(value, 0,0 );
            g.setColor(myColour);
            g.fillRect(point.x, point.y, this.width, this.height);
        }
    }
}

使红色的阴影有点透明吗?我不需要它完全透明。

Does anyone know how I can make the red shade a bit transparent? I don't need it completely transparent though.

推荐答案

int alpha = 127; // 50% transparent
Color myColour = new Color(255, value, value, alpha);

请参阅 Color c> int 或 float )。

See the Color constructors that take 4 arguments (int or float) for further details.

这篇关于如何使图形中的矩形以透明颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆