Search code examples
javatextfieldlwjglslick2d

textfield not working with BasicGameState


I've tested TextField on a clean BasicGame and it worked. (I see the border and i can type.)

The code:

            TextField lanText;  
            TrueTypeFont font;

            public void init(........)
        {
            font = new TrueTypeFont(new java.awt.Font(java.awt.Font.SERIF,java.awt.Font.BOLD,8),false);
            lanText = new TextField(gc, font, 50, 100, 350, 25);

}

    public void render(.....)
        {  
            lanText.render(gc, g);
        }

But when i try it on my game with BasicGameState it doesn't work. What's wrong? (I can see the border but I cant type)

I have 4 states. Menu 0. Game 1. Coop 2. Options 3.

Im trying to add it to the state 2(Coop).

public class Game
extends StateBasedGame
{
    public final int menu = 0;
    public final int game = 1;
    public final int option = 2;
    public final int coop = 3;

    public Game(String gamename)
    {
        super(gamename);
        this.addState(new Menu(menu));
        this.addState(new Game(game));
        this.addState(new Option(option));
        this.addState(new Coop(coop));
    }

public void initStatesList(GameContainer gc)
    throws SlickException
    {
    this.getState(menu).init(gc, this);
    this.getState(game).init(gc, this);
    this.getState(option).init(gc, this);
    this.getState(coop).init(gc, this);
    this.enterState(menu);
}
}

Solution

  • Ok, I figured it out. Here is the code for anyone who will have the same problem.

    import java.awt.Font;
    import org.newdawn.slick.GameContainer;
    import org.newdawn.slick.Graphics;
    import org.newdawn.slick.SlickException;
    import org.newdawn.slick.UnicodeFont;
    import org.newdawn.slick.font.effects.ColorEffect;
    import org.newdawn.slick.gui.ComponentListener;
    import org.newdawn.slick.gui.TextField;
    import org.newdawn.slick.state.BasicGameState;
    import org.newdawn.slick.state.StateBasedGame;
    
    public class Coop
            extends BasicGameState
    {
        TextField text;
        UnicodeFont font;
    
        public Coop(int state)
        {
        }
    
        public void init(GameContainer gc , StateBasedGame sbg)
                throws SlickException
        {
            font = getNewFont("Arial" , 16);
        }
    
        public void render(GameContainer gc , StateBasedGame sbg , Graphics g)
                throws SlickException
        {
            text.render(gc , g);
            g.setFont(font);
        }
    
        public void update(GameContainer gc , StateBasedGame sbg , int delta)
                throws SlickException
        {
            font.loadGlyphs();
        }
    
        public int getID()
        {
            return 3;
        }
    
        public void enter(GameContainer gc , StateBasedGame sbg)
                throws SlickException
        {
            text = new TextField(gc , font , 150 , 270 , 200 , 35);
        }
    
        public UnicodeFont getNewFont(String fontName , int fontSize)
        {
            font = new UnicodeFont(new Font(fontName , Font.PLAIN , fontSize));
            font.addGlyphs("@");
            font.getEffects().add(new ColorEffect(java.awt.Color.white));
            return (font);
        }
    }
    

    I don't know why it needs to use font.loadGlyphs(); but without it, it wont work.