Learn JMONKEYENGINE with Real Code Examples
Updated Nov 24, 2025
Code Sample Descriptions
1
jMonkeyEngine Simple Counter Example
import com.jme3.app.SimpleApplication;
import com.jme3.font.BitmapText;
public class CounterApp extends SimpleApplication {
private int count = 0;
private BitmapText counterText;
public static void main(String[] args) {
CounterApp app = new CounterApp();
app.start();
}
@Override
public void simpleInitApp() {
counterText = new BitmapText(guiFont, false);
counterText.setSize(guiFont.getCharSet().getRenderedSize());
counterText.setText("Count: " + count);
counterText.setLocalTranslation(300, counterText.getLineHeight(), 0);
guiNode.attachChild(counterText);
}
@Override
public void simpleUpdate(float tpf) {
count++;
counterText.setText("Count: " + count);
}
}
A minimal jMonkeyEngine application displaying a counter in a 3D scene and updating it each frame.
2
jMonkeyEngine Moving Box Example
import com.jme3.app.SimpleApplication;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
public class MovingBoxApp extends SimpleApplication {
private Geometry box;
public static void main(String[] args) {
new MovingBoxApp().start();
}
@Override
public void simpleInitApp() {
Box b = new Box(1,1,1);
box = new Geometry("Box", b);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
box.setMaterial(mat);
rootNode.attachChild(box);
}
@Override
public void simpleUpdate(float tpf) {
box.move(0, 0, 1 * tpf);
}
}
A simple box moving forward in the 3D scene.
3
jMonkeyEngine Rotating Cube Example
import com.jme3.app.SimpleApplication;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
public class RotatingCubeApp extends SimpleApplication {
private Geometry cube;
public static void main(String[] args) {
new RotatingCubeApp().start();
}
@Override
public void simpleInitApp() {
Box b = new Box(1,1,1);
cube = new Geometry("Cube", b);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Red);
cube.setMaterial(mat);
rootNode.attachChild(cube);
}
@Override
public void simpleUpdate(float tpf) {
cube.rotate(0, 1 * tpf, 0);
}
}
Rotates a cube around the Y axis every frame.
4
jMonkeyEngine Jumping Sphere Example
import com.jme3.app.SimpleApplication;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Sphere;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
public class JumpingSphereApp extends SimpleApplication {
private Geometry sphere;
private float vy = 0;
private float y = 1;
private float gravity = -9.8f;
public static void main(String[] args) {
new JumpingSphereApp().start();
}
@Override
public void simpleInitApp() {
Sphere s = new Sphere(16,16,1);
sphere = new Geometry("Sphere", s);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Green);
sphere.setMaterial(mat);
sphere.setLocalTranslation(0,y,0);
rootNode.attachChild(sphere);
}
@Override
public void simpleUpdate(float tpf) {
vy += gravity * tpf;
y += vy * tpf;
if (y < 1) { y = 1; vy = 5; } // bounce
sphere.setLocalTranslation(0, y, 0);
}
}
A sphere jumps up and falls down simulating gravity.
5
jMonkeyEngine Camera Follow Example
import com.jme3.app.SimpleApplication;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
public class CameraFollowApp extends SimpleApplication {
private Geometry box;
public static void main(String[] args) {
new CameraFollowApp().start();
}
@Override
public void simpleInitApp() {
Box b = new Box(1,1,1);
box = new Geometry("Box", b);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Yellow);
box.setMaterial(mat);
rootNode.attachChild(box);
}
@Override
public void simpleUpdate(float tpf) {
box.move(1 * tpf, 0, 0);
cam.setLocation(box.getLocalTranslation().add(0,5,10));
cam.lookAt(box.getLocalTranslation(), Vector3f.UNIT_Y);
}
}
Camera follows a moving object.
6
jMonkeyEngine Color Changing Box
import com.jme3.app.SimpleApplication;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
public class ColorBoxApp extends SimpleApplication {
private Geometry box;
private float time = 0;
public static void main(String[] args) {
new ColorBoxApp().start();
}
@Override
public void simpleInitApp() {
Box b = new Box(1,1,1);
box = new Geometry("Box", b);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
box.setMaterial(mat);
rootNode.attachChild(box);
}
@Override
public void simpleUpdate(float tpf) {
time += tpf;
box.getMaterial().setColor("Color", new ColorRGBA((float)Math.abs(Math.sin(time)), 0, 1-(float)Math.abs(Math.sin(time)), 1));
}
}
Box changes color every frame.
7
jMonkeyEngine Spinning Torus
import com.jme3.app.SimpleApplication;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Torus;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
public class SpinningTorusApp extends SimpleApplication {
private Geometry torus;
public static void main(String[] args) {
new SpinningTorusApp().start();
}
@Override
public void simpleInitApp() {
Torus t = new Torus(16,16,1,2);
torus = new Geometry("Torus", t);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Cyan);
torus.setMaterial(mat);
rootNode.attachChild(torus);
}
@Override
public void simpleUpdate(float tpf) {
torus.rotate(tpf, tpf, 0);
}
}
A torus spinning along multiple axes.
8
jMonkeyEngine Physics Falling Box
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
public class FallingBoxApp extends SimpleApplication {
private BulletAppState bulletAppState;
public static void main(String[] args) {
new FallingBoxApp().start();
}
@Override
public void simpleInitApp() {
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
Box b = new Box(1,1,1);
Geometry box = new Geometry("Box", b);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Magenta);
box.setMaterial(mat);
box.addControl(new RigidBodyControl(1f));
rootNode.attachChild(box);
bulletAppState.getPhysicsSpace().add(box);
}
}
Box falls under gravity using Bullet physics.
9
jMonkeyEngine Orbit Camera Example
import com.jme3.app.SimpleApplication;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Sphere;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
public class OrbitCameraApp extends SimpleApplication {
private float angle = 0;
public static void main(String[] args) {
new OrbitCameraApp().start();
}
@Override
public void simpleInitApp() {
Sphere s = new Sphere(16,16,1);
Geometry sphere = new Geometry("Sphere", s);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.White);
sphere.setMaterial(mat);
rootNode.attachChild(sphere);
}
@Override
public void simpleUpdate(float tpf) {
angle += tpf;
cam.setLocation(new Vector3f(10 * (float)Math.cos(angle), 5, 10 * (float)Math.sin(angle)));
cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
}
}
Camera orbits around a central object.