首页>代码>java swing绘制饼图>/饼图/ff/Z2.java
package com.cs.ff;



import java.awt.Color;

import java.awt.geom.Arc2D;

import java.awt.geom.Area;

import java.awt.geom.GeneralPath;

import java.awt.geom.Point2D;


public class Z2 {

    private Arc2D arc; // 这里的弧并不是圆上的一弧,而是椭圆的一部分.

    private Area frontSite;

    private Area leftSite;

    private Area rightSite;

    private Color color;

    private Z2 selectedPie;


    private Point2D arcMiddle;

    private Point2D labelPosition;


    private double value;

    private int shadowDepth;

    private double selectedShiftDis; // 被选中的饼图在他的中线上移动的距离


    public Z2(Arc2D arc, Color color, double value) {

        this(arc, color, value, 10, 30);

    }


    public Z2(Arc2D arc, Color color, double value, int shadowDepth, double selectedShiftDis) {

        this.arc = arc;

        this.color = color;

        this.value = value;

        this.selectedShiftDis = selectedShiftDis;

        this.shadowDepth = shadowDepth;


        Arc2D arcBottom = new Arc2D.Double(arc.getX(), arc.getY() + shadowDepth, arc.getWidth(),

            arc.getHeight() + 0, arc.getAngleStart(), arc.getAngleExtent(), Arc2D.CHORD);

        Point2D[] topPs = getPointsOfArc(arc);

        Point2D[] bottomPs = getPointsOfArc(arcBottom);


        // Front site

        GeneralPath font = new GeneralPath();

        font.moveTo(topPs[1].getX(), topPs[1].getY());

        font.lineTo(topPs[2].getX(), topPs[2].getY());

        font.lineTo(bottomPs[2].getX(), bottomPs[2].getY());

        font.lineTo(bottomPs[1].getX(), bottomPs[1].getY());

        font.closePath();

        this.frontSite = new Area(arcBottom);

        this.frontSite.add(new Area(font));


        // Left site

        GeneralPath left = new GeneralPath();

        left.moveTo(topPs[0].getX(), topPs[0].getY());

        left.lineTo(topPs[1].getX(), topPs[1].getY());

        left.lineTo(bottomPs[1].getX(), bottomPs[1].getY());

        left.lineTo(topPs[0].getX(), topPs[0].getY() + 3);

        left.closePath();

        this.leftSite = new Area(left);


        // Right site

        GeneralPath right = new GeneralPath();

        right.moveTo(topPs[0].getX(), topPs[0].getY());

        right.lineTo(topPs[2].getX(), topPs[2].getY());

        right.lineTo(bottomPs[2].getX(), bottomPs[2].getY());

        right.lineTo(topPs[0].getX(), topPs[0].getY() + 3);

        right.closePath();

        this.rightSite = new Area(right);


        arcMiddle = calculateArcMiddle();


        // Label position: 五分之四处

        Point2D c = getPieCenter();

        // Point2D m = getChordMiddle();

        Point2D m = arcMiddle;

        double dis = Z1.distanceOfPoints(c, m) * 0.8;

        labelPosition = Z1.extentPoint(c, m, dis);

    }


    // 取得Arc上的三个点,在对Arc: center, left, right.

    public static Point2D[] getPointsOfArc(Arc2D arc) {

        Point2D center = new Point2D.Double(arc.getCenterX(), arc.getCenterY());

        Point2D left = arc.getStartPoint();

        Point2D right = arc.getEndPoint();

        Point2D[] points = new Point2D[] { center, left, right };


        return points;

    }


    public Z2 getSelectedPie() {

        if (selectedPie == null) {

            selectedPie = createSeletecdPie();

        }


        return selectedPie;

    }


    private Z2 createSeletecdPie() {

        // 沿中线方向移动selectedShiftDis个单位

        Point2D c = getPieCenter();

        Point2D m = getChordMiddle();

        Point2D p = Z1.extentPoint(c, m, selectedShiftDis);


        double deltaX = p.getX() - c.getX();

        double deltaY = p.getY() - c.getY();

        double x = arc.getX() + deltaX;

        double y = arc.getY() + deltaY;


        Arc2D shiftArc = (Arc2D) arc.clone();

        shiftArc.setFrame(x, y, arc.getWidth(), arc.getHeight());


        return new Z2(shiftArc, color, value, shadowDepth, selectedShiftDis);

    }


    public Arc2D getArc() {

        return arc;

    }


    public Area getFrontSite() {

        return frontSite;

    }


    public Area getLeftSite() {

        return leftSite;

    }


    public Area getRightSite() {

        return rightSite;

    }


    public Color getColor() {

        return color;

    }


    public void setColor(Color color) {

        this.color = color;

    }


    public Point2D getLabelPosition() {

        return labelPosition;

    }


    public void setLabelPosition(Point2D labelPosition) {

        this.labelPosition = labelPosition;

    }


    public double getValue() {

        return value;

    }


    public String getLabel() {

        return value + "%";

    }


    // 弦的中心点

    public Point2D getChordMiddle() {

        return Z1.middlePoint(arc.getStartPoint(), arc.getEndPoint());

    }


    // 饼图的圆心

    public Point2D getPieCenter() {

        return new Point2D.Double(arc.getCenterX(), arc.getCenterY());

    }


    // 弧上的中心点

    public Point2D getArcMiddle() {

        return arcMiddle;

    }


    private Point2D calculateArcMiddle() {

        // 创建一个新的弧,其扩展角度为当前弧的一半

        return new Arc2D.Double(arc.getX(), arc.getY(), arc.getWidth(), arc.getHeight(),

            arc.getAngleStart(), arc.getAngleExtent() / 2, Arc2D.PIE).getEndPoint();

    }

}


最近下载更多
19933647920  LV1 2021年10月28日
风过无痕  LV1 2021年7月4日
Ls哈哈哈  LV1 2021年4月14日
lizhenlinok  LV10 2021年1月11日
city_77  LV1 2020年10月28日
chenrui7777  LV1 2020年7月25日
是一个鸽子啊  LV17 2020年7月21日
1872615558  LV13 2020年7月21日
xiaoyanzi  LV1 2020年7月10日
qiu111  LV1 2020年1月3日
最近浏览更多
jidonglin 2021年12月18日
暂无贡献等级
甘威66666 2021年12月16日
暂无贡献等级
华灯初上旧人安  LV8 2021年12月1日
elskan 2021年11月11日
暂无贡献等级
19933647920  LV1 2021年10月28日
ming_123_9715  LV23 2021年7月7日
风过无痕  LV1 2021年7月4日
zzzzzzrf  LV2 2021年5月20日
Ls哈哈哈  LV1 2021年4月14日
lizhenlinok  LV10 2021年1月11日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友