Splitting up project into multiple classes

I’m trying to better understand what Java is all about and am also trying to experiment with the jmonkey engine. It’s a great engine and fun to use, but say I want to split up my project into multiple classes so that each class does a different job, I can’t quite understand how that would work.

For example, say I have 3 classes; hud, player and level
And then a 4th class, which is the main class, where I create instances of all 3 classes above

The hud class displays all of the guiNode stuff
The player class creates and manages all of the events that are related to the player
The level class basically initializes and displays the level and its scenery (all of the trees, boxes, balls, other objects, etc.)

Would I need to have to initialize a SimpleApplication in all three classes? Because I’m trying to make a hud class that only deals with guiNode stuff, but I’m not exaclty sure how to proceed.

Well you can just pass it as a parameter in the oter classes constructor. I suggest to do some basic java tutorial/ read some book then it will become much more clear.

if someone could bear with me on this, I’d appreciate it.

I have a class called hud.java,

[java]
import com.jme3.app.SimpleApplication;
import com.jme3.font.BitmapText;

public class hud extends SimpleApplication {

public hud() {
}

public void initHUD(main app) {
    app.guiNode.detachAllChildren();
    guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
    BitmapText ch = new BitmapText(guiFont, false);
    ch.setSize(guiFont.getCharSet().getRenderedSize() * 2);
    ch.setText("+"); // crosshairs
    ch.setLocalTranslation( // center
            settings.getWidth() / 2 - guiFont.getCharSet().getRenderedSize() / 3 * 2,
            settings.getHeight() / 2 + ch.getLineHeight() / 2, 0);
    guiNode.attachChild(ch);
}

@Override
public void simpleInitApp() {
}

}
[/java]

And then my main.java:

[java]
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.material.Material;
import com.jme3.math.FastMath;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.texture.Texture;

class main extends SimpleApplication {

private static main app;
private BulletAppState bulletAppState;
private RigidBodyControl floorPhy;


public static void main(String[] args)  {
    app = new main();
    app.start();
    
}

@Override
public void simpleInitApp() {
    
    //Initialize physics space
    bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);
    
    // Create the floor
    Box floorBox = new Box(Vector3f.ZERO, 30, 0, 30);
    floorBox.scaleTextureCoordinates(new Vector2f(22,22));
    Geometry floorGeom = new Geometry("Floor", floorBox);
    Material floorMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    Texture floorTex = assetManager.loadTexture("floor.png");
    floorTex.setWrap(Texture.WrapMode.Repeat);
    floorMat.setTexture("ColorMap", floorTex);
    floorGeom.setMaterial(floorMat);
    rootNode.attachChild(floorGeom);
    
    floorPhy = new RigidBodyControl(0.0f);
    floorGeom.addControl(floorPhy);
    bulletAppState.getPhysicsSpace().add(floorPhy);
    
    
    // Create the ceiling
    Box ceilBox = new Box(Vector3f.ZERO, 10, 0, 10);
    ceilBox.scaleTextureCoordinates(new Vector2f(15,15));
    Geometry ceilGeom = new Geometry("Roof", ceilBox);
    Material ceilMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    Texture ceilTex = assetManager.loadTexture("ceiling.png");
    ceilTex.setWrap(Texture.WrapMode.Repeat);
    ceilMat.setTexture("ColorMap", ceilTex);
    ceilGeom.setMaterial(ceilMat);
    ceilGeom.setLocalTranslation(new Vector3f(0,5,0));
    rootNode.attachChild(ceilGeom);
    
    // Create the (4) walls
    Box wallBox = new Box(Vector3f.ZERO, 2, 2.5f, 0);
    Geometry[] wall = new Geometry[16];
    Material wallMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    Texture wallTex = assetManager.loadTexture("wall.png");
    wallTex.setWrap(Texture.WrapMode.Repeat);
    wallMat.setTexture("ColorMap", wallTex);
    wall[0] = new Geometry("Wall 1", wallBox);
    wall[0].setMaterial(wallMat);
    wall[0].setLocalTranslation(0,2.5f,10);
    rootNode.attachChild(wall[0]);
    
