简体   繁体   English

Slick2D-如何使用字体

[英]Slick2D - How to use Fonts

I am coding a game in fonts and I encountered a problem. 我在用字体编码游戏时遇到问题。 I have no idea how to use fonts even though I tried everything. 即使我尝试了所有方法,我也不知道如何使用字体。 Can you also please explain what is a glyph? 您还可以解释什么是字形吗? And where to get .fnt files if that is needed? 如果需要,从哪里获取.fnt文件?

A lot of that stuff is either no longer used, or just not necessary anymore. 很多东西要么不再使用,要么不再需要。 Basically all you need is a Graphics instance, and to use something like the drawString method. 基本上,您只需要一个Graphics实例,并使用诸如drawString方法之类的东西。 You can also use setFont on the Graphics object to set the font you want to use for rendering. 您还可以在Graphics对象上使用setFont来设置要用于渲染的字体。

You can get a Graphics instance from your GameContainer , and then just render to it. 您可以从GameContainer获取Graphics实例,然后将其渲染。 If you want to see an example, the render method of this file in my project on github has some lines in it that render debug information to the screen (fps, memory usage, etc.). 如果您想查看一个示例,我在github上的项目中此文件render方法中包含一些行,这些行将调试信息呈现到屏幕上(fps,内存使用量等)。 The relevant part of the code is: 该代码的相关部分是:

if (ConfigValues.renderSystemInfo) {
  g.setColor(new Color(50, 50, 50, 180));
  g.fillRect(0, 0, 300, 70);
  g.setColor(Color.white);
  g.drawString("MEM total(used):   " + (Runtime.getRuntime().totalMemory()/1000000) + "(" + ((Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())/1000000) + ") MB", 10, 25);
  g.drawString("Ped. history size: " + (peds.size()*peds.get(0).getMovementHistory().size()) + " nodes", 10, 40);
}

Just for reference, a glyph, at least conceptually, is just the visual representation of a character in a font. 仅作为参考,字形至少在概念上仅是字体中字符的视觉表示。 So, the way that the letter a looks in a given font, for example, is that font's glyph for that letter. 因此,例如,字母a以给定字体显示的方式是该字母a字体字形 There's a different glyph for upper and lowercase ( a or A ), which is what makes the upper and lowercase letters appear different in fonts. 大写和小写字母( aA )有不同的字形,这就是使大写和小写字母在字体中看起来不同的原因。

Font font = new Font('Verdana', Font.BOLD, 32);
TrueTypeFont ttf = new TrueTypeFont(font, true);

In your update method: 在您的更新方法中:

ttf.drawString(32.0f, 32.0f, "Your words here", Color.green);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM