Previous: Installing JME3, Next: Hello Node
Prerequisites: This tutorial assumes that you have .
In this tutorial series, we assume that you use the jMonkeyEngine SDK. As an intermediate or advanced Java developer, you will quickly see that, in general, you can develop jMonkeyEngine code in any integrated development environment (NetBeans IDE, Eclipse, IntelliJ) or even from the command line.
OK, let's get ready to create our first jMonkeyEngine3 application.
In the jMonkeyEngine SDK:
jMonkeyProjects
directory in your home directory.If you have questions, read more about Project Creation here.
jme3test.helloworld
samples (and many others). For example, you can use the JmeTests project to verify whether you got the solution right.
For this tutorial, you want to create a jme3test.helloworld
package in your project, and create a file HelloJME3.java
in it.
In the jMonkeyEngine SDK:
HelloJME3
jme3test.helloworld
. The SDK creates the file HelloJME3.java for you.
Replace the contents of the HelloJME3.java file with the following code:
package jme3test.helloworld;
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.math.ColorRGBA;
/** Sample 1 - how to get started with the most simple JME 3 application.
* Display a blue 3D cube and view from all sides by
* moving the mouse and pressing the WASD keys. */
public class HelloJME3 extends SimpleApplication {
public static void main(String[] args){
HelloJME3 app = new HelloJME3();
app.start(); // start the game
}
@Override
public void simpleInitApp() {
Box b = new Box(Vector3f.ZERO, 1, 1, 1); // create cube shape at the origin
Geometry geom = new Geometry("Box", b); // create cube geometry from the shape
Material mat = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md"); // create a simple material
mat.setColor("Color", ColorRGBA.Blue); // set color of material to blue
geom.setMaterial(mat); // set the cube's material
rootNode.attachChild(geom); // make the cube appear in the scene
}
}
Right-click the HelloJME3 class and choose Run. If a jME3 settings dialog pops up, confirm the default settings.
Congratulations! Now let's find out how it works!
The code above has initialized the scene, and started the application.
Look at the first line. The HelloJME3.java class extends com.jme3.app.SimpleApplication
.
public class HelloJME3 extends SimpleApplication { // your code... }
Every JME3 game is an instance of com.jme3.app.SimpleApplication
. The SimpleApplication class manages your 3D scene graph and automatically draws it to the screen ??? that is, in short, what a game engine does for you!
You start every JME3 game from the main() method, as every standard Java application:
SimpleApplication
-based classstart()
method to start the game engine. public static void main(String[] args){ HelloJME3 app = new HelloJME3(); // instantiate the game app.start(); // start the game! }
This code opens your application window. Let's learn how you put something into the window next.
What you want to do | How you say that in JME3 terminology |
---|---|
You want to create a cube. | I create a Geometry with a 1x1x1 Box shape. |
You want to use a blue color. | I create a Material with a blue Color property. |
You want to colorize the cube blue. | I set the Material of the Box Geometry. |
You want to add the cube to the scene. | I attach the Box Geometry to the rootNode. |
You want the cube to appear in the center. | I create the Box at the origin = at Vector3f.ZERO . |
If you are unfamiliar with the vocabulary, read more about the Scene Graph here.
Look at rest of the code sample. The simpleInitApp()
method is automatically called once at the beginning when the application starts. Every JME3 game must have this method. In the simpleInitApp()
method, you load game objects before the game starts.
public void simpleInitApp() { // your initialization code... }
The initialization code of a blue cube looks as follows:
public void simpleInitApp() { Box b = new Box(Vector3f.ZERO, 1, 1, 1); // create a 1x1x1 box shape at the origin Geometry geom = new Geometry("Box", b); // create a cube geometry from the box shape Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); // create a simple material mat.setColor("Color", ColorRGBA.Blue); // set color of material to blue geom.setMaterial(mat); // set the cube geometry 's material rootNode.attachChild(geom); // make the cube geometry appear in the scene }
A typical JME3 game has the following initialization process:
rootNode
.score
to 0, set health
to 100%, ???You have learned that a SimpleApplication is a good starting point because it provides you with:
simpleInitApp()
method where you create objects.rootNode
where you attach objects to make them appear in the scene.When developing a game application, you want to:
The now following tutorials teach how you accomplish these tasks with the jMonkeyEngine 3.
Continue with the Hello Node tutorial, where you learn more details about how to initialize the game world, also known as the scene graph.
See also: