2012-11-17 65 views
2

我畫上一個JPanel藍色實線過如何繪製透明線?

public void paint(Graphics g) { 
    Graphics2D g2 = (Graphics2D) g; 

    super.paint(g2); 

    if (path.size() >= 2) { 
     BasisStroke stroke = new BasicStroke(Config.TILE_SIZE_IN_PIXEL/3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL); 
     g2.setStroke(stroke); 
     g2.setPaint(Color.BLUE); 
     g2.setPaintMode(); 

     for (int i = 0; i < path.size() - 1; i++) { 
      g2.drawLine(path.get(i).x, path.get(i).y, path.get(i + 1).x, path.get(i + 1).y); 
     } 
    } 
} 

然而,我想這條線是半透明的。我如何實現這一目標?

+3

對於完整的透明線,只需刪除方法;-)。 – Anthony

回答

7

簡短的回答是設置alpha爲圖形上下文的顏色:

float alpha = 0.5; 
Color color = new Color(1, 0, 0, alpha); //Red 
g2.setPaint(color); 

阿爾法之間0.0f(不可見)範圍爲1.0f(不透明)

對於長的答案與實例中, see this article

+1

+1有一個相關示例[此處](http://stackoverflow.com/a/7824225/230513)。 – trashgod