Calling a method from another Java file.

I think I'm doing this right, but I'm obviously going wrong somewhere.

In order to not have one big long java file, I've created one main one to execute the program, and another to do some of the functions.I'm using eclipse, btw.

In the main file, I have

currentTB = getActiveTB(playerArea);

If it works, it should get send the player area variable to the getActiveTB method, and store the value it returns in the currentTB variable, right?

In the other file, I have;

package scenes;

publicclass sceneIndex{

private TerrainBlock getActiveTB(String areaCode){

TerrainBlock tBlock =new TerrainBlock();

**DOES STUFF**

return tBlock;

}

}

I have it set up so the second file is in the source folder "resources" and the package "scenes."The problem is I'm not calling it properly.I'm missing something simple, I think.The only error I'm getting is;

The method getActiveTB(String) is undefined...

Where am I screwing up?

EDIT: Yeah, I'm a rookie here trying to figure it out on my own.My computer science courses, so far, conveniently all consist of just one big java file.Easier to write, sure, but not very efficient/organized.

Message was edited by:

SuckerPunch

[1593 byte] By [SuckerPuncha] at [2007-11-27 10:53:20]
# 1

In order to call a method in another class you need to have two things. 1. Create an object of that class. 2. The method needs to be public.

sceneIndex si = new sceneIndex();

currentTB = si.getActiveTB(playerArea);

P.S. classes should be in capitals, ie SceneIndex.

P.S.S. adendum to point 1. The method can be static then you don't need an object.

floundera at 2007-7-29 11:43:33 > top of Java-index,Java Essentials,New To Java...
# 2

Hi,

Please post the entire code of the both java files including package names. Also, i think, you have to create the object for the second java file in the first java file's main method and then invoke the method.

Regards,

Loga

Loga_07a at 2007-7-29 11:43:33 > top of Java-index,Java Essentials,New To Java...
# 3

I was concerned about posting the complete code, because it uses the jMonkey Engine.I just figured that unless posters were familiar with it, it may be misleading.

SuckerPuncha at 2007-7-29 11:43:33 > top of Java-index,Java Essentials,New To Java...
# 4

Yeah, so this is still pissing me off.I'm not sure what else to do.I've tried this a million different ways, in both eclipse and netbeans.Whatever.Here's the code, I'd love it if someone could point out the boneheaded mistake I'm making.I'm getting a little frustrated at being stuck on such a simple problem.

Main.javaimport com.jme.app.BaseGame;

import com.jme.input.KeyBindingManager;

import com.jme.input.KeyInput;

import com.jme.math.Vector3f;

import com.jme.renderer.Camera;

import com.jme.renderer.ColorRGBA;

import com.jme.scene.Node;

import com.jme.system.DisplaySystem;

import com.jme.system.JmeException;

import com.jme.util.Timer;

import com.jmex.terrain.TerrainBlock;

import resources.*;

public class Main extends BaseGame {

/* General Variable Declaration */

private String scene;// Tells application which scene to load, default loads default scene

private int width, height, depth, freq; // Creates variables to store information

private boolean fullscreen; // on the user's window

private Camera playerCam; // Defines the main camera for the player, first person view

private Timer timer; // Creates a timer to be used for FPS calculations

private Node currentScene; // The basis for building the scene the player is in

private TerrainBlock currentTB; // This variable holds the current landscape

private String playerArea = new String("initial"); // Contains the string

// code for the scene

// the player is in,

// defaults to the opening scene, unless changed by

// the player loading their game

public static void main(String[] args) {

/* Initializes and Starts the Applications. */

Main app = new Main();

app.setDialogBehaviour(ALWAYS_SHOW_PROPS_DIALOG, Main.class.getClassLoader().getResource("img/spunch.jpg"));

app.start();

}

protected void update(float interpolation) {

}

protected void render(float interpolation) {

}

protected void initSystem() {

/*

* Application initialization properties (window, camera, renderer, key

* bindings, etc)

*/

// Stores variables for window properties

width = properties.getWidth();

height = properties.getHeight();

depth = properties.getDepth();

freq = properties.getFreq();

fullscreen = properties.getFullscreen();

// Assigns renderer and creates game window

try {

display = DisplaySystem.getDisplaySystem(properties.getRenderer());

display.createWindow(width, height, depth, freq, fullscreen);

playerCam = display.getRenderer().createCamera(width, height);

} catch (JmeException e) {

e.printStackTrace();

System.exit(1);

}

// Sets background to black, should never be seen anyway

display.getRenderer().setBackgroundColor(ColorRGBA.black);

// Initializes the camera

playerCam.setFrustumPerspective(45.0f, (float) width / (float) height,

1, 1000);

Vector3f loc = new Vector3f(250.0f, 100.0f, 250.0f);

Vector3f left = new Vector3f(-0.5f, 0.0f, 0.5f);

Vector3f up = new Vector3f(0.0f, 1.0f, 0.0f);

Vector3f dir = new Vector3f(-0.5f, 0.0f, -0.5f);

// Moves and orients the camera

playerCam.setFrame(loc, left, up, dir);

// Update the camera since it has been changed

playerCam.update();

// Create a timer for FPS updates

timer = Timer.getTimer();

// Assign the camera as the primary display in the application

display.getRenderer().setCamera(playerCam);

// Initialize the escape key as a way to exit the program

KeyBindingManager.getKeyBindingManager().set("exit",

KeyInput.KEY_ESCAPE);

}

protected void initGame() {

/* Game initialization */

display.setTitle("Slumlords"); // Displays title in window

SceneIndex sIndex = new SceneIndex();

currentTB = sIndex.getActiveTB(playerArea);

currentScene.attachChild(currentTB);

currentScene.updateGeometricState(0.0f, true);

currentScene.updateRenderState();

}

protected void reinit() {

}

protected void cleanup() {

}

}

