java 绘图中如何实现 带有宽度可截断的圆环

发布于 2022-09-05 21:06:32 字数 1974 浏览 15 评论 0

我需要制作一个圆环如:

clipboard.png

简单来说就是一段圆弧,但是这个圆弧有一定的宽度,不能用描边来做
我使用的是java的Graphics,但是遍寻API也没有找到合适的方式实现,最理想的结果是可以通过path来绘制之后填充,java中找到了这个类似于svg的path的类GeneralPath,但是这个类的功能有限,只有二次、三次贝赛尔曲线,竟然没有画圆弧的,不知道大家都用什么方式做这种图呢
ps:
1、只能用java原生的
2、这种需要在项目的绘图中广泛需要,最理想的情况是使用path
求指导
根据二楼的代码实践后效果:

clipboard.png
描边之后多段圆弧接口对不上,究其原因是由于描边把两头也加了100像素进行绘制了,所以圆弧两头才出现偏离轨道

    private void plotCircleSpan(Graphics g,Point point,float r1,float r2,float start,float end,Color color){
        Graphics2D g2d = (Graphics2D) g;
        Color tmpColor=g2d.getColor();
        g2d.setColor(color);  // 设置画笔颜色
        Stroke sk=g2d.getStroke();
        g2d.setStroke(new BasicStroke(100)); // 设置线条宽度
        // 抗边缘锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON);
        /**
         * drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
         * 画椭圆一部分的圆弧线,椭圆的中心是它的外接矩形的中心,其中参数 x 和 y 是外接矩形的左上角
         * 坐标(x,y),外接矩形宽是 width,高是 height。参数 startAngle 的单位是度,起始角度 0 度
         * 是指 3 点钟方位。参数 startAngle 和 arcAngle 表示从 startAngle 角度开始, 
         * 逆时针方向画 arcAngle 度的弧(正值度数是逆时针方向,负值度数是顺时针方向)
         */
        g2d.drawArc((int)(point.getX1()-(r1+r2)/2), (int)(point.getY1()-(r1+r2)/2), (int)((r1+r2)), (int)((r1+r2)), (int)start, (int)end);
        g2d.setStroke(sk); // 设置线条宽度
        g2d.setColor(tmpColor);
    }

将g2d.setStroke(new BasicStroke(100));修改为g2d.setStroke(new BasicStroke(100,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER));

clipboard.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤芳又自赏 2022-09-12 21:06:33

Graphics 有一个子类,叫 Graphics2D,拥有比 Graphics 更多的对于画笔的设置,比如设置线条宽度和设置边缘抗锯齿等 —— 所以,你在需要这些特性的时候,可以将 Graphics 转为 Graphics2D —— 对于 Swing 中的 Graphics 实例,其实就是 Graphics2D,所以可以直接进行强制类型转换。

import java.awt.*;
import javax.swing.*;

public class DrawingFrame extends JFrame {

    public DrawingFrame() {
        this.add(new DrawingPanel());
        
        this.setSize(400, 500);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    class DrawingPanel extends JPanel {

        @Override
        public void paintComponent(Graphics graphics) {

            Graphics2D g2d = (Graphics2D) graphics;
            
            g2d.setColor(Color.RED);  // 设置画笔颜色
            g2d.setStroke(new BasicStroke(10F)); // 设置线条宽度

            // 抗边缘锯齿
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                 RenderingHints.VALUE_ANTIALIAS_ON);
            /**
             * drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
             * 画椭圆一部分的圆弧线,椭圆的中心是它的外接矩形的中心,其中参数 x 和 y 是外接矩形的左上角
             * 坐标(x,y),外接矩形宽是 width,高是 height。参数 startAngle 的单位是度,起始角度 0 度
             * 是指 3 点钟方位。参数 startAngle 和 arcAngle 表示从 startAngle 角度开始, 
             * 逆时针方向画 arcAngle 度的弧(正值度数是逆时针方向,负值度数是顺时针方向)
             */
            g2d.drawArc(100, 100, 200, 200, 45, 135);
        }

    }

    public static void main(String[] args) {
        DrawingFrame frame = new DrawingFrame();
        frame.setVisible(true);
    }

}

运行结果:
运行结果

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文