The main method class:
package testcs;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.Color;
public class Main {
public static void main(String[] args)
{
JFrame win=new JFrame();
win.setSize(600,600);
win.setTitle("Pie Chart");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int num=0;
String school="";
double students=0.0;
int r, g, b=0;
num=Integer.parseInt(JOptionPane.showInputDialog("Enter your number " +
"of universities: "));
String[] schools=new String[num];
double[] population=new double[num];
Color[] colors=new Color[num];
int count=0;
while(count==0)
{
for(int i=0; i<num; i++)
{
school=JOptionPane.showInputDialog("Enter a university: ");
students=Double.parseDouble(JOptionPane.showInputDialog("How " +
"many students does "+school+" have?"));
r=Integer.parseInt(JOptionPane.showInputDialog("How much red " +
"is in "+school+"'s color?"));
g=Integer.parseInt(JOptionPane.showInputDialog("How much " +
"green is in "+school+"'s color?"));
b=Integer.parseInt(JOptionPane.showInputDialog("How much blue" +
" is in "+school+"'s color?"));
Color c=new Color(r, g, B)/>;
schools[i]=school;
population[i]=students;
colors[i]=c;
}
PieChart p=new PieChart(schools, population, colors);
PieComponent pc=new PieComponent(p);
win.add(pc);
win.setVisible(true);
break;
}
}
}
The PieChart class
package testcs;
import java.awt.geom.Arc2D;
import java.awt.Color;
import java.awt.Graphics2D;
public class PieChart
{
private String[] uni_name;
private double[] students;
private Color color[];
//Constructors
public PieChart(String[] u, double[] s, Color[] c)
{
this.uni_name=u;
this.students=s;
this.color=c;
}
//methods
public double total(double t)
{
double temp=0.0;
for(int i=0; i<students.length;i++)
{
temp=students[i];
if(i+1<students.length)
t=temp+students[i+1];
}
return t;
}
public double average(double a)
{
double t=0;
double temp=0.0;
for(int i=0; i<=students.length;i++)
{
temp=students[i];
if(i+1<=students.length)
t=temp+students[i+1];
}
a=t/students.length;
return a;
}
public String smallestUniversity()
{
String min="";
for(int i=0; i<uni_name.length; i++)
{
if(i==0)
min=uni_name[0];
else
{
if(students[i+1]<uni_name.length)
if(students[i]<students[i+1])
min=uni_name[i];
else
min=uni_name[i+1];
}
}
return min;
}
public String largestUniversity()
{
String max="";
for(int i=0; i<uni_name.length; i++)
{
if(i==0)
max=uni_name[0];
else
{
if(students[i+1]<uni_name.length)
if(students[i]>students[i+1])
max=uni_name[i];
else
max=uni_name[i+1];
}
}
return max;
}
public void drawChart(Graphics2D g, int x, int y)
{
// Get total value of all slices
double temp=0.0;
double total=0.0;
for(int i=0; i<students.length;i++)
{
temp=students[i];
if(i+1<students.length)
total=temp+students[i+1];
}
// Draw each pie slice
double curValue = 0.0;
int startAngle = 0;
for (int i=0; i<students.length; i++)
{
// Compute the start and stop angles
startAngle = (int)(curValue * 360 / total);
int arcAngle = (int)(students[i] * 360 / total);
// Set the color and draw a filled arc
g.setColor(color[i]);
g.fill(new Arc2D.Double(200, 300, 300, 300, startAngle,
arcAngle, Arc2D.PIE));
curValue += students[i];
}
}
}
The JComponent
package testcs;
import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Graphics2D;
public class PieComponent extends JComponent
{
private PieChart chart;
public PieComponent(PieChart s)
{
chart=s;
}
public void paintComponent(Graphics g)
{
Graphics2D g2=(Graphics2D)g;
chart.drawChart(g2, 100, 200);
}
}
Thank you!
This post has been edited by croog24: 04 May 2010 - 09:38 PM

New Topic/Question
Reply




MultiQuote





|