Aug 29, 2014

35. Images

Besides using images for textures, fonts, etc., they can be used as part of 2D GUI.




By searching for jMonkeyEngine, we can get this Wikipedia webpage with the logo at right. Right-click to save the file.




This is the file downloaded. In the project, create a subfolder in Interface folder, such as jmonkey_logo. The file should be saved there.




In the simpleInitApp(), create a Picture object, such as logo. You next have to point it to the Interface image file using the assetManager. The width and height should be set as well as the position it should be moved to. Finally, it should be attached to the guiNode.


        // *** 1. Start (image)
        logo = new Picture("The Monkey Engine");
        logo.setImage(assetManager, 
                "Interface/jmonkey_logo/Jmonkeyengine-logo.png",
                true); // true - use alpha
        x = settings.getWidth()/2-75;
        y = settings.getHeight()/2-60;
        logo.move(x,y,0);
        logo.setWidth(150); // image width
        logo.setHeight(120); // image height
        guiNode.attachChild(logo);
        // *** 1. End



package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.math.ColorRGBA;
import com.jme3.system.AppSettings;
import com.jme3.ui.Picture;

public class JMonkey35 extends SimpleApplication {

    public static void main(String[] args) {
        AppSettings cfg = new AppSettings(true);
        cfg.setFrameRate(60);
        JMonkey35 app = new JMonkey35();
        app.setSettings(cfg);
        app.start();
    }
    
    private Picture logo;
    private int x,y;

    @Override
    public void simpleInitApp() {
        flyCam.setEnabled(false);
        viewPort.setBackgroundColor(ColorRGBA.White);
        setDisplayStatView(false);
        setDisplayFps(false);
        // *** 1. Start (image)
        logo = new Picture("The Monkey Engine");
        logo.setImage(assetManager, 
                "Interface/jmonkey_logo/Jmonkeyengine-logo.png",
                true); // true - use alpha
        x = settings.getWidth()/2-75;
        y = settings.getHeight()/2-60;
        logo.move(x,y,0);
        logo.setWidth(150); // image width
        logo.setHeight(120); // image height
        guiNode.attachChild(logo);
        // *** 1. End
    }
    
    @Override
    public void simpleUpdate(float tps) {
        System.out.println("rate = " + 1/tps);
    }
}



No comments:

Post a Comment