1) Hello World

( Note: the starter tutorials in this wiki are not always up to date with the latest development version of jME. You can find up to date source files for the tutorials here: https://jme.dev.java.net/source/browse/jme/src/jmetest/TutorialGuide/ )

Here we’ll learn the basics of creating a jME program, by exploring SimpleGame, Box, and rootNode. OK, let’s just dive in. Here’s as basic a program as you can get:

import com.jme.app.SimpleGame;
import com.jme.scene.shape.Box;
import com.jme.math.Vector3f;
/**
 * Started Date: Jul 20, 2004<br><br>
 * Simple HelloWorld program for jME
 *
 * @author Jack Lindamood
 */
public class HelloWorld extends SimpleGame{
 public static void main(String[] args) {
  HelloWorld app = new HelloWorld(); // Create Object
  // Signal to show properties dialog
  app.setDialogBehaviour(SimpleGame.ALWAYS_SHOW_PROPS_DIALOG);
  app.start(); // Start the program
 }
 protected void simpleInitGame() {
  // Make a box
  Box b = new Box("Mybox", new Vector3f(0,0,0), new Vector3f(1,1,1));
  rootNode.attachChild(b); // Put it in the scene graph
 }
}

Pretty short, right? The real meat of our program begins with the following:

public class HelloWorld extends SimpleGame{

SimpleGame does a lot of initialization for us behind our back. If you really want to, you can look at its code, but for now just understand that it creates all the basics needed for rendering. It's a great class to start with for prototyping and testing.

app.setDialogBehaviour(SimpleGame.ALWAYS_SHOW_PROPS_DIALOG);

You know the picture of a monkey you see when the program is first run, the one that lets you select the resolution?

Well, this command makes it appear. As the name suggests, it always shows the properties dialog on every run. You’ll never see that dialog box if you change it to the following:

SimpleGame.NEVER_SHOW_PROPS_DIALOG

Not too difficult.

app.start(); // Start the program

The function start() is a while loop. First, it initializes the jME system. Then, the while loop does two things per iteration: first, it tells everything in your game that it needs to move, and second, it renders everything. Basically, it gets the game going.

protected void simpleInitGame() {
 // Make a box
 Box b = new Box("Mybox",
 new Vector3f(0,0,0),
 new Vector3f(1,1,1));
 rootNode.attachChild(b); // Put it in the scene graph
}

The function simpleInitGame() is abstract in SimpleGame, so you’re forced to implement it every time you extend SimpleGame. Looking at the code we can see that two things happen. First, I make a box (it’s the thing you saw on the screen). Second, I attach the box to the root of my scene graph. The object rootNode is of class Node which is created by SimpleGame for you. You’ll attach everything to it or one of its children. Notice I gave b 3 parameters: a string and two Vector3f objects. Every Node, Box, Circle, Person or anything in your scene graph will have a name. Usually you want the name to be specific for each object. I called this one “My box”, but really you could have called it anything. The next two parameters specify the corners of the Box. It has one corner at the origin, and another at x=1, y=1, z=1. Basically, it’s a unit cube.

OK, I’ve created the Box, but I have to tell it I want it rendered, too. That’s why I attach it to the rootNode object. Your scene graph basically looks like this:

rootNode
My box

The object rootNode is at the top and “My box” is below it. So, when SimpleGame tries to draw rootNode it will try to draw “My box” as well. That’s it! Now, on to something more complex.

 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution 3.0 Unported