Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 441|回复: 0

[Swing学习]Swing:绘制渐变的饼图

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-11-15 23:56:59 | 显示全部楼层 |阅读模式


    1. import java.awt.Color;
    2. import java.awt.geom.Arc2D;
    3. import java.awt.geom.Area;
    4. import java.awt.geom.GeneralPath;
    5. import java.awt.geom.Point2D;
    6. class Pie3D {
    7.     private Arc2D arc; // 这里的弧并不是圆上的一弧,而是椭圆的一部分.
    8.     private Area frontSite;
    9.     private Area leftSite;
    10.     private Area rightSite;
    11.     private Color color;
    12.     private Pie3D selectedPie;
    13.     private Point2D arcMiddle;
    14.     private Point2D labelPosition;
    15.     private double value;
    16.     private int shadowDepth;
    17.     private double selectedShiftDis; // 被选中的饼图在他的中线上移动的距离
    18.     public Pie3D(Arc2D arc, Color color, double value) {
    19.         this(arc, color, value, 10, 30);
    20.     }
    21.    public Pie3D(Arc2D arc, Color color, double value, int shadowDepth, double selectedShiftDis) {
    22.         this.arc = arc;
    23.         this.color = color;
    24.         this.value = value;
    25.         this.selectedShiftDis = selectedShiftDis;
    26.         this.shadowDepth = shadowDepth;
    27.        Arc2D arcBottom = new Arc2D.Double(arc.getX(), arc.getY() + shadowDepth, arc.getWidth(),
    28.             arc.getHeight() + 0, arc.getAngleStart(), arc.getAngleExtent(), Arc2D.CHORD);
    29.         Point2D[] topPs = getPointsOfArc(arc);
    30.         Point2D[] bottomPs = getPointsOfArc(arcBottom);
    31.         // Front site
    32.         GeneralPath font = new GeneralPath();
    33.         font.moveTo(topPs[1].getX(), topPs[1].getY());
    34.         font.lineTo(topPs[2].getX(), topPs[2].getY());
    35.         font.lineTo(bottomPs[2].getX(), bottomPs[2].getY());
    36.         font.lineTo(bottomPs[1].getX(), bottomPs[1].getY());
    37.         font.closePath();
    38.         this.frontSite = new Area(arcBottom);
    39.         this.frontSite.add(new Area(font));
    40.        // Left site
    41.         GeneralPath left = new GeneralPath();
    42.         left.moveTo(topPs[0].getX(), topPs[0].getY());
    43.         left.lineTo(topPs[1].getX(), topPs[1].getY());
    44.         left.lineTo(bottomPs[1].getX(), bottomPs[1].getY());
    45.         left.lineTo(topPs[0].getX(), topPs[0].getY() + 3);
    46.         left.closePath();
    47.         this.leftSite = new Area(left);
    48.        // Right site
    49.         GeneralPath right = new GeneralPath();
    50.         right.moveTo(topPs[0].getX(), topPs[0].getY());
    51.         right.lineTo(topPs[2].getX(), topPs[2].getY());
    52.         right.lineTo(bottomPs[2].getX(), bottomPs[2].getY());
    53.         right.lineTo(topPs[0].getX(), topPs[0].getY() + 3);
    54.         right.closePath();
    55.         this.rightSite = new Area(right);
    56.         arcMiddle = calculateArcMiddle();
    57.        // Label position: 五分之四处
    58.         Point2D c = getPieCenter();
    59.         // Point2D m = getChordMiddle();
    60.         Point2D m = arcMiddle;
    61.         double dis = GeometryUtil.distanceOfPoints(c, m) * 0.8;
    62.         labelPosition = GeometryUtil.extentPoint(c, m, dis);
    63.     }
    64.     // 取得Arc上的三个点,在对Arc: center, left, right.
    65.     public static Point2D[] getPointsOfArc(Arc2D arc) {
    66.         Point2D center = new Point2D.Double(arc.getCenterX(), arc.getCenterY());
    67.         Point2D left = arc.getStartPoint();
    68.         Point2D right = arc.getEndPoint();
    69.         Point2D[] points = new Point2D[] { center, left, right };
    70.        return points;
    71.     }
    72.    public Pie3D getSelectedPie() {
    73.         if (selectedPie == null) {
    74.            selectedPie = createSeletecdPie();
    75.         }
    76.        return selectedPie;
    77.     }
    78.    private Pie3D createSeletecdPie() {
    79.         // 沿中线方向移动selectedShiftDis个单位
    80.         Point2D c = getPieCenter();
    81.         Point2D m = getChordMiddle();
    82.         Point2D p = GeometryUtil.extentPoint(c, m, selectedShiftDis);
    83.        double deltaX = p.getX() - c.getX();
    84.         double deltaY = p.getY() - c.getY();
    85.         double x = arc.getX() + deltaX;
    86.         double y = arc.getY() + deltaY;
    87.        Arc2D shiftArc = (Arc2D) arc.clone();
    88.         shiftArc.setFrame(x, y, arc.getWidth(), arc.getHeight());
    89.        return new Pie3D(shiftArc, color, value, shadowDepth, selectedShiftDis);
    90.     }
    91.    public Arc2D getArc() {
    92.         return arc;
    93.     }
    94.    public Area getFrontSite() {
    95.         return frontSite;
    96.     }
    97.    public Area getLeftSite() {
    98.         return leftSite;
    99.     }
    100.    public Area getRightSite() {
    101.         return rightSite;
    102.     }
    103.    public Color getColor() {
    104.         return color;
    105.     }
    106.     public void setColor(Color color) {
    107.         this.color = color;
    108.    }
    109.     public Point2D getLabelPosition() {
    110.         return labelPosition;
    111.   }
    112.     public void setLabelPosition(Point2D labelPosition) {
    113.        this.labelPosition = labelPosition;
    114.     }
    115.    public double getValue() {
    116.         return value;
    117.     }
    118.    public String getLabel() {
    119.         return value + "%";
    120.     }
    121.    // 弦的中心点
    122.     public Point2D getChordMiddle() {
    123.         return GeometryUtil.middlePoint(arc.getStartPoint(), arc.getEndPoint());
    124.     }
    125.    // 饼图的圆心
    126.     public Point2D getPieCenter() {
    127.         return new Point2D.Double(arc.getCenterX(), arc.getCenterY());
    128.     }
    129.    // 弧上的中心点
    130.     public Point2D getArcMiddle() {
    131.         return arcMiddle;
    132.     }
    133.    private Point2D calculateArcMiddle() {
    134.         // 创建一个新的弧,其扩展角度为当前弧的一半
    135.         return new Arc2D.Double(arc.getX(), arc.getY(), arc.getWidth(), arc.getHeight(),
    136.             arc.getAngleStart(), arc.getAngleExtent() / 2, Arc2D.PIE).getEndPoint();
    137.     }
    138. }
    复制代码
    其它代码请下载。

       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/11/15/235658640.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-25 07:37 , Processed in 0.338830 second(s), 35 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表