1

I'm trying to make a simple game using the slick2D library, with the user having to input their name on text box of some sort. I searched on google to find tutorials on how to get user input, the only thing I got was that one can check if a specified key is pressed. I also tried using the built in TextField but it didn't seem to work inside a BasicGameState class. Other than that I could not find any sort of tutorials to get user input. Checking for every single key seems rather inefficient. Ideally I would like to check if the user hits a key and whatever that key is I want to add it to a string variable which might be later used. the code i tried for the built in TextField is as follows package scenes;

import static javagame.Main.*;

import org.newdawn.slick.*;
import org.newdawn.slick.gui.TextField;
import org.newdawn.slick.state.*;

public class Scene01 extends BasicGameState{

    public static void drawBG(Graphics g){
        g.setColor(new Color(0, 0, 0, 255));
        g.fillRect(0, 0, displayWidth, displayHeight);
        g.setColor(new Color(255, 255, 255, 2));
        g.fillRect(0, 0, displayWidth, displayHeight);
        int circleXY, circleWH;
        for(int i = 0; i < 100; i++){
            circleWH = 500 - (i * 5);
            circleXY = (int) (-50 + (i * 2.5));
            g.setColor(new Color(255, 255, 255, 1));
            g.fillOval(circleXY , circleXY, circleWH, circleWH);
        }
    }

    public  String inputText;
    private boolean active = false;
    private Color activeColor = new Color(0, 255, 255);
    private Color inactiveColor = new Color(0, 128, 128);
    private Color colorRn = inactiveColor;
    private TextField textField;

    String str;

    public Scene01(int state){}

    public void init(GameContainer gc, StateBasedGame sbg)throws SlickException {
        textField = new TextField(gc, gc.getDefaultFont(), 240, 240, 100, 100);
        textField.setBackgroundColor(colorRn);
        textField.setBorderColor(Color.black);
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException {

    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g)throws SlickException {
        g.setColor(Color.white);
        g.fillRect(0, 0, displayWidth, displayHeight);
        //drawBG(g);
        textField.render(gc, g);
    }

    public int getID() {
        return 2;
    }

}

1 Answers1

-1

Here's a way to open a small panel that shows a message of your preference and a box in which the user can input the answer(im also on my phone atm so excuse any mistakes) :

First put this in the imports (before every public class) :

import javax.swing.JOptionPane;

Then type this in your program:

String X;
Int Y;

X=JOptionPane.showInputDialog("Enter your name");
Y=Integer.parseInt(s);

~Hope this helped!

  • i got it working thanks a lot i'll look into customizing it to make it look a little better than what it is atm thanks anyways :) – Huzaif Ahmed Apr 02 '18 at 11:09