SceneIndex.javapackage resources;

import javax.swing.ImageIcon;

import com.jme.bounding.BoundingBox;

import com.jme.image.Texture;

import com.jme.math.Vector3f;

import com.jme.scene.state.TextureState;

import com.jme.system.DisplaySystem;

import com.jme.util.TextureManager;

import com.jmex.terrain.TerrainBlock;

import com.jmex.terrain.util.MidPointHeightMap;

import com.jmex.terrain.util.ProceduralTextureGenerator;

public class SceneIndex {

private DisplaySystem display;

public TerrainBlock getActiveTB(String areaCode) {

TerrainBlock tBlock = new TerrainBlock();

if (areaCode == "initial") {

// Generates random terrain data

MidPointHeightMap heightMap = new MidPointHeightMap (64, 1.0f);

// Scale the data

Vector3f terrainScale = new Vector3f(4, 0.0575f, 4);

// Create the terrain block

tBlock = new TerrainBlock("Terrain", heightMap.getSize(), terrainScale, heightMap.getHeightMap(), new Vector3f(0, 0, 0), false);

tBlock.setModelBound(new BoundingBox());

tBlock.updateModelBound();

// Creating a blended texture based on the height map

ProceduralTextureGenerator pTexture = new ProceduralTextureGenerator(heightMap);

pTexture.addTexture(new ImageIcon(SceneIndex.class.getClassLoader().getResource("img/grass.gif")), -128, 0, 128);

pTexture.addTexture(new ImageIcon(SceneIndex.class.getClassLoader().getResource("img/dirt.jpg")), 0, 128, 255);

pTexture.addTexture(new ImageIcon(SceneIndex.class.getClassLoader().getResource("img/granite.jpg")), 128, 255, 384);

pTexture.createTexture(64);

// Assigning the texture to the terrain

TextureState tState = display.getRenderer().createTextureState();

Texture t1 = TextureManager.loadTexture(pTexture.getImageIcon().getImage(), Texture.MM_LINEAR_LINEAR, Texture.FM_LINEAR, true);

tState.setTexture(t1, 0);

tBlock.setRenderState(tState);

}

return tBlock;

}

}

Again, I've tried this a few different ways, and I either get a "Could Not Find Symbol" error referring to the getActiveTB(playerArea); in Main, or it just says java.lang.NoClassDefFoundError: and Exception in thread "main".

Whatever.

SuckerPuncha at 2007-7-29 11:43:33 > top of Java-index,Java Essentials,New To Java...
# 5

You have already been given the solution in the first reply by Flounder

c0demonk3ya at 2007-7-29 11:43:33 > top of Java-index,Java Essentials,New To Java...
# 6

Give the Modifier as Public for the Menthod getActiveTB(Sring areacode)

Public TerrainBlock getActiveTB(String areaCode) {

TerrainBlock tBlock = new TerrainBlock();

return tBlock;

selestin.muthu@sify.coma at 2007-7-29 11:43:33 > top of Java-index,Java Essentials,New To Java...