|

Return to Unit
Menu | Java Main Page |
MathBits.com |
Terms
of Use
Demo Pie Graph

//Display a pie graph representing the number of A's, B's, C's, D's and F's
// on the Java Programming Final Exam
//notice the new
items for this style graph displayed in blue
import java.awt.*;
import BreezyGUI.*;
public class piegraph extends GBFrame
{
//Establish the look of the drop down box
Label LabelA = addLabel("A", 1,1,1,1);
IntegerField FieldA = addIntegerField(0,1,2,1,1);
Label LabelB = addLabel("B", 1,3,1,1);
IntegerField FieldB = addIntegerField(0,1,4,1,1);
Label LabelC = addLabel("C", 1,5,1,1);
IntegerField FieldC = addIntegerField(0,1,6,1,1);
Label LabelD = addLabel("D", 1,7,1,1);
IntegerField FieldD = addIntegerField(0,1,8,1,1);
Label LabelF = addLabel("F", 1,9,1,1);
IntegerField FieldF = addIntegerField(0,1,10,1,1);
//Establish a drop down menu for the drawing
//Right now we only have one choice -- Pier
MenuItem pieChoice = addMenuItem ("Graph",
"Pie");
//Establish variables for this program
int numberOfScores = 5;
int Xleft = 100;
int Xright = 300;
int Ytop = 100;
int Ybottom = 250;
int totalX , totalY;
int[ ] scores;
char graphChoice;
//Constructor to establish the initial values in the program
public piegraph ()
{
int i;
scores = new int[numberOfScores];
for (i=0; i<scores.length; i++)
scores[i] = 0;
totalX = Xright - Xleft + 1;
totalY = Ybottom - Ytop + 1;
graphChoice = 'P';
}
//Allow
for the menu choices
//Remember, we only have one choice right now
public void menuItemSelected (MenuItem item)
{
if (item == pieChoice)
graphChoice =
'P';
repaint();
}
public void paint (Graphics g)
{
getInputData();
drawPieGraph(g);
}
//Get input from the screen
public void getInputData()
{
scores[0] = FieldA.getNumber();
scores[1] = FieldB.getNumber();
scores[2] = FieldC.getNumber();
scores[3] = FieldD.getNumber();
scores[4] = FieldF.getNumber();
}
//Draw the pie graph
public void drawPieGraph(Graphics g)
{
int i,
totalUnits, centerX, centerY, radius, startAngle;
double
unitAngleSize;
//Set up center point and radius of the
pie, and the unit angle size
totalUnits =
sum(scores);
centerX = getSize().width
/ 2;
centerY =
getSize().height / 2;
radius = centerX -
centerX/3;
centerX = radius;
centerY = centerY -
centerY/3;
if (totalUnits == 0)
unitAngleSize = 0;
else
unitAngleSize = 360.0 / totalUnits;
startAngle = 0;
//Draw the wedges of
the pie
for(i=0; i < numberOfScores; i++)
{
int centralAngle = (int)(unitAngleSize
* scores[i]);
g.setColor(intToColor(i));
g.fillArc(centerX, centerY,
radius, radius, startAngle, centralAngle);
startAngle = startAngle +
centralAngle;
}
g.setColor(Color.black);
}
//Find the sum of the array
public int sum(int [ ] a)
{
int i;
int total = 0;
for (i=0; i<a.length; i++)
total = total + a[i];
return total;
}
//Set the colors for the pie slices
public Color intToColor (int i)
{
Color color =
Color.black;
switch (i)
{
case 0: {
color = Color.red;
break;
}
case 1: {
color = Color.green;
break;
}
case 2: {
color = Color.blue;
break;
}
case 3: {
color = Color.yellow;
break;
}
case 4: {
color = Color.magenta;
break;
}
}
return color;
}
//Main
public static void main(String[ ] args)
{
Frame frm = new
piegraph();
frm.setSize (400,
300);
frm.setVisible
(true);
}
}

Return to Unit
Menu | Java Main Page |
MathBits.com |
Terms
of Use
|