java 如何使用 Slick 显示文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11144869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I display text with Slick?
提问by Taylor Golden
I have been trying to display text with ninjacave's tutorials and lot's of others but I can't get it to work. Could someone give me a link to a tutorial that works? Thanks.
我一直在尝试使用 ninjacave 的教程和许多其他教程来显示文本,但我无法让它工作。有人可以给我一个有效教程的链接吗?谢谢。
The code is below:
代码如下:
Main Class:
主要类:
package oregon.client;
import oregon.src.*;
import org.lwjgl.*;
import org.lwjgl.opengl.*;
import static org.lwjgl.opengl.GL11.*;
public class Oregon {
public static Settings settings = new Settings();
public GameController controls = new GameController();
public FPSCounter fps = new FPSCounter();
public void init() {
try {
if (settings.fullscreen) {
Display.setDisplayModeAndFullscreen(Display
.getDesktopDisplayMode());
} else if (!settings.fullscreen) {
Display.setDisplayModeAndFullscreen(new DisplayMode(
settings.defaultWidth, settings.defaultHeight));
}
Display.setVSyncEnabled(true);
Display.create();
} catch (LWJGLException e) {
stop(e);
}
initOpenGL();
start();
}
public void initOpenGL() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, settings.defaultWidth, settings.defaultHeight, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
}
public void debug() {
}
public void openGL() {
}
public void start() {
while (!Display.isCloseRequested()) {
controls.keyboard();
fps.tick();
debug();
openGL();
Display.update();
Display.sync(100);
}
Display.destroy();
}
public static void stop() {
System.exit(0);
Display.destroy();
}
public static void stop(Exception e) {
e.printStackTrace();
System.exit(0);
Display.destroy();
}
public static void setFullscreen() {
try {
Display.setDisplayModeAndFullscreen(Display.getDesktopDisplayMode());
} catch (LWJGLException e) {
stop(e);
}
}
public static void removeFullscreen() {
try {
Display.setDisplayModeAndFullscreen(new DisplayMode(
settings.defaultWidth, settings.defaultHeight));
} catch (LWJGLException e) {
stop(e);
}
}
public static void main(String args[]) {
Oregon oregon = new Oregon();
oregon.init();
}
}
That was the code for the main-class.
那是主类的代码。
EDIT:-This is a picture of what I'm getting with Jesse B's code.
编辑:-这是我用 Jesse B 的代码得到的图片。
回答by Jesse Webb
DavidB mentions in his answer using the graphics
instance to drawString()
. This will use your current font. This means you should call graphics.setFont()
first, otherwise it will use the default system font. This also makes it hard to remember (in various methods of your game) which is the 'current' font.
DavidB 在他的回答中提到使用graphics
实例 to drawString()
。这将使用您当前的字体。这意味着您应该graphics.setFont()
先调用,否则它将使用默认的系统字体。这也使您很难记住(在您的游戏的各种方法中)哪个是“当前”字体。
Alternatively, you can initialize a font instance and use it directly to drawString
. From Slick2D Wiki...
或者,您可以初始化字体实例并将其直接用于drawString
. 来自Slick2D Wiki...
// initialise the font
Font font = new Font("Verdana", Font.BOLD, 20);
TrueTypeFont trueTypeFont = new TrueTypeFont(font, true);
// render some text to the screen
trueTypeFont.drawString(20.0f, 20.0f, "Slick displaying True Type Fonts", Color.green);
This requires that all supported platforms can resolve the font name. A better option is to embed the font in your JAR by placing it it your resources directory.
这要求所有支持的平台都可以解析字体名称。更好的选择是将字体嵌入到 JAR 中,方法是将其放置在资源目录中。
EDIT
After seeing the picture you posted, my guess is that you don't have the font loading correctly. Try this instead...
编辑
看到您发布的图片后,我的猜测是您没有正确加载字体。试试这个...
graphics.drawString("Test drawing string.",20.0f,20.0f);
If this works, it confirms the font isn't working. If you want to use custom fonts, you will have to add your font file into your JAR and reference it as a resource.
如果这有效,则确认字体无效。如果要使用自定义字体,则必须将字体文件添加到 JAR 中并将其作为资源引用。
回答by Michael Curry
Download the Slick2D library and import the jar.
下载 Slick2D 库并导入 jar。
Inside your class to cover the scope of the whole class:
在您的班级内覆盖整个班级的范围:
Font font;
TrueTypeFont ttf;
Your init method
你的初始化方法
public void init(GameContainer gc){
font = new Font("Verdana", Font.BOLD, 20);
ttf = new TrueTypeFont(font, true);
}
Your render method:
你的渲染方法:
public void render(GameContainer gc, Graphics g){
//render code here
ttf.drawString("Hello, World!")
}
回答by David B
According to this tutorial, you can call drawString
from your graphics object in the render
method.
根据本教程,您可以drawString
在render
方法中从图形对象调用。
g.drawString("Hello World!",200,200);
g.drawString("Hello World!",200,200);