Slick TextField not working

I have a problem when using Slick2D TextField.

Using Slick 'BasicGame' TextField works fine - I can click on it, type the words and System.out.println the contents of the text fields.

However, when using the same code in "BasicGameState", TextField does not require a click and does not respond to any input.

So: Working code:

package Help;
import java.awt.Font;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
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.AbstractComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.gui.TextField;

public class TextFieldWorking extends BasicGame{
    private TextField text1;
    private UnicodeFont font = getNewFont("Arial" , 16);


    public TextFieldWorking() {
        super("Test");
    }
    @Override
    public void init(GameContainer container) throws SlickException {
        font.loadGlyphs();
        text1 = new TextField(container, font, 50,50,100,25);
    }
    @Override
    public void update(GameContainer gc, int delta) throws SlickException {
        System.out.println(text1.getText());
    }
    @Override
    public void render(GameContainer gc, Graphics g) throws SlickException {
        text1.render(gc, g);
    }
    public UnicodeFont getNewFont(String fontName , int fontSize){
        UnicodeFont returnFont = new UnicodeFont(new Font(fontName , Font.PLAIN , fontSize));
        returnFont.addAsciiGlyphs();
        returnFont.getEffects().add(new ColorEffect(java.awt.Color.white));
        return (returnFont);
    }
    public static void main(String[] args) throws SlickException  {
        TextFieldWorking g = new TextFieldWorking();
        AppGameContainer gc = new AppGameContainer(g);
        gc.setDisplayMode(500, 500, false);
        gc.start();
    }
}

Inoperative code:

package Help;
import java.awt.Font;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
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.AbstractComponent;
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 TextFieldTest extends BasicGameState{

    int WindowWidth, WindowHeight;  
    TextField text1;
    private UnicodeFont font = getNewFont("Arial" , 16);
    private int stateId = 0;

    public TextFieldTest(int State) {
        this.stateId = State;
    }

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{

        WindowWidth = gc.getWidth();
        WindowHeight = gc.getHeight();
        font.loadGlyphs();
        text1 = new TextField(gc, font, 50,50,100,25);
    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
        text1.render(gc, g);
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
        System.out.println(text1.getText());
    }

    public UnicodeFont getNewFont(String fontName , int fontSize){
        UnicodeFont returnFont = new UnicodeFont(new Font(fontName , Font.PLAIN , fontSize));
        returnFont.addAsciiGlyphs();
        returnFont.getEffects().add(new ColorEffect(java.awt.Color.white));
        return (returnFont);
    }

    public int getID(){
        return this.stateId; //Returns the Id of this state (menu is 0)
    }
}

For convenience:

This is due to BasicGameState - how Slick2D works - you can just copy - paste the code now: P

package Help;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

public class Game extends StateBasedGame{

    public static final String gamename = "Problem!";
    public static final int TextFieldTestNum = 1;

    public Game(String gamename){
        super(gamename); 
        this.addState(new TextFieldTest(TextFieldTestNum));

    }
    public void initStatesList(GameContainer gc) throws SlickException{
        this.getState(TextFieldTestNum).init(gc, this);
        this.enterState(TextFieldTestNum); //The First state to enter
    }

    @Override
    public boolean closeRequested(){
        System.exit(0);
        return false;
    }

    public static void main(String[] args) {
        AppGameContainer appgc;
        try{
            Game g = new Game(gamename);
            appgc = new AppGameContainer(g);
            appgc.setDisplayMode(500, 500, false);
            appgc.start();
        }catch(SlickException e){
            e.printStackTrace();
        }
    }
}

Any ideas appreciated!

+4
source share
1 answer

, StateBasedGames slick. : http://slick.ninjacave.com/wiki/index.php?title=Game_States

, initStatesList() this.addState(new StateName()). , addState(). init , getState() enterState() (Game.java ).

, , initStatesList() this.addState( TextFieldTest (TextFieldTestNum)); initStatesList().

, .

+2

Source: https://habr.com/ru/post/1525069/


All Articles