2012-06-07 33 views
1

我有一個關於改變繪製drawChar函數的字符大小的問題。修改char size字體

我已經找到了一個解決方案:

setFont(Font.getFont(Font.FONT_STATIC_TEXT, Font.STYLE_BOLD, Font.SIZE_LARGE)); 

卻只有3字符的大小可能。
是 - 有一種方法來增加大小?
或者這是不可能的?

回答

0

您可以使用自定義等寬字體。你可能會油漆和使用下面的代碼從http://smallandadaptive.blogspot.com.br/2008/12/custom-monospaced-font.html的所有字符創建一個PNG文件:

 

    public class MonospacedFont { 

    private Image image; 
    private char firstChar; 
    private int numChars; 
    private int charWidth; 

    public MonospacedFont(Image image, char firstChar, int numChars) { 
     if (image == null) { 
      throw new IllegalArgumentException("image == null"); 
     } 
     // the first visible Unicode character is '!' (value 33) 
     if (firstChar <= 33) { 
      throw new IllegalArgumentException("firstChar <= 33"); 
     } 
     // there must be at lease one character on the image 
     if (numChars <= 0) { 
      throw new IllegalArgumentException("numChars <= 0"); 
     } 
     this.image = image; 
     this.firstChar = firstChar; 
     this.numChars = numChars; 
     this.charWidth = image.getWidth()/this.numChars; 
    } 

    public void drawString (Graphics g, String text, int x, int y) { 
     // store current Graphics clip area to restore later 
     int clipX = g.getClipX(); 
     int clipY = g.getClipY(); 
     int clipWidth = g.getClipWidth(); 
     int clipHeight = g.getClipHeight(); 
     char [] chars = text.toCharArray(); 

     for (int i = 0; i < chars.length; i++) { 
      int charIndex = chars[i] - this.firstChar; 
      // current char exists on the image 
      if (charIndex >= 0 && charIndex <= this.numChars) { 
       g.setClip(x, y, this.charWidth, this.image.getHeight()); 
       g.drawImage(image, x - (charIndex * this.charWidth), y, Graphics.TOP | Graphics.LEFT); 
       x += this.charWidth; 
      } 
     } 

     // restore initial clip area 
     g.setClip(clipX, clipY, clipWidth, clipHeight); 
    } 
    } 

下面是一個使用這個類的樣本代碼。

 

    Image img; 
    try { 
     img = Image.createImage("/monospaced_3_5.PNG"); 
     MonospacedFont mf = new MonospacedFont(img, '0', 10); 
     mf.drawString(g, "", 40, 40); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

+0

Thans爲您的迴應。我選擇了其他解決方案,其中包含爲每個數字繪製一個位圖。 – Jazys