
public void drawPie(Graphics2D g, PieValue[] slices){
// Góc trên, trái và chi u dài, r ng c a hình ch nh tề ộ ủ ữ ậ
int top = 100, left = 100, width = 200, height = 200;
// total: t ng đ l n c a các cungổ ộ ớ ủ
double total = 0.0D;
// Xác đ nh total thông qua duy t qua các cung trong m ngị ệ ả
for(int j=0; j<slices.length; j++) {
total += slices[j].value;
}
// curValue: giá tr c a cung hi n t iị ủ ệ ạ
double curValue = 0.0D;
// startAngle: góc b t đ u c a cung hi n t iắ ầ ủ ệ ạ
int startAngle = 0;
// Duy t qua các cung trong m ng và v t ng cung m tệ ả ẽ ừ ộ
for(int j=0; j<slices.length; j++) {
// Tìm góc b t đ uắ ầ
startAngle = (int)(curValue*360/total);
// Tìm góc k t thúcế
int sweepAngle = (int)(slices[j].value*360/total);
// Đ m b o cho cung cu i cùng luôn khít v i cung đ u tiênả ả ố ớ ầ
if(j == slices.length-1) {
sweepAngle = 360 - startAngle;
}
// Thi t l p màu v t ng cungế ậ ẽ ừ
g.setColor(slices[j].color);
// V hình ch nh t bao quanh bi u đ (cho d nhìn)ẽ ữ ậ ể ồ ễ
// g.drawRect(top, left, width, height);
// V cung tròn (ti p giáp v i hình ch nh t có góc trênẽ ế ớ ữ ậ
// là 100,100; chi u r ng là 200, chi u cao là 200ề ộ ề
g.fillArc(top, left, width, height, startAngle, sweepAngle);
// Góc đ v nhãnể ẽ
int textAngle = (int)((startAngle+sweepAngle)*3.14/180);
// r: bán kính c a m t cung trònủ ộ
int r = width/2;
// ox, oy: g c t a đ m iố ọ ộ ớ
int ox = left + (width/2);

int oy = top + (height/2);
// x,y: t a đ v nhãnọ ộ ẽ
int x = (int)(r*Math.cos(textAngle));
int y = (int)(r*Math.sin(textAngle));
g.setColor(Color.black);
// V nhãnẽ
g.drawString(String.valueOf(slices[j].value), ox + x, oy - y);
// Tăng giá tr hi n t i đ v cung ti p theoị ệ ạ ể ẽ ế
curValue += slices[j].value;
}
}
package chart;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*+---------------+
| Lop PieValue |
+---------------+*/
class PieValue {
double value;
Color color;
public PieValue(double value, Color color) {
this.value = value;
this.color = color;
}
}
/*+---------------+
| Lop Pie |
+---------------+*/
public class Pie extends JFrame {
PieValue[] slices = new PieValue[4];

/*+---------------+
| Constructor |
+---------------+*/
Pie() {
// Cac gia tri thu nghiem
slices[0] = new PieValue(25, Color.red);
slices[1] = new PieValue(33, Color.green);
slices[2] = new PieValue(20, Color.pink);
slices[3] = new PieValue(15, Color.blue);
setSize(400,400);
setVisible(true);
}
/*+---------------+
| Ham ve |
+---------------+*/
public void paint(Graphics g) {
drawPie((Graphics2D)g, slices);
}
/*+-----------------+
| Ham ve do thi |
+-----------------+*/
public void drawPie(Graphics2D g, PieValue[] slices){
int top = 100, left = 100, width = 200, height = 200;
double total = 0.0D;
for(int j=0; j<slices.length; j++) {
total += slices[j].value;
}
double curValue = 0.0D;
int startAngle = 0;
for(int j=0; j<slices.length; j++) {
startAngle = (int)(curValue*360/total);
int sweepAngle = (int)(slices[j].value*360/total);

if(j == slices.length-1) {
sweepAngle = 360 - startAngle;
}
g.setColor(slices[j].color);
g.fillArc(top, left, width, height, startAngle, sweepAngle);
int textAngle = (int)((startAngle+sweepAngle)*3.14/180);
int r = width/2;
int ox = left + (width/2);
int oy = top + (height/2);
int x = (int)(r*Math.cos(textAngle));
int y = (int)(r*Math.sin(textAngle));
g.setColor(Color.black);
g.drawString(String.valueOf(slices[j].value), ox + x, oy - y);
curValue += slices[j].value;
}
}
/*+---------------+
| Ham main() |
+---------------+*/
public static void main(String args[]) {
Pie app = new Pie();
// Dinh nghia su kien khi ket thuc chuong trinh
app.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
}
}

public void drawPie(Graphics2D g, PieValue3D[] slices){
// Góc trên, trái và chi u dài, r ng c a hình ch nh tề ộ ủ ữ ậ
int top = 100, left = 150, width = 200, height = 100;
double total = 0.0D;
for(int j=0; j<slices.length; j++) {
total += slices[j].value;
}
//-----------------------------
// V bóng c a các cung trònẽ ủ
//-----------------------------
// Đ sâu c a bóngộ ủ
int depth = 10;
double curValue = 0.0D;
for(int x = depth; x>=1; x--) {
int startAngle = 0;
for(int j=0; j<slices.length; j++) {
startAngle = (int)(curValue*360/total);
int sweepAngle = (int)(slices[j].value*360/total);
if(j == slices.length-1) {
sweepAngle = 360 - startAngle;
}
// V cung v i màu t iẽ ớ ố
g.setColor(Color.darkGray);
// Chi u r ng l n h n chi u cao width > heightề ộ ớ ơ ề
g.fillArc(top, left + x, width, height, startAngle, sweepAngle);
curValue += slices[j].value;
}
}

