Java2D / Swing: rendering a text-smoothed component in BufferedImage

I would like to display the Java Swing component, for example. JButton, which I also put on JFrame, in BufferedImage. This works in general, but with a big drawback: text smoothing, especially the LCD "smooth" mode, does not work when rendering in BufferedImage.

I gave some code examples to demonstrate the problem, but first my system information:

  • OS : Windows 7 64 bit
  • JVM : 1.6.0_26-b03 (32 bit)

The following sample code will create a simple JFrame, place a JButton on it, and then turn the JButton into a test.png file:

public class TextAntiAliasingTest
{
  public TextAntiAliasingTest() throws IOException
  {
    // Create Test-Button which will be rendered to an image
    JButton button = new JButton( "The Test-Button" );
    button.setSize( 200, 70 );
    button.setLocation( 200, 150 );

    // Create JFrame
    final JFrame frame = new JFrame();
    frame.setSize( 800, 600 );
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    frame.setLayout( null );
    frame.setLocationRelativeTo( null );
    frame.add( button );

    // Show JFrame
    SwingUtilities.invokeLater( new Runnable() {
        @Override public void run() {
            frame.setVisible( true );
        }
    });

    // Render JButton to an BufferedImage
    BufferedImage image = new BufferedImage( 800, 600, BufferedImage.TYPE_INT_ARGB );
    Graphics2D g2d = (Graphics2D)image.getGraphics();
    button.paint( g2d );

    // Write BufferedImage to a PNG file
    ImageIO.write( image, "PNG", new File( "test.png" ) );
  }

  public static void main( String[] args ) throws Exception
  {
    UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );
    System.setProperty( "awt.useSystemAAFontSettings", "lcd" );

    new TextAntiAliasingTest();
  }
}

The following figure shows the difference between the JButton in the JFrame on the screen and the same JButton displayed in the image file:

enter image description here

, - , JFrame ( LookAndFeel , "WindowsLookAndFeel" ).

RenderingHint "g2d", Graphics2D- BufferedImage, :

g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, 
    RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB );

.

JButton , ( , , ), , - , - .

- !

+5
3

JVM

, . , , . , : http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6749069

. , :

private void drawString(String text, int x, int y, Graphics2D g, Color bg){    
    // Prepare an off-screen image to draw the string to
    Rectangle2D bounds = g.getFontMetrics().getStringBounds(text, g);
    BufferedImage image = configuration.createCompatibleImage(
                              (int)(bounds.getWidth() + 1.0f), 
                              (int)(bounds.getHeight() + 1.0f),
                              Transparency.OPAQUE);
    Graphics2D ig = image.createGraphics();

    // Fill the background color
    ig.setColor(bg);
    ig.fillRect(0, 0, image.getWidth(), image.getHeight());

    // Draw the string
    int x0 = 0;
    int y0 = ig.getFontMetrics().getAscent();
    ig.setColor(g.getColor());
    ig.setRenderingHints(g.getRenderingHints());
    ig.setFont(g.getFont());
    ig.drawString(text, x0, y0);
    ig.dispose();

    // Blit the image to the destination
    g.drawImage(image, x-x0, y-y0, null);
}

, , A. C. , D : D += A*C D = D*(1-A)+A*C .

+3

, . BufferedImage, . , Ok:

BufferedImage image = new BufferedImage( 800, 600, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D)image.getGraphics();
g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);

, .

+2

, (a) BufferedImage , JButton, (b) , JButton BufferedImage .

(b), JButton? , BufferedImage?

(a) , JButton BufferedImage ( Java BufferedImage, ). - BufferedImage ( , ). BufferedImage, , - , .

0

All Articles