Java 3D Application in JavaScript (Irrlicht & Jirr)
With the new features of Java 1.6 it is incredibly easy to add scripting capabilities to your programs. By using JavaScript you do not have to recompile your application every time you change something, also JavaScript is much easier especially for those new to Java.
Check out the way we implement an Irrlicht application in JavaScript. Of course, you have to download Jirr & have JDK 1.6 installed.
This is the Java program to load and execute the JavaScript code:
JAVA:
-
package com.javazing.gameengine;
-
-
import java.io.BufferedInputStream;
-
import java.io.File;
-
import java.io.FileInputStream;
-
import java.io.InputStream;
-
import java.io.InputStreamReader;
-
import java.io.Reader;
-
-
import javax.script.ScriptEngine;
-
import javax.script.ScriptEngineManager;
-
-
/**
-
*
-
* This program loads and executes the JavaScript program
-
*
-
* @author Sergey Nechaev (snechaev@javazing.com)
-
*
-
*/
-
public class GameEngine {
-
-
-
-
private ScriptEngine jsEngine;
-
-
private ScriptEngineManager mgr;
-
-
static {
-
}
-
-
-
new GameEngine();
-
-
}
-
-
-
mgr = new ScriptEngineManager();
-
jsEngine = mgr.getEngineByExtension(SCRIPT_ENGINE_NAME);
-
-
// InputStream is = this.getClass().getResourceAsStream("js.js");
-
-
jsEngine.eval(reader);
-
-
}
-
-
}
And the JavaScript with the 'HelloWorld' example from the Irrlicht distribution:
JavaScript:
-
importPackage(net.sf.jirr);
-
importPackage(java.lang);
-
-
-
var device = Jirr.createDevice(E_DRIVER_TYPE.EDT_OPENGL,
-
new dimension2di(640, 480), 16, false, false, false, null);
-
-
print("idevice = " + device);
-
-
var windowCaption = "Java@Hello World! - Irrlicht Engine Demo";
-
device.setWindowCaption(windowCaption);
-
-
var driver = device.getVideoDriver();
-
var smgr = device.getSceneManager();
-
var guienv = device.getGUIEnvironment();
-
-
var text = "Hello World! This is the Irrlicht Software engine for Java!";
-
var rect = new recti(10, 10, 280, 30);
-
var staticText = guienv.addStaticText(text, rect, true, true, null, -1);
-
-
smgr.addCameraSceneNode(null, new vector3df(0,10,-60),
-
new vector3df(0,0,0), -1);
-
-
var step = 100;
-
var counter = 0;
-
var diff /*= 0*/;
-
var timer1 = System.currentTimeMillis();
-
var timer2 /*= 0*/;
-
while(device.run())
-
{
-
-
-
var a = 0;
-
var r = 100;
-
var g = 100;
-
var b = 100;
-
-
driver.beginScene(true, true, new SColor(a, r, g, b));
-
-
smgr.drawAll();
-
guienv.drawAll();
-
-
driver.endScene();
-
-
counter++;
-
if (counter>= step)
-
{
-
timer2 = System.currentTimeMillis();
-
diff = 1000.0 / ((timer2 - timer1) / step);
-
counter = 0;
-
timer1 = System.currentTimeMillis();
-
-
//text = Jirr.createWchar("This is the Irrlicht Software
-
//engine for Java! FPS: " + diff);
-
text = "This is the Irrlicht Software engine for Java! FPS: " + diff;
-
staticText.setText(text);
-
}
-
}
-
-
device.closeDevice();
April 27th, 2007 at 4:24 am
[...] Java 3D Application in JavaScript With the new features of Java 1.6 it is incredibly easy to add scripting capabilities to your programs. By using JS, you do not have to recompile your application every time you change something, also JavaScript is much easier especially for those new to (tags: Java JavaScript) [...]
April 27th, 2007 at 4:08 pm
Cool! That is something worth to be mentioned in the news section! Great!!
May 9th, 2007 at 8:28 pm
Where could I get
"javax.script.ScriptEngine"
"javax.script.ScriptEngineManager"
Thinks.
August 3rd, 2007 at 4:59 pm
you should install java 1.6