2014-05-08 53 views
1

我想要以毫米爲單位獲取字符串的寬度和高度。我有以下代碼:Java:以毫米爲單位獲取字符串的寬度

public static void main(String[] args) 
{ 
    String text = "Sample STRING"; 
    AffineTransform affinetransform = new AffineTransform();  
    FontRenderContext frc = new FontRenderContext(affinetransform,true,true);  
    Font regularFont = new Font("Helvetica", Font.PLAIN, 8); 
    Font fractionalFont = regularFont.deriveFont(8.5f); 
    int textwidth = (int)(fractionalFont.getStringBounds(text, frc).getWidth()); 
    int textheight = (int)(fractionalFont.getStringBounds(text, frc).getHeight()); 

    System.out.println("Width of the text is:" + textwidth); 
    System.out.println("Height of the text is:" + textheight); 

} 

此外,它是非常重要的,我得到的字符串的實際尺寸,因爲該字符串是一個特定大小的標籤印刷;過去我沒有真正使用Java來查找字符串的寬度和高度;我從Java獲得的寬度和高度是否精確並且適用於真實世界的應用程序?客戶可以輸入大小寫,正如您所注意到的,字體大小爲8.5。

我將不勝感激任何幫助。

回答

相關問題