2013-08-27 17 views
0

我試圖嵌入TTF字體,然後使用繪圖與Grapics2D。我已經能夠創建字體,但我不確定如何將字體傳遞給setFont。我在這裏做一個新的字體,它沒有拋出異常:嵌入TTF和使用g2d

private Font pixel = Font.createFont(Font.TRUETYPE_FONT, this.getClass().getResourceAsStream("font/amora.ttf")); 

但我無法弄清楚如何使用setfont程序繪製它();

這裏是我的代碼:

private static final long serialVersionUID = 1L; 
private Timer timer; 
private Char Char; 
private Font pixel = Font.createFont(Font.TRUETYPE_FONT, this.getClass().getResourceAsStream("font/amora.ttf")); <<<-------- 

public Board() throws FontFormatException, IOException { 

    addKeyListener(new TAdapter()); 
    setFocusable(true); 
    setBackground(Color.BLACK); 
    setDoubleBuffered(true); 

    Char = new Char(); 

    timer = new Timer(5, this); 
    timer.start(); 
} 


public void paint(Graphics g) { 
    super.paint(g); 

    Graphics2D g2d = (Graphics2D)g; 
    g2d.drawImage(Char.getImage(), Char.getX(), Char.getY(), this); 
    g.setColor(Color.white); 
    g.setFont(What goes here?); // <------------ 
    g.drawString("Amora Engine rev25 (acetech09)", 10, 20); 
    g.drawString(Char.getDebugStats(0), 10, 40); 
    g.drawString(Char.getDebugStats(1), 10, 60); 
    Toolkit.getDefaultToolkit().sync(); 
    g.dispose(); 
} 


public void actionPerformed(ActionEvent e) { 
    Char.move(); 
    repaint(); 
} 
} 

任何幫助將不勝感激。謝謝。

+0

'g.setFont(/ *這是什麼?* /);'...'像素'? – MadProgrammer

+0

像素的方法之一,看起來像。儘管我似乎無法理解它們。 – user2720349

+0

實際上,像素確實可以工作......但它使字體高度恰好爲1像素,所以直到現在我纔看到它。我認爲這些方法中的一種給它適當的大小。 – user2720349

回答

0

你可以只是做...

g.setFont(pixel); 

但你可能有

g.setFont(pixel.deriveFont(Font.BOLD, 36f)); 

是....

而且變化更好sucess,不處置Graphics您並未創建的上下文...

Graphics2D g2d = (Graphics2D)g; 
/*...*/ 
// g.dispose(); 

或者

Graphics2D g2d = (Graphics2D)g.create(); 
/*...*/ 
g.dispose(); 

我還可以厭惡覆蓋paint方法。假設您使用JComponentJPanel之類的東西,則應該使用paintComponent。如果你直接渲染一個頂級容器(如JFrame),那麼我不會。有雙緩衝和框架邊界的問題,不會讓你的生活變得有趣...

我也關注new Timer(5, this) - 5毫秒已經足夠接近0,幾乎沒有什麼區別。你會用類似40,這應該給你的東西像25fps的或17這將給你大概60fps的更好...

0

應該

g.setFont(this.pixel); 

如果不工作,嘗試:

  1. 註釋掉setFont指令。
  2. 替換Font.createFont參照Java標準字體。

排除可能的問題。