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
{
JButton button = new JButton( "The Test-Button" );
button.setSize( 200, 70 );
button.setLocation( 200, 150 );
final JFrame frame = new JFrame();
frame.setSize( 800, 600 );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setLayout( null );
frame.setLocationRelativeTo( null );
frame.add( button );
SwingUtilities.invokeLater( new Runnable() {
@Override public void run() {
frame.setVisible( true );
}
});
BufferedImage image = new BufferedImage( 800, 600, BufferedImage.TYPE_INT_ARGB );
Graphics2D g2d = (Graphics2D)image.getGraphics();
button.paint( g2d );
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:

, - , JFrame ( LookAndFeel , "WindowsLookAndFeel" ).
RenderingHint "g2d", Graphics2D- BufferedImage, :
g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB );
.
JButton , ( , , ), , - , - .
- !