3

I know from this page that I can select a method like this:

API["test(Integer[])"](1);

How do I do this for constructors? In particular, I'm trying to instantiate a java.awt.Color from Nashorn:

var highlightColor = new java.awt.Color(1, 1, 128/255, 1);

I get the following error: Can't unambiguously select between fixed arity signatures [(float, float, float, float), (int, int, int, int)] of the method java.awt.Color. for argument types [java.lang.Integer, java.lang.Integer, java.lang.Double, java.lang.Integer]

I've tried this:

var highlightColor = new java.awt.Color["(float,float,float,float)"](1, 1, 128/255, 1);

But that gives me this error: Caused by: :52 TypeError: null is not a function

Community
  • 1
  • 1
David Struck
  • 157
  • 1
  • 9

3 Answers3

4

We added this functionality, but it will only be available with Java 8u40. The exact syntax is java.awt["Color(int, int, int)"] (the signature is part of the last name component, consistent with how it works on method names). You can try it out with early access releases of 8u40 at this time (it's scheduled for release in March 2015).

Attila Szegedi
  • 4,405
  • 26
  • 24
2

You need to make sure you're passing in 4 integers (or floats in this case), try this.

var highlightColor = new java.awt.Color(1.0, 1.0, 128/255, 1.0);
Constant
  • 780
  • 1
  • 5
  • 19
2

Pass either 4 ints or 4 floats

var highlightColor = new java.awt.Color(255, 255, 128, 255);

OR

var highlightColor = new java.awt.Color(1.0, 1.0, 128/255, 1.0);
Mmmh mmh
  • 5,334
  • 3
  • 21
  • 29