Lesson 4: Writing Text

Return to the home page

The goal: (the scale factor was changed to 7 for ease of view on my resolution)

Click for full size. Hover over for more details.

Click here to download the code. (If the page opens in the broswer, you can right click on link and click "Save Link As", or right click on the page and click "Save As")

            
        // most applications will want to extend the SimpleGame class
        import com.jme.app.SimpleGame;
        
        // a vector containing three float numbers;
        //   used to store positions and translations
        import com.jme.math.Vector3f;
        
        // geometric shapes used in program
        import com.jme.scene.shape.Box;
        import com.jme.scene.shape.Sphere;
        
        // use predefined colors and create custom colors
        import com.jme.renderer.ColorRGBA;
        
        // work with basic materials
        import com.jme.scene.state.MaterialState;
        
        // process user input
        import com.jme.input.InputHandler;
        import com.jme.input.KeyInput;
        import com.jme.input.KeyBindingManager;
        import com.jme.input.FirstPersonHandler;
        
        // display text
        import com.jme.scene.Text;
        import com.jmex.font2d.Text2D;
        import com.jmex.font2d.Font2D;
        
        // an internal vertex of the scenegraph tree;
        //  use to group together objects,
        //  apply render settings, which affects all child vertices
        import com.jme.scene.Node;
        import com.jme.scene.Spatial;
        import com.jme.scene.Geometry;
        
        // methods to change render settings
        import com.jme.renderer.Renderer;
        
        /**
         *   To add text to a scene, we need to
         *   create a font object, a text object (using the font object),
         *   and a node, with customized renderer settings, for attaching text.
         */
        public class TextDemo extends SimpleGame
        {
           // a geometric shape that will be rendered
           Sphere mySphere;
        
           // create a font object, used to create a text object
           //  and a node to which we can attach the text object
           Font2D myFont;
           Text2D myText;
           Node textNode;
        
           public static void main(String[] args)
           {
               // create an instance of this program
               TextDemo app = new TextDemo();
        
               // before the main program starts, display the graphics configuration window
               //   where the user can choose among screen resolution, color depth, and full screen options
               app.setConfigShowMode(ConfigShowMode.AlwaysShow);
        
               // The start() method runs simpleInitGame(),
               //   then starts the game loop: simpleUpdate(), then simpleRender()
               // To terminate game loop, call finish()
               app.start();
           }
        
           protected void simpleInitGame()
           {
               // make the standard cursor visible.
               org.lwjgl.input.Mouse.setGrabbed(false);
               // SimpleGame class defaults mouse behavior to rotate view; here we disable this.
               ((FirstPersonHandler)input).getMouseLookHandler().setEnabled(false);
        
               // create a material to apply to geometric objects, use objects' default color,
               //  apply to front and back, and assign rendering to root node
               MaterialState customMaterial = display.getRenderer().createMaterialState();
               customMaterial.setColorMaterial(MaterialState.ColorMaterial.AmbientAndDiffuse);
               customMaterial.setMaterialFace(MaterialState.MaterialFace.FrontAndBack);
               rootNode.setRenderState(customMaterial);
        
               // Parameters for the Sphere constructor:
               //   Sphere( String name, Vector3f center, int zSamples, int radialSamples, float radius )
               mySphere = new Sphere("s", new Vector3f(0,0,0), 30, 30, 2 );
               mySphere.setDefaultColor( ColorRGBA.gray );
               rootNode.attachChild( mySphere );
        
               // assign (bind) strings to keys
               KeyBindingManager.getKeyBindingManager().set( "sphereRed", KeyInput.KEY_1 );
               KeyBindingManager.getKeyBindingManager().set( "sphereGreen", KeyInput.KEY_2 );
               KeyBindingManager.getKeyBindingManager().set( "sphereBlue", KeyInput.KEY_3 );
        
               // create default font.
               //   optional parameter: bitmap font filename.
               myFont = new Font2D();
               // create a text object using previously created font;
               //  numeric parameters are meaningless (not implemented)
               //  default font size is always 12 point, plain style
               myText = myFont.createText("Hello World!", 12, 0);
               // set location of text bottom-left corner: x,y,0 (pixels)
               myText.setLocalTranslation( 100, 20, 0 );
               // set text scale 
               myText.setLocalScale( 1 );
               // set color for text
               myText.setTextColor( ColorRGBA.yellow );
        
               // set up Node to hold text object
               textNode = new Node();
               // queue: order in which items are rendered.
               // QUEUE_ORTHO: drawn last, so it appears on top.
               textNode.setRenderQueueMode(Renderer.QUEUE_ORTHO);
               // turn off lighting effects for this object
               textNode.setLightCombineMode(Spatial.LightCombineMode.Off);
        
               // attach nodes to scenegraph so that they are rendered
               textNode.attachChild( myText );
               rootNode.attachChild( textNode );
           }
        
           protected void simpleUpdate()
           {
               // process input entered since last update.
               //  boolean parameter:
               //  true - activate continuously when key is down;
               //  false - activate only once per key press
               if (KeyBindingManager.getKeyBindingManager().isValidCommand("sphereRed",  false))
               {
                   mySphere.setDefaultColor( ColorRGBA.red );
                   // call updateRenderState() whenever appearance of an object changes
                   mySphere.updateRenderState();
                   myText.setText("The Sphere is Red!");
                   myText.setTextColor( ColorRGBA.red );
               }
               if (KeyBindingManager.getKeyBindingManager().isValidCommand("sphereGreen", false))
               {
                   mySphere.setDefaultColor( ColorRGBA.green );
                   mySphere.updateRenderState();
                   myText.setText("The Sphere is Green!");
                   myText.setTextColor( ColorRGBA.green );
               }
               if (KeyBindingManager.getKeyBindingManager().isValidCommand("sphereBlue",  false))
               {
                   mySphere.setDefaultColor( ColorRGBA.blue );
                   mySphere.updateRenderState();
                   myText.setText("The Sphere is Blue!");
                   myText.setTextColor( ColorRGBA.blue );
               }
           }
        
           protected void simpleRender()
           {
               // nothing to add
           }
        }

     

Return to the home page