
In this installment of ACWCP with programming examples, I am presenting a Java application that creates a simple pie chart.
This version of the application extends JFrame to create a driver class (with main method) so that the calculations and logic in the paint method will draw a simple pie chart. It asks the user to input four values and then does the math to paint a pie chart with four slices.
You can change and use the code anyway you would like but understand it is use at your own risk with no warranties or guarantees (legal stuff).
The program starts with the main method. It will ask the user via JOptionPane input dialog boxes for four values. These values will be included in the declare/instantiation of JFPieChart as part of the constructor.
The constructor is called first. The values are passed as strings and converted to integers. A try/catch block makes sure that if a string number is not passed that the application will not abort with a runtime error. NumberFormatException handling was added to allow for a more graceful exit. You may want to change the code in the catch block depending on your application.
The paint method is called by the runtime environment whenever it needs to repaint the items in the paint method. Variables changed in your constructor will cause the paint method to fire and paint the sections of pie with the same point of origin. This has the effect of creating a pie wheel of pie slices. The last activity is to place a legend and title at the side and bottom of the window frame.
The Code:
// 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
}
Run it, change it and include it in your programs.
If you have a comment, send it along as a comment.
Happy Programming...
p.s. Anyone know where the picture came from?

1 comments:
very nice! hahahahaha
Post a Comment