import java.awt.*;
import java.applet.applet;
public class graph extends applet {
int depth, radius;
public void init() {
float value;
string at = getparameter("width");
radius = (at != null) ? integer.valueof(at).intvalue() : 100;
at = getparameter("depth");
depth = (at != null) ? integer.valueof(at).intvalue() : 20;
at = getparameter("values");
piechartcanvas c = new piechartcanvas(radius, depth);
setlayout(new borderlayout());
// create hashtable to map color name (string) to color type
hashtable colors = new hashtable();
colors.put("green", color.green);
colors.put("red", color.red);
colors.put("blue", color.blue);
colors.put("yellow", color.yellow);
colors.put("magenta", color.magenta);
colors.put("cyan", color.cyan);
colors.put("orange", color.orange);
colors.put("pink", color.pink);
colors.put("white", color.white);
colors.put("black", color.black);
// "value-color,value-color,..."
stringtokenizer t = new stringtokenizer(at, ",");
string s;
int i;
while (t.hasmoretokens()) {
s = t.nexttoken();
i = s.indexof(-);
value = float.valueof(s.substring(0, i)).floatvalue();
c.addslice(value, (color)colors.get(s.substring(i + 1)));
}
resize(c.getminimumsize().width, c.getminimumsize().height);
add("center", c);
}
}
class piechartcanvas extends canvas {
/*
** author ciaran treanor ciaran@broadcom.ie
*/
final double aspectfudge = 2.5;
int radius, depth, called = 1, numslices = 0;
float total = 0, value[] = new float[10];
color color[] = new color[10];
graphics offgraphics;
image gfxbuff;
public piechartcanvas(int radius, int depth) {
this.value = value;
this.color = color;
this.radius = radius;
this.depth = depth;
}
public void paint(graphics g) {
int startangle;
float angle;
dimension d = getsize();
if(gfxbuff == null) {
gfxbuff = createimage(d.width, d.height);
offgraphics = gfxbuff.getgraphics();
offgraphics.setcolor(getbackground());
offgraphics.fillrect(0, 0, d.width, d.height);
}
// do the 3d effect
for(int x = depth; x >= 1; x--) {
startangle = -45;
for(int i = 0; i < numslices; i++) {
offgraphics.setcolor(color[i].darker());
angle = math.round(360 * (value[i] / total));
offgraphics.fillarc(0, x, radius, (int)(radius / aspectfudge),
startangle, (int)angle);
startangle += angle;
}
}
// draw the pie slice
startangle = -45;
for(int i = 0; i < numslices; i++) {
offgraphics.setcolor(color[i]);
angle = math.round(360 * (value[i] / total));
offgraphics.fillarc(0, 0, radius, (int)(radius / aspectfudge),
startangle, (int)angle);
startangle += angle;
}
g.drawimage(gfxbuff, 0, 0, null);
}
public void addslice(float value, color color) {
this.value[numslices] = value;
this.color[numslices++] = color;
total += value;
}
public dimension getpreferredsize() {
return getminimumsize();
}
public dimension getminimumsize() {
return new dimension(radius, (int)((radius / aspectfudge) + depth));
}
}
[javapie.hmtl]
<html>
<applet code=graph.class width=150 height=150>
<param name="depth" value="30">
<param name="width" value="120">
<param name="values" value="1-red,2-green,3-blue">
</applet>
</hmtl>
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!


