Although it is possible to embed a jME3 canvas in a Swing GUI app, a 3D game typically runs full-screen, or in a window of its own. This soon raises the question of how to add a user interface: Most games respond to the escape key by displaying buttons that allow users to switch to different screens – for example to view high scores, customize settings, or load saved games.
This doc introduces you to Nifty GUI, a Java library for graphical user interfaces (GUIs). Nifty GUI (de.lessvoid.nifty
package) is well integrated with jME3 via the com.jme3.niftygui
package. You define the GUI layout in XML and call it from your Java code. All the JAR libraries that you need are already included in your jME3 download, so you do not need to install anything extra (Just make sure they are on the classpath).
Typically, you lay out the Nifty GUI using XML, but using Java will soon be a second option.
There are three steps needed to add a GUI to your jME3 game:
If you include the following XML schema in the first lines of your NiftyGUI XML files, your IDE will give you helpful hints and code completion.
<?xml version="1.0" encoding="UTF-8"?> <nifty xmlns="http://nifty-gui.sourceforge.net/nifty.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://nifty-gui.sourceforge.net/nifty.xsd http://nifty-gui.sourceforge.net/nifty.xsd"> <!-- Example: The IDE will now tell you that one <screen></screen> element is expected here, etc. --> </nifty>
Learn more from the NiftyGUI page!