private BufferedImage getFontImage ( char ch ) { // Create a temporary image to extract the character's size BufferedImage tempfontImage = new BufferedImage ( 1 , 1 , BufferedImage . TYPE_INT_ARGB ) ; Graphics2D g = ( Graphics2D ) tempfontImage . getGraphics ( ) ; if ( antiAlias == true ) { g . setRenderingHint ( RenderingHints . KEY_ANTIALIASING , RenderingHints . VALUE_ANTIALIAS_ON ) ; } g . setFont ( font ) ; fontMetrics = g . getFontMetrics ( ) ; int charwidth = fontMetrics . charWidth ( ch ) ; if ( charwidth <= 0 ) { charwidth = 1 ; } int charheight = fontMetrics . getHeight ( ) ; if ( charheight <= 0 ) { charheight = fontSize ; } // Create another image holding the character we are creating BufferedImage fontImage ; fontImage = new BufferedImage ( charwidth , charheight , BufferedImage . TYPE_INT_ARGB ) ; Graphics2D gt = ( Graphics2D ) fontImage . getGraphics ( ) ; if ( antiAlias == true ) { gt . setRenderingHint ( RenderingHints . KEY_ANTIALIASING , RenderingHints . VALUE_ANTIALIAS_ON ) ; } gt . setFont ( font ) ; gt . setColor ( Color . WHITE ) ; int charx = 0 ; int chary = 0 ; gt . drawString ( String . valueOf ( ch ) , ( charx ) , ( chary ) + fontMetrics . getAscent ( ) ) ; return fontImage ; }
Create a standard Java2D BufferedImage of the given character