protected void paintCenteredText(Graphics2D g2, String s, Rectangle rect,
            double fontHeight, Color color)
    {
        g2 = (Graphics2D) g2.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setPaint(color);
        Rectangle2D bounds = null;
        LineMetrics lm = null;
        boolean done = false;
        // shrink font in increments of sqrt(2)/2 until string fits
        while (!done)
        {
            g2.setFont(new Font("SansSerif", Font.BOLD,
                    (int) (fontHeight * rect.height)));
            FontRenderContext frc = g2.getFontRenderContext();
            bounds = g2.getFont().getStringBounds(s, frc);
            if (bounds.getWidth() > rect.getWidth())
                fontHeight = fontHeight * Math.sqrt(2) / 2;
            else
            {
                done = true;
                lm = g2.getFont().getLineMetrics(s, frc);
            }
        }
        float centerX = rect.x + rect.width / 2;
        float centerY = rect.y + rect.height / 2;
        float leftX = centerX - (float) bounds.getWidth() / 2;
        float baselineY = centerY - lm.getHeight() / 2 + lm.getAscent();
        g2.drawString(s, leftX, baselineY);
        g2.dispose();
    }