Skip to content

Instantly share code, notes, and snippets.

@taka2
Created November 11, 2009 08:11
Show Gist options
  • Select an option

  • Save taka2/231778 to your computer and use it in GitHub Desktop.

Select an option

Save taka2/231778 to your computer and use it in GitHub Desktop.
import java.awt.*;
import java.awt.image.*;
import java.io.File;
import javax.imageio.*;
public class JavaTest
{
public static void main(String[] args) throws Exception
{
BufferedImage bi = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bi.createGraphics();
String data = "2009/11/11";
for(int i=1; i<=200; i++)
{
g2d.setFont(new Font("Dialog", Font.PLAIN, i));
FontMetrics fm = g2d.getFontMetrics();
int width = 0;
for(int j=0; j<data.length(); j++)
{
width += fm.charWidth(data.charAt(j));
}
System.out.printf("Font size = %3d(pt), Width = %4d, Height = %4d\n", i, width, fm.getHeight());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment