Search code examples
javaopengllwjgl2d-gamesslick2d

drawing Strings in the (java game using LWJGL and slick2D) is not working properly


I am building a game in Java using some openGL libraries. I have almost finished the application but i have some really ridiculous problem. All I need to to is to change the default color of the text to some other color, in my case its black.

Exactly more than an hour i was trying to make this and every time i start a game the whole window turns into that color... I put the code down there and if any of you have some ideas I would like to consider that.

import java.awt.Font;
import org.newdawn.slick.Color;
import org.newdawn.slick.TrueTypeFont;
import player.Player;


public class GameMenu {

    private TrueTypeFont gameFont;
    private Font font;

    public GameMenu() {

    font = new Font("Times New Roman", Font.BOLD, 24);
    gameFont = new TrueTypeFont(font, false);

 }

    public void drawChangableText(int x, int y, String changableText) {
    gameFont.drawString(x, y, changableText, Color.black);
 }

    public void update() {
    drawChangableText(1330, 700, "Lives " + Player.lives);
    drawChangableText(1330, 750, "Gold " + Player.gold);
 }
}

And then I am calling this update method further somewhere. Let me not forget to mention that if omit 4. parameter of drawString() method, everything works perfectly fine but with white text on the screen.

Once again, if someone could help i would appreciate that. Maybe some of you will mark my question as duplicated but technically its not, someone had the similar problem but in his case he just imported wrong package. Here is the link of the similar problem

LWJGL Drawing colored text to the screen issue


Solution

  • You are drawing frames with current color, in this case you're using Color.black for everything in your frame not just the text.

    So to avoid something like that you can do this:

    public void drawChangableText(int x, int y, String text) {
        //pick your color
        Color.black.bind();
        //do the job
        gameFont.drawString(x, y, text, Color.black);
        //reset the color
        Color.white.bind();
    }