2

我正在通过 JPanel 在 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);
        }
    }
}

然而我希望这条线是半透明的。我该如何做到这一点?

4

1 回答 1

7

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

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

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

有关示例的详细答案,请参阅本文

于 2012-11-17T16:39:41.200 回答