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();
}
}
分享到:
相关推荐
float piefloat pie float pie float pie float pie
PIE 算法按照自己的理解写的 但是还有些问题,希望有相同感兴趣的给点建议
CMU Multi-PIE人脸数据库包含超过750,000张337人的图像,这些图像在五个月内最多可记录四次。受试者在15个视点和19个照明条件下成像,同时显示一系列面部表情。此外,还获得了高分辨率正面图像。总的来说,...
pie数据集matlab版本,fea特征,gnd标签。
pie.js以及pie.htc支持在ie8浏览器支持css3的border-redius属性
PIE-UAV用户使用手册V2.6(20151202),仅供学习和研究,请勿商用
pie_3.0总线规范,关于pie和pie总线的介绍可供大家学习
PIE FACE 数据库用于计算机视觉中的人脸识别,多姿态,多角度,有不同光照
一个基于PIE平台自己编写的RVI指数代码,作为一个事例,通过给大家分享讨论中国自己的PIE大数据平台的应用
内含CMU_PIE人脸数据集,来自68个人的40000张照片,其中包括了每个人的13种姿态条件,43种光照条件和4种表情下的照片。共有5种pose,用于训练测试,亲测不错
PIE工程师考试试题(答案).pdf
multipie数据集.txt,百度网盘下载
multiPIE 人脸数据库(1515张)大小为240*280
PIE-Engine Studio水体指数计算实例操作.pdf
PIE-SAR用户手册
CMU的人脸库,源文件是若干个.mat文件,我将.mat文件中的图像文件都提取了出来,由于CSDN的大小限制,便没有将.mat文件放在这里,这里都是.jpg文件
PIE-Engine Studio植被覆盖度计算实例操作.pdf
源码包括PIE Engine的数据下载器完整代码,支持不同数据源的下载,也支持不同区域分辨率影像的导出。
PIE-Engine快速入门.pdf