Label Class

Though there was no real need for providing a Label class, as the Element class is a label (as well as everything else), one is provided for two reasons:

  • 1. Provide the 3 common constructors

  • 2. Use of styles to allow for global settings for all text using the Label class.

Style information can be overridden by any instance by using the Text/Font setters provided by the Element class


Alternatively, the LabelElement class can be used to create a label, if deemed more convenient.

Here is an example of how the Label class (or LabelElement class) can be used:

The following Gui class can be instantiated and called from the initialize() method of an appState [preferred], or the simpleInitApp() method of the app class itself [which extends SimpleApplication], like this:

  @Override
  public void simpleInitApp() {
      gui = new Gui(this);
      guiNode.addControl(gui.getScreen());
      gui.setText1("Rotate to 0 degrees.");
      gui.setText2("Rotating clockwise.");
  }

Below is the code that creates the label(s):

public class Gui {

    private Screen screen;
    //private LabelElement label1, label2;
    private Label label1, label2;
    private Panel background;

    public Gui(App app){
        screen = new Screen(app);
        float width  = 300f;
        float height = 100f;

        //label1 = new LabelElement(
        label1 = new Label(
                screen,                           // Screen
                "text1",                          // ID
                new Vector2f(0f, height / 10f),   // position
                new Vector2f(width, height / 2),  // size
                new Vector4f(0f, 0f, 0f, 0f),     // resize borders
                null);                            // image

        //label2 = new LabelElement(
        label2 = new Label(
                screen,                             // Screen
                "text2",                            // ID
                new Vector2f(0f, height / 2 - 10f), // position
                new Vector2f(width, height / 2),    // size
                new Vector4f(0f, 0f, 0f, 0f),       // resize borders
                null);                              // image

        background = new Panel(
                screen,                                       // Screen
                "background",                                 // ID
                new Vector2f(20f, 20f),                       // position
                new Vector2f(width, height),                  // size
                new Vector4f(10f, 10f, 10f, 10f),             // resize borders
                "tonegod/gui/style/def/Window/panel_x.png");  // image


//        AssetManager assetManager = app.getAssetManager();
//        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
//        mat.setColor("Color", ColorRGBA.Gray);
//        background.setMaterial(mat);

        label1.setText("First Text");
        label1.setFontColor(ColorRGBA.White);
        label1.setTextAlign(BitmapFont.Align.Center);

        label2.setText("Second Text");
        label2.setFontColor(ColorRGBA.White);
        label2.setTextAlign(BitmapFont.Align.Center);

        background.addChild(label1);
        background.addChild(label2);
        screen.addElement(background);

    }

    public Screen getScreen(){
        return screen;
    }

    public void setText1(String text1){
        label1.setText(text1);
        //label1.show();
    }

    public void setText2(String text1){
        label2.setText(text1);
        //label2.show();
    }
}

Troubleshooting:

If your labels don’t show up, make sure that the ID string for each of them is different. For labels that have the same ID String, only the first one will show up. Consequent ones will not be added to the parent, and … as far as I know … it doesn’t throw an exception. Having the same ID Strings between labels, or any other element, can come from copying and pasting constructors.

… been there … done that … .