This tweaked, standalone version of your code works for me in Sun's J2SDK 1.4.2. I suspect my default FontRenderTransform is different from the one you're getting from your graphics. Try making sure that fractionalMetrics is true.
BTW, in general you should avoid using Font.createGlyphVector unless you know all your text is ASCII. Also, createGlyphVector has some overhead (you are creating several objects, after all) and you might as well avoid it when you can.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| import java.awt.Font; import java.awt.font.FontRenderContext; import java.awt.font.GlyphVector;
public class TestScale { public static void main(String[] args) { int pw = 200; for (int i = 0; i < args.length; ++i) { if (args[i].startsWith("-w")) { pw = Integer.parseInt(args[++i]); } } System.out.println("Starting panel width: " + pw);
String[] allTexts = { "A short line.", "A longer line.", "Too short.", "Like, way longer than the others.", "Long, but not so long." }; Font f = new Font("Dialog", Font.PLAIN, 12); Font[] allFonts = { f, f, f, f, f }; int longestLine = 0 ; FontRenderContext fontRC = new FontRenderContext(null, true, true); double longestWidth = allFonts [ longestLine ] .createGlyphVector ( fontRC , allTexts [ longestLine ] ) .getPixelBounds ( null, 0 , 0 ).getWidth () ; for ( int i = 1 ; i < allTexts.length ; i ++ ) { double temp; if ( (temp = allFonts [ i ].createGlyphVector ( fontRC , allTexts [ i ] ) .getPixelBounds ( null, 0 , 0 ).getWidth ()) > longestWidth ) { longestLine = i ; longestWidth = temp; } } System.out.println ( allTexts [ longestLine ] ) ;
for (int panelWidth = pw, i = 0; i < 10; panelWidth += 5, ++i) { Font maxFont = allFonts [ longestLine ] ; String maxWidth = allTexts [ longestLine ] ; float widthRatio = maxFont.getSize2D () / ( float ) longestWidth ; float fontSize = panelWidth * widthRatio ; maxFont = maxFont.deriveFont ( fontSize ) ; int iterations = 0; while ( Math.abs(panelWidth - longestWidth) > .5 ) { fontSize = (float)(fontSize * panelWidth / longestWidth) ; maxFont = maxFont.deriveFont ( fontSize ) ; longestWidth = maxFont .createGlyphVector ( fontRC , maxWidth ) .getPixelBounds ( null , 0 , 0 ).getWidth () ;
++iterations; } System.out.println ( "Panel Width: " + panelWidth + " MaxWidth: " + longestWidth + " iterations: " + iterations + " font size: + maxFont.getSize2D()); } } } |
Here's my results on a win2k system:
Starting panel width: 200
Like, way longer than the others.
Panel Width: 200 MaxWidth: 200.0 iterations: 2 font size: 13.927027
Panel Width: 205 MaxWidth: 205.0 iterations: 2 font size: 14.279213
Panel Width: 210 MaxWidth: 210.0 iterations: 3 font size: 14.621629
Panel Width: 215 MaxWidth: 215.0 iterations: 3 font size: 14.954442
Panel Width: 220 MaxWidth: 220.0 iterations: 3 font size: 15.287282
Panel Width: 225 MaxWidth: 225.0 iterations: 2 font size: 15.689565
Panel Width: 230 MaxWidth: 230.0 iterations: 2 font size: 16.022388
Panel Width: 235 MaxWidth: 235.0 iterations: 3 font size: 16.37691
Panel Width: 240 MaxWidth: 240.0 iterations: 3 font size: 16.71169
Panel Width: 245 MaxWidth: 245.0 iterations: 2 font size: 17.116066