So, Iam making a 2D GAME for a final project for school and in the main menu I wanted the user to input his name before playing the game but whenever I try to do a scanner the game freezes, or If I try the Jpanel the GUI is kinda infinitely popping up thus making the game unplayable. Anything I can do? BTW Iam only a beginner in java and got this code only on tutorials by RyiSnow. Thank you
I am planning to put the input name part into.
else if (gp.ui.titleScreenState == 3)

package main;
import javax.swing.*;
import java.awt.*;
import java.awt.Font;
import java.awt.Graphics2D;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
public class UI {
GamePanel gp;
Graphics2D g2;
Font arial_40, arial_80B;
public boolean messageOn = false;
public String message = "";
int messageCounter = 0;
public boolean gameFinished = false;
public int commandNum = 0;
public int titleScreenState = 0;
String playerName = "";
double playTime;
DecimalFormat decimalFormat = new DecimalFormat("#0.00");
public UI(GamePanel gp) {
this.gp = gp;
arial_40 = new Font("Arial", Font.PLAIN, 40);
arial_80B = new Font("Arial", Font.BOLD, 80);
}
public void showMessage(String text) {
message = text;
messageOn = true;
}
public void draw(Graphics2D g2) {
this.g2 = g2;
g2.setFont(arial_40);
g2.setColor(Color.white);
//TITLE STATE
if(gp.gameState == gp.titleState) {
drawTitleScreen();
}
//PLAY STATE
if (gp.gameState == gp.playState) {
//latur
}//PAUSE STATE
if (gp.gameState == gp.pauseState) {
drawPauseScreeen();
}
}
public
void drawTitleScreen(){
if (titleScreenState == 0){
g2.setColor(new Color(0, 0, 0));
g2.fillRect(0,0, gp.screenWidth, gp.screenHeight);
//TITLE NAME
g2.setFont(g2.getFont().deriveFont(Font.BOLD,96F));
String text = "Byte Boulevard";
int x = getXforCenteredText(text);
int y = gp.tileSize*3;
//SHADOW
g2.setColor(Color.gray);
g2.drawString(text, x+5, y+5);
//MAIN COLOR
g2.setColor(Color.white);
g2.drawString(text, x, y);
/* IMAGE
x = gp.screenHeight/2 - (gp.tileSize*2)/2;
y += gp.tileSize*2;
g2.drawImage(gp.player.down1, x, y, gp.tileSize*2, gp.tileSize*2, null);
*/
// MENU
g2.setFont(g2.getFont().deriveFont(Font.BOLD,48F));
text = "NEW GAME";
x = getXforCenteredText(text);
y += gp.tileSize*2;
g2.drawString(text, x, y);
if (commandNum == 0){
g2.drawString("►", x-gp.tileSize, y);
}
text = "LOAD GAME";
x = getXforCenteredText(text);
y += gp.tileSize;
g2.drawString(text, x, y);
if (commandNum == 1){
g2.drawString("►", x-gp.tileSize, y);
}
text = "OPTIONS";
x = getXforCenteredText(text);
y += gp.tileSize;
g2.drawString(text, x, y);
if (commandNum == 2){
g2.drawString("►", x-gp.tileSize, y);
}
text = "ABOUT";
x = getXforCenteredText(text);
y += gp.tileSize;
g2.drawString(text, x, y);
if (commandNum == 3){
g2.drawString("►", x-gp.tileSize, y);
}
text = "QUIT";
x = getXforCenteredText(text);
y += gp.tileSize;
g2.drawString(text, x, y);
if (commandNum == 4){
g2.drawString("►", x-gp.tileSize, y);
}
}
else if (titleScreenState == 1){
//intro
g2.setColor(Color.white);
g2.setFont(g2.getFont().deriveFont(42F));
String text = "Welcome! This Game is still under development and ";
int x = getXforCenteredText(text);
int y = gp.tileSize*4;
g2.drawString(text, x, y);
text = "It is still in its early phase, that's why with utmost respect";
x = getXforCenteredText(text);
y += gp.tileSize;
g2.drawString(text, x, y);
text = "Please expect little content within the game";
x = getXforCenteredText(text);
y += gp.tileSize;
g2.drawString(text, x, y);
text = "Press [ENTER] to CONTINUE...";
x = getXforCenteredText(text);
y += gp.tileSize*4;
g2.drawString(text, x, y);
} else if (titleScreenState == 2) {
String text = "While our team is currently doing their best in developing";
int x = getXforCenteredText(text);
int y = gp.tileSize * 4;
g2.drawString(text, x, y);
text = "new contents in to the game, We kindly ask of you";
x = getXforCenteredText(text);
y += gp.tileSize;
g2.drawString(text, x, y);
text = "to wait in future additions into the game.";
x = getXforCenteredText(text);
y += gp.tileSize;
g2.drawString(text, x, y);
text = "Enjoy the early phase of our game";
x = getXforCenteredText(text);
y += gp.tileSize;
g2.drawString(text, x, y);
text = "Press [ENTER] to CONTINUE...";
x = getXforCenteredText(text);
y += gp.tileSize * 3;
g2.drawString(text, x, y);
}
else if (gp.ui.titleScreenState == 3){
String text = "Please input the name of your character: ";
int x = getXforCenteredText(text);
int y = gp.tileSize * 4;
g2.drawString(text, x, y);
}
}
public void drawPauseScreeen (){
String text = "PAUSED";
int x = getXforCenteredText(text);
int y = gp.screenHeight/2;
g2.drawString(text, x, y);
//
}
public int getXforCenteredText(String text){
int length = (int) g2.getFontMetrics().getStringBounds(text, g2).getWidth();
int x = gp.screenWidth/2 - length/2;
return x;
}
}
Tried to use scanner but didn't work, tried to use jpanel but didn't work, tried to use bufferedreader but didnt work also