import javax.swing.*;
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.awt.image.VolatileImage;

public class TextRenderingTest extends JPanel{
    final static int w = 450;
    final static int h = 150;
    public void paint(Graphics g) {

        Graphics2D paintg2d = (Graphics2D)g;

        GraphicsEnvironment ge =
                GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsConfiguration gc =
                ge.getDefaultScreenDevice().getDefaultConfiguration();
        VolatileImage vi = gc.createCompatibleVolatileImage(w, h);

        while (true) {
            vi.validate(gc);
            Graphics2D g2d = vi.createGraphics();
            g2d.setColor(Color.white);
            g2d.fillRect(0, 0, w, h);

            g2d.setPaint(new GradientPaint(
                    new Point2D.Float(0, h / 2), Color.white,
                    new Point2D.Float(w, h / 2), Color.black));
            g2d.fillRect(0, 0, w, h);

            String fnt = g2d.getFont().getFamily();
            g2d.setFont(new Font(fnt, Font.PLAIN, 100));
            g2d.drawString("IIIIIIIIII", 100, 100); // draw text with offset

            g2d.dispose();

            if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {}
                continue;
            }

            if (vi.contentsLost()) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {}
                continue;
            }

            break;
        }
        BufferedImage bi = vi.getSnapshot();
        paintg2d.drawImage(bi, null, null);
    }

    public static void main(String[] args) {
        System.setProperty("sun.java2d.metal", "tru");
        Frame f = new Frame();
        f.add(new TextRenderingTest());
        f.setSize(w, h);
        f.setVisible(true);
    }
}
