2012-06-07 10 views
1

У меня есть вопрос об изменении размера символа, который нарисован функцией drawChar.Изменить размер шрифта шрифта

Я нашел решение с:

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

Но есть только три возможности для размера символа.
Есть ли способ увеличить размер?
Или это невозможно?

ответ

0

Вы можете использовать специальный моноширинный шрифт. Создайте файл в формате PNG со всеми персонажами вы можете покрасить и использовать ниже код из http://smallandadaptive.blogspot.com.br/2008/12/custom-monospaced-font.html:

 

    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

Смежные вопросы