protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (total == 0) {
            return;
        }

        Graphics2D g2 = (Graphics2D) g;
        // for smooth drawing
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);

        changeSize();

        int startDeg = 0;
        int arcDeg;
        int arcDegCount = 0;
        int x, y;
        double angleRad;

        g2.setPaint(shadowColor); // shadow, down and to the right 3 pixels
        g2.fillOval(pieBorderWidth + shadowOffset,
                    pieBorderWidth + shadowOffset,
                    pieDiameter, pieDiameter);

        g2.setPaint(otherColor); // "other" is gray
        g2.fillOval(pieBorderWidth, pieBorderWidth, pieDiameter, pieDiameter);

        int wci = 0;
        int i;
        for (i = 0; i < wedges.length; i++) { // draw wedges
            double frac = ((Double) wedges[i].getValue()).doubleValue();
            arcDeg = (int) Math.round(((frac / total) * 360));
            arcDegCount += arcDeg;
            if ((i + 1) == this.wedges.length & arcDegCount != 360) {
                arcDeg += (360 - arcDegCount); // Avoid a gray wedge due to roundoff.
            }

            g2.setPaint(wedgeColor[wci++]);

            g2.fillArc(pieBorderWidth, pieBorderWidth, pieDiameter, pieDiameter,
                       startDeg, arcDeg);
            if (wci >= wedgeColor.length) {
                wci = 1; // Rotate colors.  Don't reuse 0 in case first and last are same.
            }
            startDeg += arcDeg;
        } // draw wedges

        startDeg = 0; // Do labels so they go on top of the wedges.
        for (i = 0; i < this.wedges.length; i++) {
            double frac = ((Double) wedges[i].getValue()).doubleValue();
            arcDeg = (int) Math.round(((frac / total) * 360));

            if (arcDeg > minLabeledWedge) { // Don't label small wedges.
                g2.setPaint(textColor);
                angleRad = (float) (startDeg + (arcDeg / 2)) *
                           java.lang.Math.PI /
                           180.0;
                x = pieCenterPosX +
                    (int) ((pieRadius / 1.3) * java.lang.Math.cos(angleRad));
                y = pieCenterPosY -
                    (int) ((pieRadius / 1.3) * java.lang.Math.sin(angleRad))
                    + 5; // 5 is about half the height of the text
                String name = (String) wedges[i].getKey();
                String perc = ": " + (int) Math.round(((frac / total) * 100)) +
                              "%";
                Font f = getFont();
                FontMetrics fm = g2.getFontMetrics(f);
                while ((fm.stringWidth(name + perc) + x) > getSize().width) {
                    if (name.length() < 4) {
                        name = null;
                        break;
                    } else {
                        name = name.substring(0, name.length() - 4) + "...";
                    }
                }

                if (name != null) {
                    String label = name + perc;
                    g2.drawString(label, x, y);
                }
            }

            startDeg += arcDeg;
        }
    }