java 绘制 3D饼图

package com.edu.cec;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.RenderingHints;

import java.awt.Toolkit;

import java.awt.geom.Arc2D;

import java.awt.geom.Area;

import java.awt.geom.GeneralPath;

import java.awt.geom.Point2D;

import java.util.ArrayList;

import java.util.List;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class Pie3D extends JPanel {

 // 饼图中每一个部分的百分比

 private double[] percents = { 10.72, 15.38, 3.74, 10.26, 6.56, 5.69, 10.72,
   15.38, 6.15, 18.0 };

 // 饼图的颜色

 private Color[] pieColors;

 public Pie3D() {

  List<Color> colors = new ArrayList<Color>();

  colors.add(getColorFromHex("#169800"));

  colors.add(getColorFromHex("#00E500"));

  colors.add(getColorFromHex("#D0F15A"));

  colors.add(getColorFromHex("#FF7321"));

  colors.add(getColorFromHex("#E2FF55"));

  colors.add(getColorFromHex("#AA6A2D"));

  colors.add(getColorFromHex("#BFDD89"));

  colors.add(getColorFromHex("#D718A5"));

  colors.add(getColorFromHex("#00DBFF"));

  colors.add(getColorFromHex("#00FF00"));

  pieColors = colors.toArray(new Color[0]);

 }

 public static Color getColorFromHex(String hex) {

  try {

   return new Color(Integer.valueOf(hex.substring(1), 16));

  } catch (Exception e) {

   return Color.BLACK;

  }

 }

 @Override
 protected void paintComponent(Graphics g) {

  super.paintComponent(g);

  Graphics2D g2d = (Graphics2D) g;

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

  int w = 380;

  int h = (int) (w * 0.618);

  fillPies(g2d, percents, 30, 30, w, h);

 }

 private void fillPies(Graphics2D g2d, double[] percents, int x, int y,
   int w, int h) {

  int colorIndex = 0;

  double startAngle = 0;

  double arcAngle = 0;

  // 一次性绘制完阴影,然后再绘制饼图的效果比绘制饼图的同时绘制它的阴影好

  for (int i = 0; i < percents.length; ++i) {

   arcAngle = percents[i] * 3.6;

   if (i + 1 == percents.length) {

    arcAngle = 360 - startAngle;

   }

   Arc2D.Double arcTop = new Arc2D.Double(x, y, w, h, startAngle,
     arcAngle, Arc2D.PIE);

   g2d.setColor(pieColors[colorIndex].darker());

   g2d.fill(buildShadow(arcTop));

   // g2d.setColor(pieColors[colorIndex]);

   // g2d.fill(arcTop);

   colorIndex = (colorIndex + 1) % pieColors.length;

   startAngle += arcAngle;

  }

  colorIndex = 0;

  startAngle = 0;

  for (int i = 0; i < percents.length; ++i) {

   arcAngle = percents[i] * 3.6;

   if (i + 1 == percents.length) {

    arcAngle = 360 - startAngle;

   }

   Arc2D.Double arcTop = new Arc2D.Double(x, y, w, h, startAngle,
     arcAngle, Arc2D.PIE);

   g2d.setColor(pieColors[colorIndex]);

   g2d.fill(arcTop);

   colorIndex = (colorIndex + 1) % pieColors.length;

   startAngle += arcAngle;

  }

 }

 private Area buildShadow(Arc2D arc) {

  int shadowDapth = 10;

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

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

  Point2D topLeft = new Point2D.Double(arc.getStartPoint().getX(), arc
    .getStartPoint().getY());

  Point2D topRight = new Point2D.Double(arc.getEndPoint().getX(), arc
    .getEndPoint().getY());

  Point2D bottomLeft = new Point2D.Double(arcBottom.getStartPoint()
    .getX(), arcBottom

  .getStartPoint().getY());

  Point2D bottomRight = new Point2D.Double(
    arcBottom.getEndPoint().getX(), arcBottom

    .getEndPoint().getY());

  double[] xpoints = { topLeft.getX(), topRight.getX(),
    bottomRight.getX(), bottomLeft.getX() };

  double[] ypoints = { topLeft.getY(), topRight.getY(),
    bottomRight.getY(), bottomLeft.getY() };

  GeneralPath path = new GeneralPath();

  path.moveTo(xpoints[0], ypoints[0]);

  path.lineTo(xpoints[1], ypoints[1]);

  path.lineTo(xpoints[2], ypoints[2]);

  path.lineTo(xpoints[3], ypoints[3]);

  path.closePath();

  Area area = new Area(arcBottom);

  area.add(new Area(path));

  return area;

 }

 private static void createGuiAndShow() {

  JFrame frame = new JFrame("Pie with Shadow");

  frame.getContentPane().add(new Pie3D());

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  int sw = Toolkit.getDefaultToolkit().getScreenSize().width;

  int sh = Toolkit.getDefaultToolkit().getScreenSize().height;

  int w = 500;

  int h = 400;

  int x = (sw - w) / 2;

  int y = (sh - h) / 2 - 40;

  x = x > 0 ? x : 0;

  y = y > 0 ? y : 0;

  frame.setBounds(x, y, w, h);

  frame.setVisible(true);

 }

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值