2011-11-13 5 views
19

私は赤い色合いでアプリケーションに矩形をペイントしようとしていますが、その下のコンポーネントが表示されるように透明にする必要があります。しかし、私はまだいくつかの色が表示されることを望む。私が描いている方法は次の通りです:グラフィックスで矩形を透明な色にするには?

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

赤い色合いを少し透明にする方法は誰にも分かりますか?私はそれを完全に透明にする必要はありません。

答えて

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

は、詳細は4つの引数(intまたはfloatを)取るColor constructorsを参照してください。

1

これを試してみてください:

protected void paintComponent(Graphics 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); 

    } 
} 
関連する問題