Bitmap Fonts can be created using a Plugin in the jMonkeyEngine SDK, and we can use them as 2D objects.
We can check plugins from the Tools menu. If the Font Creator plugin is not already installed, install it from the Available Plugins tab, and restart the SDK.
In a project, right-click on the Interface folder in the Assets, and select new Font file. Then, any system font can be installed. You can also get free fonts from some websites.
In the next step, you have to set some settings such as image size, font size and padding.
After these steps, two files be in a Fonts folder.
To use the fonts, first load the file into BitmapFont object. Then you can create BitmapText objects and attach them to guiNode.
// *** 1. Start (Load font, initial text) BitmapFont myFont = assetManager.loadFont( "Interface/Fonts/TrebuchetMS.fnt"); outText = new BitmapText(myFont, false); outText.setSize(guiFont.getCharSet().getRenderedSize()); outText.move(settings.getWidth()/5, // X settings.getHeight()/2, // Y 0); // Z outText.setText("This is test # " + count); outText.setColor(ColorRGBA.Blue); outText.setLocalScale(2); guiNode.attachChild(outText); // *** 1. End
In the simpleUpdate(), the counter is incremented and new text is written.
// *** 2. Start (Updated text) outText.setText("This is a test # " + (++count)); // *** 2. End
package mygame; import com.jme3.app.SimpleApplication; import com.jme3.font.BitmapFont; import com.jme3.font.BitmapText; import com.jme3.math.ColorRGBA; import com.jme3.system.AppSettings; public class JMonkey34 extends SimpleApplication { public static void main(String[] args) { AppSettings cfg = new AppSettings(true); cfg.setFrameRate(2); // 2 frames per second JMonkey34 app = new JMonkey34(); app.setSettings(cfg); app.start(); } private int count = 0; private BitmapText outText = null; @Override public void simpleInitApp() { flyCam.setEnabled(false); viewPort.setBackgroundColor(ColorRGBA.LightGray); setDisplayStatView(false); setDisplayFps(false); // *** 1. Start (Load font, initial text) BitmapFont myFont = assetManager.loadFont( "Interface/Fonts/TrebuchetMS.fnt"); outText = new BitmapText(myFont, false); outText.setSize(guiFont.getCharSet().getRenderedSize()); outText.move(settings.getWidth()/5, // X settings.getHeight()/2, // Y 0); // Z outText.setText("This is test # " + count); outText.setColor(ColorRGBA.Blue); outText.setLocalScale(2); guiNode.attachChild(outText); // *** 1. End } @Override public void simpleUpdate(float tps) { // *** 2. Start (Updated text) outText.setText("This is a test # " + (++count)); // *** 2. End } }
Output:
No comments:
Post a Comment