// Gary Marrer - JFPieChart.java
// October 2008
// Use this program example at your own risk. This code was created for educational purposes only.
// The author provides programming code "as is" and provides no warranty or guarantee. If you have questions,
// you can email the author at gmarrer@gmail.com
// import relevant package files
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
// extend JFrame to test pie chart creation
// you can use the constructor and paint method in other classes
public class JFPieChart extends JFrame
{
// class instance variables
// used by multiple methods inside this class
private int a, b, c, d, ax, bx, cx, dx, tot;
private static String first, second, third, forth;
// constructor sets private instance variables
public JFPieChart(String v1, String v2, String v3, String v4)
{
// turn on exception handling for NumberFormatException
try
{
//convert String values to int for pie chart calculation
a = Integer.parseInt(v1);
b = Integer.parseInt(v2);
c = Integer.parseInt(v3);
d = Integer.parseInt(v4);
// add the values
tot = a + b + c + d;
// calculate pie section
ax = (360 * a) / tot;
bx = (360 * b) / tot;
cx = (360 * c) / tot;
dx = (360 * d) / tot;
}
// exception handling
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Error Message- Number Format Exception: " + e.getMessage(), "Error message",JOptionPane.ERROR_MESSAGE);
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Unknown Error Message - Aborting " + e.getMessage(), "Error message",JOptionPane.ERROR_MESSAGE);
}
} // end JFPieChart
// paint called by system when screen needs refreshing
public void paint(Graphics g)
{
// used to create Java2D API environment
Graphics2D g2d = (Graphics2D) g;
// Chart Title - set font, color and background
g.setFont(new Font("Arial", Font.BOLD, 16));
g.drawString("Sample Pie Chart",15,200);
g.setColor(Color.black);
g.fillRect(0,0,500,500);
// Start Java2D code
// set colors, stroke and location of point to anchor pie slice
// place each pie slice next to the other
g2d.setPaint(Color.blue);
g2d.setStroke( new BasicStroke(1.0f));
g2d.fill(new Arc2D.Double(15,35, 80, 80, 0 , ax, Arc2D.PIE));
g2d.setPaint(Color.orange);
g2d.setStroke( new BasicStroke(1.0f));
g2d.fill(new Arc2D.Double(15,35, 80, 80, 0 + ax, bx, Arc2D.PIE));
g2d.setPaint(Color.green);
g2d.setStroke( new BasicStroke(1.0f));
g2d.fill(new Arc2D.Double(15,35, 80, 80, 0 + ax + bx, cx, Arc2D.PIE));
g2d.setPaint(Color.white);
g2d.setStroke( new BasicStroke(1.0f));
g2d.fill(new Arc2D.Double(15,35, 80, 80, 0 + ax + bx + cx , dx, Arc2D.PIE));
// draw Legend - color
g.setColor(Color.white);
g.drawString( "Value 1 = " + a ,250,50);
g.drawString("Value 2 = " + b ,250,100);
g.drawString("Value 3 = " + c ,250,150);
g.drawString("Value 4 = " + d ,250,200);
} // end paint
// main is used in this program to provide input to the pie chart
//drawing routines
public static void main(String args[])
{
// ask user for four values
first = JOptionPane.showInputDialog("Enter First Number");
second = JOptionPane.showInputDialog("Enter Second Number");
third = JOptionPane.showInputDialog("Enter Third Number");
forth = JOptionPane.showInputDialog("Enter Forth Number");
// insatiate the class and call constructor. Pass four user values.
JFPieChart pie = new JFPieChart(first, second, third, forth);
// size of frame
pie.setSize(500,500);
// close app when frame is closed
pie.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// make pie chart visible - this needs to be last statement before Frame is displayed
pie.setVisible(true);
} // end main
}