Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package scenes; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.GlyphLayout; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.circle.game.Config; import networking.GameClient; /** * * @author <a href="mailto:martin.drost@student.fontys.nl">Martin Dorst</a> */ public class SelectServer extends ApplicationAdapter { private Stage stage; //done private TextureAtlas atlas; //done private Skin skin; //done private Table table; // Align all buttons private TextButton buttonStart; private BitmapFont font60; private BitmapFont font25; private BitmapFont font30; private BitmapFont font30Fade; private GlyphLayout layout; private SpriteBatch batch; private String enteredName; private int numberOfChars; private int maxNumberOfChars; private FontFade fade; public SelectServer() { create(); } @Override public void create() { layout = new GlyphLayout(); batch = new SpriteBatch(); enteredName = Config.serverIp; Config.username = ""; numberOfChars = enteredName.length(); maxNumberOfChars = 10; //initialize fonts FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/MunroSmall.ttf")); FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter(); parameter.size = 60; font60 = generator.generateFont(parameter); // font size 12 pixels font60.setColor(Color.BLACK); parameter.size = 40; font30 = generator.generateFont(parameter); // font size 12 pixels font30.setColor(0.38f, 0.38f, 0.38f, 1f); font30Fade = generator.generateFont(parameter); // font size 12 pixels font30Fade.setColor(0.38f, 0.38f, 0.38f, 1f); parameter.size = 25; font25 = generator.generateFont(parameter); // font size 12 pixels font25.setColor(Color.BLACK); generator.dispose(); // don't forget to dispose to avoid memory leaks! fade = new FontFade(font30Fade); //prepare buttons stage = new Stage() { @Override public boolean keyDown(int keyCode) { if (keyCode == Input.Keys.ENTER) { setServerIP(); } return true; } @Override public boolean keyTyped(char character) { if (character == '\b' && enteredName.length() > 0) { enteredName = enteredName.substring(0, enteredName.length() - 1); } if (character == '\b' || character == '\t' || character == '\n' || character == '\r' || character == '\f' || character == '`' || character == '\u0000' || enteredName.length() >= 30) { return true; } enteredName += character; return true; } }; Gdx.input.setInputProcessor(stage); atlas = new TextureAtlas("ui/buttonPack.pack"); skin = new Skin(atlas); table = new Table(skin); table.setBounds(0, -150, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); final TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle(); textButtonStyle.downFontColor = new Color(0f, 0.57f, 0.92f, 1f); textButtonStyle.overFontColor = new Color(0f, 0.57f, 1f, 1f); textButtonStyle.font = font30; textButtonStyle.fontColor = Color.BLACK; buttonStart = new TextButton("Play", textButtonStyle); buttonStart.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { if (enteredName.length() == 0) return; setServerIP(); } }); buttonStart.pad(20); table.add(buttonStart); stage.addActor(table); } @Override public void render() { batch.begin(); //Write Circle layout.setText(font60, "Enter server IP"); int x = (int) (Gdx.graphics.getWidth() / 2 - layout.width / 2); int y = (int) (Gdx.graphics.getHeight() / 2 + 60); font60.draw(batch, "Enter server IP", x, y); //Write entered name layout.setText(font30, enteredName + "_"); x = (int) (Gdx.graphics.getWidth() / 2 - layout.width / 2); y = (int) (Gdx.graphics.getHeight() / 2 - 30); font30.draw(batch, enteredName, x, y); //Write fading text trailer layout.setText(font30, enteredName); x = (int) (x + layout.width); y = (int) (Gdx.graphics.getHeight() / 2 - 30); font30Fade.draw(batch, "_", x, y); fade.update(); batch.end(); stage.act(); stage.draw(); } public void setServerIP() { Config.serverIp = enteredName; GameClient.connect(); } }