We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Halli hallo people, I have a question.
I want to make a library which uses Processing functions to draw some shapes.
I have visited https://github.com/processing/processing/wiki/Library-Basics and have already got the BUILD SUCCESSFUL. My problem is that my classes are unable to call the methods from Processing (eg: println, stroke, etc).
This is my source code.
package src.template.library;
import processing.core.*;
public class HelloLibrary {
// myParent is a reference to the parent sketch
PApplet parent;
int myVariable = 0;
public final static String VERSION = "##library.prettyVersion##";
public HelloLibrary(PApplet parent) {
this.parent = parent;
parent.registerMethod("dispose", this);
welcome();
}
private void welcome() {
System.out.println("##library.name## ##library.prettyVersion## by ##author##");
//error occurs here
println("the Method println is undefined for HelloLibrary");
}
public String sayHello() {
return "hello library.";
}
public static String version() {
return VERSION;
}
public void setVariable(int theA, int theB) {
myVariable = theA + theB;
}
public int getVariable() {
return myVariable;
}
public void dispose(){
}
}
The println(); method cannot be called, and the console writes ""the Method println is undefined for HelloLibrary". Is there something I am missing? Thank you for reading this.
Answers
static imports?
http://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html
import static processing.core.*;
making the whole import static makes calling PApplet not possible.
I guess putting " PApplet. " in front of everything works, but I do not know the consequences yet. Thanks for your time koogs.
PApplet.println("this works");
However, PApplet.ellipse(....); or PApplet.rect(....); does not work, due to error:
"Cannot make a static refrence to a non-static method"
Anyone have any ideas?
In order to access a PApplet member, we gotta use some instance reference of it.
You already get 1 and store it in the field parent. You just need to keep using it!
Like you just did when you invoked registerMethod() @ line #16! #-o
parent.registerMethod("dispose", this);
parent.println("this works");
Although I strongly advise you to use a 1-letter name for that field:
final PApplet p;
p.registerMethod("dispose", this);
p.println("this works");
Perhaps name it as
$
à la JQuery: ;)final PApplet $;
No 1 deserves to type in 6 letters plus a dot before using each of the Processing's API members! >:)
Some methods such as println(...) are class methods - they belong to a class to run them you simply prefix it with the class name e.g.
PApplet.println(...);
These are called static methods.
Methods such as ellipse and rect work on an object of type PApplet so in your Hello library use
parent.ellipse(...) and parent.rect(...)
These are called non-static methods.
If you look at the source code for PApplet you will find that the println method is declared as static (line 4601) but the ellipse method is not (line 12163)
I think more methods should be declared as
static
in PApplet. Like delay() for example!Of course, methods that would act upon an instantiated canvas (JFrame & PGraphics) gotta be non-
static
! ;)