public void drawPie(Graphics2D g, PieValue[] slices){
// Góc tn, trái 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 tng 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: 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 ln 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 nn)
// g.drawRect(top, left, width, height);
// V cung tn (ti p giáp v i hình ch nh t có c trên ế
// là 100,100; chi u r ng 200, chi u cao 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 nn
int x = (int)(r*Math.cos(textAngle));
int y = (int)(r*Math.sin(textAngle));
g.setColor(Color.black);
// V nn
g.drawString(String.valueOf(slices[j].value), ox + x, oy - y);
// Tăng g 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 cung tròn
//-----------------------------
// Đ 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;
}
}