example using rhino's JavaAdapter

懵懂的女人 提交于 2019-12-05 01:11:52

问题


Can someone provide me an example on how to extend a java class in java script using Rhino's java adapter ?


回答1:


For anyone else who might come across this, there's a decent example here (the author uses it to extend awt.Canvas).

var smileyCanvas = new JavaAdapter(awt.Canvas, {
   paint: function (g) {
       var size = this.getSize();
       var d = Math.min(size.width, size.height);
       var ed = d / 20;
       var x = (size.width - d) / 2;
       var y = (size.height - d) / 2;
       // draw head (color already set to foreground)
       g.fillOval(x, y, d, d);
       g.setColor(awt.Color.black);
       g.drawOval(x, y, d, d);
       // draw eyes
       g.fillOval(x+d/3-(ed/2), y+d/3-(ed/2), ed, ed);
       g.fillOval(x+(2*(d/3))-(ed/2), y+d/3-(ed/2), ed, ed);
       //draw mouth
       g.drawArc(x+d/4, y+2*(d/5), d/2, d/3, 0, -180);
   }
});

There's more information on MDN, including a brief explanation and calling syntax example.




回答2:


Depends on what you want to inherit. I find that if I use the JavaScript prototype for object “definitions” I get the static methods of Java objects only:

function test() {
  this.hello = function() {
    for(var i in this) {
      println(i);
    }
  };
}
test.prototype= com.acme.app.TestClass; // use your class with static methods
// see the inheritance in action:
var testInstance=new test();
test.hello();

However, JavaScript allows you to do prototype assignments on object instances as well, so you could use a function like this, and get a more Java-like inheritance behaviour:

function test(baseInstance) {
  var obj = new function() {
    this.hello=function() { 
      for(var i in this) { 
        println(i); 
      }
    };
  };
  obj.prototype=baseInstance; // inherit from baseInstance.
}

// see the thing in action:
var testInstance=test(new com.acme.TestClass()); // provide your base type
testInstance.hello();

Or use a function (e.g. init) similar to the above function in the object itself:

function test() {
  this.init=function(baseInstance) {
    this.prototype=baseInstance;
  };
  this.hello=function() {
    for(var i in this) {
      println(i);
    }
  };
}

var testInstance=new test();
println(typeof(testInstance.prototype)); // is null or undefined
testInstance.init(new com.acme.TestClass()); //  inherit from base object

// see it in action:
println(typeof(testInstance.prototype)); // is now com.acme.TestClass
testInstance.hello();



回答3:


Since I am not a 100% sure that by Java Adapter you mean what I think it is, Java interfaces and such can be created with property lists (name = function()):

var runnable = new java.lang.Runnable({
   run: function() { println('hello world!'); } // uses methodName: implementationFunction convention
};
java.awt.EventQueue.invokeLater(runnable); // test it

Or alternatively for single-method things like that:

function runnableFunc() { println('hello world!'); }
var runnable = new java.lang.Runnable(runnableFunc); // use function name
java.awt.EventQueue.invokeLater(runnable); // test it



回答4:


E.g. starting a thread in Rhino:

function startThread(funcOfThread) {
    var someClass = { run: funcOfThread }; // Rhino syntax for new class with overwritten method
    var r = new java.lang.Runnable(someClass);
    var t = new java.lang.Thread(r);
    t.start();
}

function UDP_Client() {
    while (1)
        java.lang.Thread.sleep(100);
}

startThread(UDP_Client);


来源:https://stackoverflow.com/questions/3427344/example-using-rhinos-javaadapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!