【问题标题】:How to draw a transparent line?如何画一条透明线?
【发布时间】:2012-11-17 16:35:53
【问题描述】:

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

但我希望这条线是半透明的。我该如何实现?

【问题讨论】:

  • 完整的透明线,去掉方法即可;-)。

标签: java jpanel line transparency graphics2d


【解决方案1】:

简短的回答是为图形上下文的颜色设置alpha:

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

Alpha 范围在 0.0f(不可见)到 1.0f(不透明)之间

对于带有示例的长答案,see this article。

【讨论】:

    猜你喜欢
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 2023-03-06
    • 2020-03-25
    • 2013-01-02
    • 2015-04-16
    相关资源
    最近更新 更多