We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProcessing DevelopmentLibraries,  Tool Development › how to get pixels value
Page Index Toggle Pages: 1
how to get pixels value? (Read 821 times)
how to get pixels value?
Apr 17th, 2008, 12:08pm
 
Hi all,

If I implement a library, how could I get the pixels[] values inside the processing from the outside library?

Thx
Re: how to get pixels value?
Reply #1 - Apr 17th, 2008, 2:59pm
 
You have to pass the main PApplet object or the main canvas to your library, like this
MyLib myLib = new MyLib(this) /new MyLib(g)

In your lib you can use yourPassedPApplet.g.pixels[] or g.pixels[]
Re: how to get pixels value?
Reply #2 - May 19th, 2008, 7:52am
 
thank you very much.
Re: how to get pixels value?
Reply #3 - May 19th, 2008, 10:29am
 
I have encountered situations where a library offers several different classes, and in order to avoid having to pass the PApplet to each class, I had a static property and init() method containing the PApplet:

Code:

public class MyLibrary{
static PApplet parent;

public static class LibraryNotInitializedException extends NullPointerException{
LibraryNotInitializedException(){
super("Must call MyLibrary.init(this); before using this library.");
}
}

static void init(PApplet _parent){
parent = _parent;
}

static PApplet parent(){
if(parent == null){
throw new LibraryNotInitializedException();
}

return parent;
}
}


From inside the library then you can access the pixels by doing:

Code:

for(int i=0; i<MyLibrary.parent().pixels.length; i++){
MyLibrary.parent().pixels[i] = 0F;// paint the pixels of black
}


From Processing you would always have to initialize your library before using it:

Code:

MyLibrary.init(this);


This method has the advantage of only having to pass the PApplet once, instead of at every constructor.
Page Index Toggle Pages: 1