-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPieChart.java
More file actions
32 lines (30 loc) · 966 Bytes
/
PieChart.java
File metadata and controls
32 lines (30 loc) · 966 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class PieChart extends JFrame {
public static void main(String[] args) {
new PieChart();
}
public PieChart() {
super("Pie Chart");
setSize(400, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void paint(Graphics g) {
int[] values = {25, 35, 20, 10, 10};
Color[] colors = {Color.red, Color.green, Color.blue, Color.yellow, Color.orange};
int startAngle = 0;
int arcAngle = 0;
int total = 0;
for (int i = 0; i < values.length; i++) {
total += values[i];
}
for (int i = 0; i < values.length; i++) {
g.setColor(colors[i]);
arcAngle = (int) Math.round((values[i] * 360.0) / total);
g.fillArc(100, 100, 200, 200, startAngle, arcAngle);
startAngle += arcAngle;
}
}
}