21

アプリケーションで長方形を赤い色合いでペイントしようとしていますが、その下のコンポーネントが引き続き表示されるように、ある種の透明にする必要があります。しかし、私はまだいくつかの色がまだ表示されることを望んでいます。私が描いている方法は次のとおりです。

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);
        }
    }
}

赤い色合いを少し透明にする方法を知っている人はいますか?完全に透明にする必要はありません。

4

2 に答える 2

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

詳細については、 ( または のいずれかの) 4 つの引数を取るColorコンストラクターを参照してください。intfloat

于 2011-11-13T10:43:42.693 に答える
2

これを試してください: (ただし、Graphics ではなく Graphics2D オブジェクトで機能します)

protected void paintComponent(Graphics2D g) {
    if (point != null) {
        int value = this.chooseColour(); // used to return how bright the red is needed
        g.setComposite(AlphaComposite.SrcOver.derive(0.8f));

        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);
        }

        g.setComposite(AlphaComposite.SrcOver); 
    }
}
于 2011-11-13T12:39:14.620 に答える