Use processing math functions in a processing library

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

  • edited May 2017

    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);
    http://docs.Oracle.com/javase/8/docs/api/java/lang/Math.html

  • 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 myLibrary but this displays an error "only a type can be imported, library resolves to a package."

  • edited May 2017

    import static works only for the static members of a class or 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: http://www.lagers.org.uk/qscript/index.html although I believe the PApplet offers you what you need. A quote from the website above:

    QScript is entirely written in Java and has no external dependancies.

    Although the library is packaged as a Processing library the actual library jar does not require Processing for its functions. The examples that come with this package use the G4P and Graphica libraries and include a QScript IDE. For non-Processing users the library jar also inclues a QScript IDE using Java Swing controls.

    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.

Sign In or Register to comment.