GT500FOMOCO
Programmer
I have a program that draws a pie chart with 4 slices. It draws the info from the Applet tag of the HTML file. I need it to draw 5 slices. Here is the code...
And if you can help me do that, can you also help me with one that will draw any number of slices that is specified in the HTML file? The one with 5 slices is priority 1, but it would be nice to have one that would do as many slices as I wanted.
Thanks... "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse
"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
Code:
Java...
import java.applet.Applet;
import java.awt.*;
import java.awt.geom.*;
public class PieChart extends Applet {
public void paint(Graphics g) {
Graphics2D pen = (Graphics2D) g;
final double INSET = 10;
double diameter = Math.min(getWidth(), getHeight()) - INSET * 2;
r = diameter / 2;
centerX = Math.round(INSET + r);
centerY = Math.round(INSET + r);
theta = 0;
pen.draw(new Ellipse2D.Double(INSET, INSET, diameter, diameter));
int value1 = Integer.parseInt(getParameter("value1").trim()),
value2 = Integer.parseInt(getParameter("value2").trim()),
value3 = Integer.parseInt(getParameter("value3").trim()),
value4 = Integer.parseInt(getParameter("value4").trim());
String name1 = getParameter("name1").trim(),
name2 = getParameter("name2").trim(),
name3 = getParameter("name3").trim(),
name4 = getParameter("name4").trim();
double total = value1 + value2 + value3 + value4;
drawPieSlice(pen, name1, value1 / total);
drawPieSlice(pen, name2, value2 / total);
drawPieSlice(pen, name3, value3 / total);
drawPieSlice(pen, name4, value4 / total);
}
public void drawPieSlice(Graphics2D pen, String name, double fraction) {
double thetaSlice = -fraction * 2 * Math.PI;
Line2D.Double segment =
new Line2D.Double(centerX, centerY,
centerX + r * Math.cos(theta),
centerY + r * Math.sin(theta));
pen.draw(segment);
theta = theta + thetaSlice / 2;
pen.drawString(name,
Math.round(centerX + r * Math.cos(theta)),
Math.round(centerY + r * Math.sin(theta)));
theta = theta + thetaSlice / 2;
}
private double theta, r;
private long centerX, centerY;
}
HTML...
<HTML>
<HEAD>
<TITLE>A Java Applet
</TITLE>
</HEAD>
<BODY>
<CENTER>
<APPLET CODE="PieChart.class" WIDTH=400 HEIGHT=500>
<PARAM NAME="name1" VALUE="Golden Gate">
<PARAM NAME="value1" VALUE="4200">
<PARAM NAME="name2" VALUE="Brooklyn">
<PARAM NAME="value2" VALUE="1595">
<PARAM NAME="name3" VALUE="Delaware Memorial">
<PARAM NAME="value3" VALUE="2150">
<PARAM NAME="name4" VALUE="Mackinac">
<PARAM NAME="value4" VALUE="3800">
</APPLET>
</CENTER>
</BODY>
</HTML>
And if you can help me do that, can you also help me with one that will draw any number of slices that is specified in the HTML file? The one with 5 slices is priority 1, but it would be nice to have one that would do as many slices as I wanted.
Thanks... "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse
"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy