We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Just checking, but do you know that is js and not java? You can includ it in any html code and it can be run in any browser. You could even use it in the PDE under p5.js mode. Now, taking a wide guess, you could use nashorn
https://forum.processing.org/two/search?Search=nashorn
to run it in Processing java. I haven't done it myself but probly @GoToLoop could tell you if it is possible as he has written most of those post of js in java.
Kf
Just so you know, I've also made some utility functions for Nashorn: O:-)
https://Forum.Processing.org/two/discussion/15151/how-to-convert-string-to-a-line-of-code#Item_9
I've copied file "exJS.java" and pasted it into the PDE as a ".java" tab:
https://Raw.GitHubUserContent.com/liquid600pgm/lib_exJavaScript/master/src/main/java/iliquid/exjs/exJS.java
Created a "script.js" inside subfolder "data/" w/ just this simple statement: print('Nashorn')
.
Made some tiny changes to your posted code. And voilà, it's just worked: \m/
// Forum.Processing.org/two/discussion/26901/
// library-in-code-folder-doesn-t-work-properly#Item_1
// 2018-Mar-18
import iliquid.exjs.exJS;
final exJS js = new exJS();
js.loadScript(dataPath("script.js"));
//js.addImport("java.lang.System", "system");
js.initEngine();
js.evalScript();
exit();
thanks GoToLoop, so this means i can use Nashorn directly from processing IDE?
Can I use this Nashorn in my current program (written in Java Mode)?
Well, as I've already stated, Nashorn is bundled in Java. So it's ready available. :)>-
You can dynamically import and use your Java classes from within Nashorn code. ~O)
AFAIK, Nashorn's imports are fully dynamic at runtime....
can i use this Nashorn in my current program (written in java mode)?
AFAIK, Nashorn's imports are fully dynamic at runtime. ~O)
In the link below, class PApplet is imported via Java.type(): \m/
https://Forum.Processing.org/two/discussion/21709/code-evaluation-during-program-execution#Item_3
I've also made some useful functions to deal w/ Nashorn more easily: :bz
https://Forum.Processing.org/two/discussion/15151/how-to-convert-string-to-a-line-of-code#Item_9
Does Nashorn enable live runtime JavaScript importing / re-importing?
Another way to approach this is to use a language that makes it much easier to do runtime reimport for live coding.
Java bundles Nashorn JS: https://Forum.Processing.org/two/discussions/tagged?Tag=#Nashorn
How do I trigger a sketch-level method from getEnclosingPApplet() then?
Dunno what you mean by "trigger". But if you've simply meant for an inner class to access members of our sketch via "this$0", we can't by regular means. [-X
Only by some hackish workarounds like 100% reflection or Java's bundled JS Nashorn. :ar!
The reason why we can't is b/c we dunno the name of the enclosing top-class under Processing's IDE (PDE); which is the name of our sketch's folder btW. @-)
Therefore, whatever name is chosen when we save our sketch becomes the datatype of the enclosing PApplet top-subclass. :-B
Regardless, an inner class already has full access to its enclosing class members and vice-versa; as long as the inner class doesn't overshadow an enclosing member's name. :-\"
What I don't understand is why are you attempting to create a library using an inner class! :-/
You should at least create a ".java" tab and put your library class there! *-:)
If you opt for Java Mode + Nashorn JS approach, take a look at this example about pulse equation expressions evaluated via ScriptEngine::eval(): :ar!
Do I need P5 as well?
Two of the entries on the "high-profile languages" list of that wikipedia page -- Jython and JRuby -- are what enable the Processing.py and JRubyArt modes. Renjin is being used for the under-development Processing.R. There is also a fair amount on the forum (mostly examples from @GoToLoop) about using Nashorn to write JavaScript for Processing JVM. https://forum.processing.org/two/search?Search=Nashorn
But in the end, I've managed to use Stream::allMatch(), by instantiating my own Predicate & implementing its abstract
method test(). \m/
However, it's not as good as an actual lambda ->
expression, which isn't bound to a specific datatype, escaping somehow from Java's brutal strongly typed system. O:-)
// forum.Processing.org/two/discussion/21761/querying-arraylist#Item_2
// GoToLoop 2017-Apr-02
import java.util.List;
import java.util.function.Predicate;
static final Predicate<Flag> FLAG = new Predicate<Flag>() {
@ Override final boolean test(final Flag f) {
return f.flag;
}
};
int qty = (int) random(1, 6);
final List<Flag> flags = new ArrayList(qty);
void setup() {
for (int i = 0; i++ < qty; ) flags.add(new Flag());
println(flags);
println(flags.stream().allMatch(FLAG));
exit();
}
class Flag {
boolean flag = random(1) > .2;
@ Override String toString() {
return str(flag);
}
}
Another approach, besides using another IDE for a full Java 8 syntax, is implementing a function under Java's builtin JS language called Nashorn, and invoking it from Java's side, passing your Collection container as its argument. :ar!
After all in JS, all of its functions act like lambdas. And they can be passed freely as argument for another function, becoming callbacks for the receiving function! :-bd
You should also consider loadStrings() your Nashorn ".js" scripts.
Visit this forum thread for some utility functions I did for ScriptEngine: :bz
https://forum.Processing.org/two/discussion/17161/eval-js-with-power-general-help-on-eval-js-eval
public
for us behind the scene if they don't have 1 already for ".pde" tab files. :bz public
if something needs them that way in order to access them. :-\" public
. :> // forum.Processing.org/two/discussion/21709/
// code-evaluation-during-program-execution#Item_3
// GoToLoop (2017-Mar-30)
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
final ScriptEngine js = new ScriptEngineManager().getEngineByName("Nashorn");
public class Player {
public color colour = #FF0000;
}
final Player player = new Player();
static final String SCRIPT =
"var PApplet = Java.type('processing.core.PApplet');" +
"print(PApplet.hex(player.colour, 6));" +
"player.colour = 0xff0080A5;";
void setup() {
println(hex(player.colour, 6)); // FF0000
js.put("player", player);
try {
js.eval(SCRIPT); // FF0000
}
catch (final ScriptException ex) {
System.err.println(ex.getMessage());
}
println(hex(player.colour, 6)); // 0080A5
exit();
}
Or you could just load code dynamically, ...
Java is already bundled w/ a JS script language spin-off called Nashorn: \m/
https://forum.Processing.org/two/discussions/tagged?Tag=#nashorn