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:

  1. package com.javazing.gameengine;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.io.Reader;
  9.  
  10. import javax.script.ScriptEngine;
  11. import javax.script.ScriptEngineManager;
  12.  
  13. /**
  14. *
  15. * This program loads and executes the JavaScript program
  16. *
  17. * @author Sergey Nechaev (snechaev@javazing.com)
  18. *
  19. */
  20. public class GameEngine {
  21.  
  22.     private static final String SCRIPT_ENGINE_NAME = "js";
  23.  
  24.     private static final String SCRIPT_FILE_NAME = "script/js.js";
  25.  
  26.     private ScriptEngine jsEngine;
  27.  
  28.     private ScriptEngineManager mgr;
  29.  
  30.     static {
  31.         System.loadLibrary("irrlicht_wrap");
  32.     }
  33.  
  34.     public static void main(String[] args) throws Exception {
  35.  
  36.         new GameEngine();
  37.  
  38.     }
  39.  
  40.     public GameEngine() throws Exception {
  41.  
  42.         mgr = new ScriptEngineManager();
  43.         jsEngine = mgr.getEngineByExtension(SCRIPT_ENGINE_NAME);
  44.  
  45.         // InputStream is = this.getClass().getResourceAsStream("js.js");
  46.         File file = new File(SCRIPT_FILE_NAME);
  47.         InputStream is = new BufferedInputStream(new FileInputStream(file));
  48.  
  49.         Reader reader = new InputStreamReader(is);
  50.         jsEngine.eval(reader);
  51.  
  52.     }
  53.  
  54. }

And the JavaScript with the 'HelloWorld' example from the Irrlicht distribution:

JavaScript:

  1. importPackage(net.sf.jirr);
  2. importPackage(java.lang);
  3.  
  4.  
  5. var device = Jirr.createDevice(E_DRIVER_TYPE.EDT_OPENGL,
  6. new dimension2di(640, 480), 16, false, false, false, null);
  7.  
  8. print("idevice = " + device);
  9.  
  10. var windowCaption = "Java@Hello World! - Irrlicht Engine Demo";
  11. device.setWindowCaption(windowCaption);
  12.  
  13. var driver = device.getVideoDriver();
  14. var smgr = device.getSceneManager();
  15. var guienv = device.getGUIEnvironment();
  16.  
  17. var text = "Hello World! This is the Irrlicht Software engine for Java!";
  18. var rect = new recti(10, 10, 280, 30);
  19. var staticText = guienv.addStaticText(text, rect, true, true, null, -1);
  20.  
  21. smgr.addCameraSceneNode(null, new vector3df(0,10,-60),
  22. new vector3df(0,0,0), -1);
  23.  
  24. var step = 100;
  25. var counter = 0;
  26. var diff /*= 0*/;
  27. var timer1 = System.currentTimeMillis();
  28. var timer2 /*= 0*/;
  29. while(device.run())
  30. {
  31.  
  32.  
  33.     var a = 0;
  34.     var r = 100;
  35.     var g = 100;
  36.     var b = 100;
  37.  
  38.     driver.beginScene(true, true, new SColor(a, r, g, b));
  39.  
  40.     smgr.drawAll();
  41.     guienv.drawAll();
  42.  
  43.     driver.endScene();
  44.  
  45.     counter++;
  46.     if (counter>= step)
  47.     {
  48.         timer2 = System.currentTimeMillis();
  49.         diff = 1000.0 / ((timer2 - timer1) / step);
  50.         counter = 0;
  51.         timer1 = System.currentTimeMillis();
  52.  
  53.         //text = Jirr.createWchar("This is the Irrlicht Software
  54.         //engine for Java! FPS: " + diff);
  55.         text = "This is the Irrlicht Software engine for Java! FPS: " + diff;
  56.         staticText.setText(text);
  57.     }
  58. }
  59.  
  60. device.closeDevice();

4 Responses to “Java 3D Application in JavaScript (Irrlicht & Jirr)”

  1. All in a days work… Says:

    [...] 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) [...]

  2. Stefan Says:

    Cool! That is something worth to be mentioned in the news section! Great!!

  3. Irrlicht jirr Says:

    Where could I get
    "javax.script.ScriptEngine"
    "javax.script.ScriptEngineManager"

    Thinks.

  4. admin Says:

    you should install java 1.6

Leave a Reply