# Integrating Nifty GUI: Projection

1. Nifty GUI XML Layout
2. Nifty GUI Overlay or Nifty GUI Projection
3. Interact with the GUI from Java

Define a key (for example escape) that switches the GUI on and off. You can either overlay the running game with the GUI (you will most likely pause the game then), or even project it as a texture onto a mesh textures (but then you cannot click to select).

On this page, we look at the projection variant (less commonly used).

## Sample Code

## Projecting the User Interface Onto a Texture

You can project the Nifty GUI onto a texture, load the texture into a material, and assign it to a 3D mesh. Allthough this is possible the approach is rarely used since it is difficult to record clicks this way, you can only interact with this UI by keyboard or programmatically. (e.g. an action performed by a character standing in front of a “computer”)

    /** Create a new viewport for the GUI */
    ViewPort niftyView = renderManager.createPreView("NiftyView", new Camera(1024, 768));
    niftyView.setClearEnabled(true);

    /** Create a new NiftyJmeDisplay for the integration */
    NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(
      assetManager,  inputManager,  audioRenderer,  niftyView);

    /** Create a new NiftyGUI object and read your XML */
    Nifty nifty = niftyDisplay.getNifty();
    nifty.fromXml("Interface/helloworld.xml", "start");

    /** We prepare a framebuffer for the texture niftytex */
    niftyView.addProcessor(niftyDisplay);
    FrameBuffer fb = new FrameBuffer(1024, 768, 0);
    fb.setDepthBuffer(Format.Depth);
    Texture2D niftytex = new Texture2D(1024, 768, Format.RGB8);
    fb.setColorTexture(niftytex);
    niftyView.setClearEnabled(true);
    niftyView.setOutputFrameBuffer(fb);

    /** This is the 3D cube we project the GUI on */
    Box(Vector3f.ZERO, 1, 1, 1);
    Geometry geom = new Geometry("Box", b);
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/SimpleTextured.j3md");
    mat.setTexture("m_ColorMap", niftytex); /** Here comes the texture! */
    geom.setMaterial(mat);
    rootNode.attachChild(geom);

You select buttons on this GUI with the arrow keys and then press return – Clicking them will not work.

Again, check the
to get all the “bells and whistles”!

1. Nifty GUI XML Layout
2. Nifty GUI Overlay or Nifty GUI Projection
3. Interact with the GUI from Java

documentation, gui, texture

view online version