We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Can't we use processing math functions when creating a library in java for processing? I added the core.jar but get multiple errors when I use functions like cos() or int().
Also, the HelloLibrary.java file created has a
package template.library; package name. Do we rename it entirely or just the "template" part?
i.e.
package testLibrary; or
package testLibrary.library;
Thanks in advance, Alex
Answers
Either use
import static processing.core.PApplet.*;:
cos(radians(90));
Or w/
import processing.core.PApplet;, prefix them w/ PApplet:
PApplet.cos(PApplet.radians(90)));
Keep in mind we can directly access members from Java's class Math as well:
Math.cos(Math.PI);
Just choose a lowercase name for
package:
package org.alex_pr.name_of_my_library;or something like this. ~:>
That works nicely! Thanks a lot!
I also want to use the methods in processing without prefixing the class. Is there any way to do that? I tried
import static myLibrarybut this displays an error "only a type can be imported, library resolves to a package."
import staticworks only for the
staticmembers of a
classor
interface.
And we can't use it if it name-conflicts w/ something else in our code.
So do I have to prefix the methods in processing? It would be really useful to simple type the method name (like we do for processing functions line(), translate() etc.)
A sketch program (.pde) is a subclass of PApplet.
That's why we don't need to prefix its members w/
this.if we don't want to.
You can explore this link: although I believe the PApplet offers you what you need. A quote from the website above:
Kf
This is a bit off topic but is it possible to create a public variable inside a class and link it with a method (of the same class and the same type as the variable) so that each time that variable is used, it gets its value from the linked function?
In JS it is, but not in Java. ~O)
That is also possible in Python. But the Java way is to write getters and setters for almost everything -- that way if you ever decide to mediate variable access with a function as you describe your access method doesn't change, because every variable is already mediated by a .get() type of function.