package my;
import java.awt.geom.Point2D;
public class P1 {
public static double distanceOfPoints(Point2D p1, Point2D p2) {
double disX = p2.getX() - p1.getX();
double disY = p2.getY() - p1.getY();
double dis = Math.sqrt(disX * disX + disY * disY);
return dis;
}
public static Point2D middlePoint(Point2D p1, Point2D p2) {
double x = (p1.getX() + p2.getX()) / 2;
double y = (p1.getY() + p2.getY()) / 2;
return new Point2D.Double(x, y);
}
public static Point2D extentPoint(Point2D startPoint, Point2D endPoint, double disToStartPoint) {
double disX = endPoint.getX() - startPoint.getX();
double disY = endPoint.getY() - startPoint.getY();
double dis = Math.sqrt(disX * disX + disY * disY);
double sin = (endPoint.getY() - startPoint.getY()) / dis;
double cos = (endPoint.getX() - startPoint.getX()) / dis;
double deltaX = disToStartPoint * cos;
double deltaY = disToStartPoint * sin;
return new Point2D.Double(startPoint.getX() + deltaX, startPoint.getY() + deltaY);
}
}
package my;
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 P2 {
private Arc2D arc;
private Area frontSite;
private Area leftSite;
private Area rightSite;
private Color color;
private P2 selectedPie;
private Point2D arcMiddle;
private Point2D labelPosition;
private double value;
private int shadowDepth;
private double selectedShiftDis;
public P2(Arc2D arc, Color color, double value) {
this(arc, color, value, 10, 30);
}
public P2(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);
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));
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);
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();
Point2D c = getPieCenter();
Point2D m = arcMiddle;
double dis = P1.distanceOfPoints(c, m) * 0.8;
labelPosition = P1.extentPoint(c, m, dis);
}
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 P2 getSelectedPie() {
if (selectedPie == null) {
selectedPie = createSeletecdPie();
}
return selectedPie;
}
private P2 createSeletecdPie() {
Point2D c = getPieCenter();
Point2D m = getChordMiddle();
Point2D p = P1.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 P2(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 P1.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();
}
}
package my;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Arc2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class P3 extends JPanel {
private double[] data;
private Color[] defaultColors;
private P2[] pies;
private int shadowDepth = 8;
private int shiftAngle = -30;
private int selectedPieIndex = -1;
public P3() {
data = new double[] { 20.72, 6.56, 3.74, 10.26, 15.38, 5.69, 10.72, 15.38, 6.15, 18.0 };
defaultColors = createColors();
int x = 50;
int y = 50;
int w = 380;
int h = (int) (w * 0.618);
pies = createPies(x, y, w, h, shadowDepth, shiftAngle, data, defaultColors);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
selectedPieIndex = -1;
for (int i = 0; i < pies.length; ++i) {
if (pies[i].getArc().contains(e.getX(), e.getY())) {
selectedPieIndex = i;
break;
}
}
repaint();
}
});
}
private Color[] createColors() {
List<Color> colors = new ArrayList<Color>();
colors.add(Color.decode("#FF7321"));
colors.add(Color.decode("#169800"));
colors.add(Color.decode("#00E500"));
colors.add(Color.decode("#D0F15A"));
colors.add(Color.decode("#AA6A2D"));
colors.add(Color.decode("#BFDD89"));
colors.add(Color.decode("#E2FF55"));
colors.add(Color.decode("#D718A5"));
colors.add(Color.decode("#00DBFF"));
colors.add(Color.decode("#00FF00"));
return colors.toArray(new Color[0]);
}
public static P2[] createPies(int x,
int y,
int w,
int h,
int shadowDepth,
double shiftAngle,
double[] data,
Color[] colors) {
double sum = 0;
for (double d : data) {
sum += d;
}
double arcAngle = 0;
double startAngle = shiftAngle;
P2[] pies = new P2[data.length];
for (int i = 0; i < data.length; ++i) {
arcAngle = data[i] * 360 / sum;
if (i + 1 == data.length) {
arcAngle = 360 + shiftAngle - startAngle;
arcAngle = arcAngle > 0 ? arcAngle : 0;
}
Arc2D.Double arc = new Arc2D.Double(x, y, w, h, startAngle, arcAngle, Arc2D.PIE);
pies[i] = new P2(arc, colors[i % colors.length], data[i], shadowDepth, 30);
startAngle += arcAngle;
}
return pies;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (pies != null) {
drawPies(g2d, pies, selectedPieIndex);
}
}
private void drawPies(Graphics2D g2d, P2[] pies, int selectedIndex) {
int startIndex = 0;
int endIndex = pies.length;
boolean closed = (endIndex - startIndex == pies.length) ? true : false;
boolean selected = (selectedIndex >= startIndex && selectedIndex < endIndex) ? true : false;
FontMetrics metrics = g2d.getFontMetrics();
for (int i = startIndex; i < endIndex; ++i) {
if (i != selectedIndex) {
P2 p = pies[i];
g2d.setColor(p.getColor().darker());
g2d.fill(p.getFrontSite());
}
}
if (!closed && selectedIndex != startIndex) {
g2d.setColor(pies[startIndex].getColor().darker());
g2d.fill(pies[startIndex].getLeftSite());
}
if (!closed && selectedIndex + 1 != endIndex) {
g2d.setColor(pies[endIndex - 1].getColor().darker());
g2d.fill(pies[endIndex - 1].getRightSite());
}
if (selected) {
int prevIndex = selectedIndex > startIndex ? (selectedIndex - 1) : endIndex - 1;
int nextIndex = (selectedIndex + 1) >= endIndex ? startIndex : (selectedIndex + 1);
g2d.setColor(pies[prevIndex].getColor().darker());
g2d.fill(pies[prevIndex].getRightSite());
g2d.setColor(pies[nextIndex].getColor().darker());
g2d.fill(pies[nextIndex].getLeftSite());
}
for (int i = startIndex; i < endIndex; ++i) {
if (i != selectedIndex) {
P2 p = pies[i];
g2d.setColor(p.getColor());
g2d.fill(p.getArc());
int sw = metrics.stringWidth(p.getLabel()) / 2;
int sh = (metrics.getAscent()) / 2;
int x = (int) (p.getLabelPosition().getX() - sw);
int y = (int) (p.getLabelPosition().getY() + sh);
g2d.setColor(Color.BLACK);
g2d.drawString(p.getLabel(), x, y);
}
}
if (selected) {
P2 p = pies[selectedIndex].getSelectedPie();
g2d.setColor(p.getColor().darker());
g2d.fill(p.getFrontSite());
g2d.fill(p.getLeftSite());
g2d.fill(p.getRightSite());
g2d.setColor(p.getColor());
g2d.fill(p.getArc());
int sw = metrics.stringWidth(p.getLabel()) / 2;
int sh = (metrics.getAscent()) / 2;
int x = (int) (p.getLabelPosition().getX() - sw);
int y = (int) (p.getLabelPosition().getY() + sh);
g2d.setColor(Color.BLACK);
g2d.drawString(p.getLabel(), x, y);
}
}
private static void createGuiAndShow() {
JFrame frame = new JFrame("Pie with 3D Effect");
frame.getContentPane().add(new P3());
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();
}
}