2009-11-22 21 views
3

我有一個擴展框架的應用程序。然後,它會使用顯示文本的幾行:Java(AWT):將文本放在一個框中

Font f = new Font("Arial", Font.PLAIN, 10); 
g.setFont(f); 
g.drawString("Test|great Yes ^.", x, y + 10); 

現在發生的事情是,文本不適合在箱子周圍。例如。我期望文本適合[x,y] - [x +寬度,y + 10](不關心寬度),但它低於y + 10線。現在對於大多數人物('T','e'等)來說,這符合'|' 'g'不要!他們低於y + 10線。看來你不能使用:在y + characterHeight處繪製。但是什麼工作?

要明白我的意思,這裏的一些示例代碼:

import java.awt.*; 

public class test extends Frame 
{ 
     public test() 
     { 
       /* retrieve max window size */ 
       GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
       GraphicsDevice[] gs = ge.getScreenDevices(); 
       GraphicsConfiguration [] gc = gs[0].getConfigurations(); 
       Rectangle r = gc[0].getBounds(); 
       setSize(r.width, r.height); 
       setVisible(true); 
     } 

     public void paint(Graphics g) 
     { 
       final int windowWidth = getSize().width; 
       final int windowHeight = getSize().height; 
       g.setColor(Color.BLUE); 
       g.fillRect(0, 0, windowWidth, windowHeight); 
       g.setColor(Color.WHITE); 
       g.fillRect(0, 100, windowWidth, 110); 
       int textHeight = 100; 
       Font f = new Font("Arial", Font.PLAIN, textHeight); 
       g.setFont(f); 
       g.setColor(Color.BLACK); 
       g.drawString("Test|great Yes ^.", 10, 100 + textHeight); 
     } 

     public void guiLoop() 
     { 
       for(;;) { try { Thread.sleep(1000); } catch(Exception e) { } } 
     } 

     public static void main(String [] args) 
     { 
       new test().guiLoop(); 
     } 
} 

我嘗試下面的代碼,以及:

public void paint(Graphics g) 
{ 
     final int windowWidth = getSize().width; 
     final int windowHeight = getSize().height; 
     g.setColor(Color.BLUE); 
     g.fillRect(0, 0, windowWidth, windowHeight); 
     g.setColor(Color.WHITE); 
     g.fillRect(0, 100, windowWidth, 110); 
     int textHeight = 100; 

     String str = "Test|great Yes ^."; 
     Font f = new Font("Arial", Font.PLAIN, textHeight); 
     Rectangle2D boundingRectangle = f.getStringBounds(str, 0, str.length(), new FontRenderContext(null, false, false)); 
     f = f.deriveFont((float)(textHeight * (textHeight/boundingRectangle.getHeight()))); 
     boundingRectangle = f.getStringBounds(str, 0, str.length(), new FontRenderContext(null, false, false)); 
     g.drawString(str, 10, 100 + (int)boundingRectangle.getHeight()); 

     g.setFont(f); 
     g.setColor(Color.BLACK); 
     g.drawString(str, 10, 100 + textHeight); 
} 

這是稍微好:文字是小,所以它威力合適,但仍然存在y位置不正確的問題。

所有幫助表示讚賞!

+0

是guiLoop()真的有必要嗎? – Suppressingfire 2009-11-22 19:43:03

回答

4

怎麼樣使用FontMetrics?你可以從Graphics對象中獲得g.getFontMetrics()

比你可以檢索最大下降或上升或直接高度(使用getHeight),所以你的實現將是字體獨立的,它應該很好..檢查文檔here

編輯(解釋註釋): 沒有一種直接的方式告訴字符串以適合盒子的方式繪製自己。你必須自己做。就像從最大字體大小開始,並檢查寬度是否適合框,否則減小大小,然後再試一次。對於高度,您應該首先決定(或獲取)最大字體高度,然後您可以設置盒子的像素數。

+0

是的,特別是FontMetrics.stringWidth():http://java.sun.com/j2se/1.5.0/docs/api/java/awt/FontMetrics.html#stringWidth%28java.lang.String%29 – Suppressingfire 2009-11-22 19:43:38

+0

是的,但是如何處理這些指標?我試圖在y + fontmetrics.getAscent()處繪製字符串,並且還會將文本繪製得太低。 – 2009-11-22 19:48:05

+2

另外一個問題是,這些方法會告訴你文本會變得多大:是不是有一種方法可以說這個字符串必須適合這個邊界框? – 2009-11-22 19:49:21

0

我想我解決了這個問題有點:

boundingBoxHeight:框的高度,其中的文字應符合 Y偏移從哪裏開始繪製字體

  Font f = new Font("Arial", Font.PLAIN, boundingBoxHeight); 
      g.setFont(f); 
      FontMetrics fm = g.getFontMetrics(); 
      double shrink = ((double)textHeight/(double)fm.getHeight()); 
      double newSize = (double)textHeight * shrink; 
      double newAsc = (double)fm.getAscent() * shrink; 
      int yOffset = (int)newAsc - fm.getLeading(); 
      f = f.deriveFont((float)newSize); 
      g.setFont(f); 

      g.drawString(str, 10, 100 + yOffset); 

有相當多的空白的文本上方雖然。

相關問題