2011-11-13 42 views
19

Tôi đang cố gắng vẽ một hình chữ nhật trên ứng dụng của tôi trong một màu đỏ nhưng tôi cần phải làm cho nó loại trong suốt để các thành phần theo nó sẽ vẫn hiển thị. Tuy nhiên tôi vẫn muốn rằng một số màu sắc vẫn sẽ hiển thị. Phương pháp tôi vẽ là:Làm cách nào để tạo hình chữ nhật trong Đồ họa có màu trong suốt?

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

Có ai biết cách làm cho màu đỏ trong suốt không? Tôi không cần nó hoàn toàn minh bạch mặc dù.

Trả lời

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

Xem Color constructors mà mất 4 đối số (int hay float) để biết thêm chi tiết.

1

Hãy thử điều này:

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

    } 
} 
Các vấn đề liên quan