ptg7068951
Baking a Pie Graph 337
13: background= getBackground();
14: }
15:
16: public voidaddSlice(Color sColor, floatsSize) {
17: if (current<= slice.length) {
18: slice[current] = new PieSlice(sColor, sSize);
19: totalSize+= sSize;
20: current++;
21: }
22: }
23:
24: public voidpaintComponent(Graphics comp) {
25: super.paintComponent(comp);
26: Graphics2D comp2D = (Graphics2D) comp;
27: int width = getSize().width- 10;
28: int height = getSize().height- 15;
29: int xInset = 5;
30: int yInset = 5;
31: if (width < 5) {
32: xInset = width;
33: }
34: if (height < 5) {
35: yInset = height;
36: }
37: comp2D.setColor(background);
38: comp2D.fillRect(0, 0, getSize().width, getSize().height);
39: comp2D.setColor(Color.lightGray);
40: Ellipse2D.Float pie = new Ellipse2D.Float(
41: xInset, yInset, width, height);
42: comp2D.fill(pie);
43: floatstart = 0;
44: for (int i = 0; i < slice.length; i++) {
45: floatextent = slice[i].size* 360F / totalSize;
46: comp2D.setColor(slice[i].color);
47: Arc2D.Float drawSlice = new Arc2D.Float(
48: xInset, yInset, width, height, start, extent,
49: Arc2D.Float.PIE);
50: start += extent;
51: comp2D.fill(drawSlice);
52: }
53: }
54: }
55:
56: classPieSlice {
57: Color color= Color.lightGray;
58: floatsize= 0;
59:
60: PieSlice(Color pColor, floatpSize) {
61: color= pColor;
62: size= pSize;
63: }
64: }
LISTING 23.1 Continued