我試圖嵌入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();
}
}
任何幫助將不勝感激。謝謝。
'g.setFont(/ *這是什麼?* /);'...'像素'? – MadProgrammer
像素的方法之一,看起來像。儘管我似乎無法理解它們。 – user2720349
實際上,像素確實可以工作......但它使字體高度恰好爲1像素,所以直到現在我纔看到它。我認爲這些方法中的一種給它適當的大小。 – user2720349