How can I display text with Slick?
Solution 1
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.
Alternatively, you can initialize a font instance and use it directly to drawString
. From 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.
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.
Solution 2
Download the Slick2D library and import the 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!")
}
Solution 3
According to this tutorial, you can call drawString
from your graphics object in the render
method.
g.drawString("Hello World!",200,200);
Taylor Golden
Hey there! I am a 13 year old programmer. I have experience with: HTML CSS Java JavaScript and a bit of PHP I enjoy coding because of the result. Oregon, the game I'm working on at the moment, will be up soon, hopefully!
Updated on June 08, 2022Comments
-
Taylor Golden almost 2 years
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.
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.
-
Taylor Golden almost 12 yearsThanks, I'll give it a try now. Sorry for my comment earlier. :'(
-
David B almost 12 yearsNo problem. If it works for you, mark the answer accepted by clicking on the green checkmark underneath the vote number.
-
Taylor Golden almost 12 yearsSorry, the tutorial I find really hard to follow. But thanks anyway.
-
David B almost 12 years@TaylorGolden How proficient are you with Java, sans Slick? Learning this library may be difficult if you don't possess some core Java skills.
-
Taylor Golden almost 12 yearsThat tutorial is for if I am extending by the BasicGame class. I'm not though. Thanks but it wasn't the right one for me. Mostly my fault for not posting the code in the question. I will do that now.
-
Taylor Golden almost 12 yearsThat comes up with a green streak across the screen. Are there any "gl" things I need to add?
-
Taylor Golden almost 12 yearsI'll post a pic of what I'm getting!
-
Jesse Webb almost 12 yearsWhat *Game class you use doesn't change how you
drawString()
. What does matter is that it appears as though you aren't using Slick2D at all, only lwjgl. You may want to search for lwjgl tutorials specifically, or try going through the 'Spiegel' tutorials here: slick.cokeandcode.com/wiki/doku.php?id=tutorials -
Andy over 7 yearsCan you pls explain where you got that graphics variable from
-
Andy over 7 yearswhere do you get that g variable from
-
Jesse Webb over 7 years@IchHabsDrauf This code is meant to be executed inside a
render
method which has a signature of:public void render(GameContainer gc, Graphics g)
. It is the Slick library which calls this function for you and provides you with theGraphics
instance.