2011-11-13 25 views
19

Estoy tratando de pintar un rectángulo en mi aplicación en un tono rojo, pero tengo que hacerlo de forma transparente para que el componente debajo de él todavía se muestre. Sin embargo, todavía quiero que algún color se muestre. El método en el que estoy dibujando es la siguiente:¿Cómo hacer un rectángulo en Gráficos en un color transparente?

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

¿Alguien sabe cómo puedo hacer que la sombra roja un poco transparente? No lo necesito completamente transparente sin embargo.

Respuesta

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

Véase el Color constructors que tomar 4 argumentos (int o float) para más detalles.

1

Prueba esto:

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

    } 
} 
Cuestiones relacionadas