    wall[1] = new Geometry("Wall 2", wallBox);
    wall[1].setMaterial(wall[0].getMaterial());
    wall[1].setLocalTranslation(8,2.5f,10);
    rootNode.attachChild(wall[1]);
    
    wall[2] = new Geometry("Wall 3", wallBox);
    wall[2].setMaterial(wall[0].getMaterial());
    wall[2].rotate(0f, FastMath.DEG_TO_RAD * 90, 0f);
    wall[2].setLocalTranslation(10,2.5f,8);
    rootNode.attachChild(wall[2]);
    
    wall[3] = new Geometry("Wall 4", wallBox);
    wall[3].setMaterial(wall[0].getMaterial());
    wall[3].setLocalRotation(wall[2].getLocalRotation());
    wall[3].setLocalTranslation(10, 2.5f, 0);
    rootNode.attachChild(wall[3]);
    
    wall[4] = new Geometry("Wall 5", wallBox);
    wall[4].setMaterial(wall[0].getMaterial());
    wall[4].setLocalRotation(wall[3].getLocalRotation());
    wall[4].setLocalTranslation(10, 2.5f, -8);
    rootNode.attachChild(wall[4]);
    
    wall[5] = new Geometry("Wall 6", wallBox);
    wall[5].setMaterial(wall[0].getMaterial());
    wall[5].setLocalTranslation(0, 2.5f, -10);
    rootNode.attachChild(wall[5]);
    
    wall[6] = new Geometry("Wall 7", wallBox);
    wall[6].setMaterial(wall[0].getMaterial());
    wall[6].setLocalTranslation(8, 2.5f, -10);
    rootNode.attachChild(wall[6]);
    
    wall[7] = new Geometry("Wall 8", wallBox);
    wall[7].setMaterial(wall[0].getMaterial());
    wall[7].setLocalTranslation(-8, 2.5f, 10);
    rootNode.attachChild(wall[7]);
    
    wall[8] = new Geometry("Wall 8", wallBox);
    wall[8].setMaterial(wall[0].getMaterial());
    wall[8].setLocalTranslation(-8, 2.5f, -10);
    rootNode.attachChild(wall[8]);
    
    //Big back wall
    Box bigWallBox = new Box(Vector3f.ZERO, 10, 2.5f, 0);
    bigWallBox.scaleTextureCoordinates(new Vector2f(5,1));
    wall[9] = new Geometry("Wall 9", bigWallBox);
    wall[9].setMaterial(wallMat);
    wall[9].setLocalRotation(wall[2].getLocalRotation());
    wall[9].setLocalTranslation(-10, 2.5f, 0);
    rootNode.attachChild(wall[9]);
    
    flyCam.setMoveSpeed(75);
    
    
    guiNode.detachAllChildren();
    
    hud myhud = new hud();
    myhud.initHUD();
    
}

}
[/java]

I tried to pass in my app in main.java as a parameter for initHud() and it didnt work. I also tried to pass it in the constructor of hud.java, but I wasn’t sure what to do after passing it into the constructor. Thanks again.

How many applications do you want in your application? That’s the fundamental disconnect, I think. Things are named somewhat logically so if you catch yourself trying to have lots of applications in your application then something is wrong.

Learning Java is hard. Learning to do object oriented programming is hard. Learning to do 3D game programming is one of the hardest things there is. Attempting to do all three at the same time is like trying to learn to juggle while standing on the seat of a moving bicycle without having learned to ride a bike first.

If you are still learning to program in Java and still learning OOP then I suggest starting with a simpler more straight-forward project to get your feet wet. A simple text adventure is usually what I recommend. It teaches code separation, game/command loops, etc… all of which is 100% applicable to any game.

okay, what is a texture adventure?

@hamid14 said: okay, what is a texture adventure?

Sorry, I meant “text adventure”. My fingers are so used to typing “texture” that the automatically add the additional characters.

Text adventures:
http://www.malinche.net/demos/

Basically, a command line based game. Enter command, see result, enter another command. Ultimately illustrates a game loop, modular commands, game entities, inventory, etc… Furthermore, anyone with the chops to write a 3D game could write one in a few hours. I think it’s a great learning tool.