如何使用3色渐变渲染圆弧?

时间:2017-09-20 21:53:40

标签: java swing gradient paintcomponent

我尝试渲染一个以绿色开始,过渡到黄色,最后变为红色的弧。现在它使用标准的GradientPaint方法从绿色过渡到红色。有没有办法用3种颜色做到这一点?

public void paintComponent(Graphics gr) {
    super.paintComponent(gr);
    Graphics2D g = (Graphics2D) gr;

    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    BasicStroke wideStroke = new BasicStroke(24.0f);
    g.setStroke(wideStroke);

    g.setPaint(new GradientPaint(new Point(10, 140), Color.GREEN, new Point(100, 10), Color.RED));  

    g.draw(new Arc2D.Double(25, 25, 200, 200, 180, -90, Arc2D.OPEN));   
}

1 个答案:

答案 0 :(得分:1)

感谢MadProgrammer

    public void paintComponent(Graphics gr) {
    super.paintComponent(gr);
    Graphics2D g = (Graphics2D) gr;

    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    BasicStroke wideStroke = new BasicStroke(24.0f);
    g.setStroke(wideStroke);

    LinearGradientPaint lgp = new LinearGradientPaint(
            new Point2D.Float(0, 160),
            new Point2D.Float(160, 0),
            new float[] {0f, 0.5f, 1f},
            new Color[] {Color.green, Color.yellow, Color.red}
            );

    g.setPaint(lgp);

    g.draw(new Arc2D.Double(25, 25, 200, 200, 180, -90, Arc2D.OPEN));
}