package com.sky.java.chat; 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; /** * 绘制带阴影的饼图 * * @author Biao */ @SuppressWarnings("serial") public class Pie 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 Pie() { 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)); 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(""); frame.getContentPane().add(new Pie()); 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); } public static void main(String[] args) { createGuiAndShow(); } }