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.
IndexProgramming Questions & HelpSyntax Questions › Using a PImage in a class
Page Index Toggle Pages: 1
Using a PImage in a class? (Read 507 times)
Using a PImage in a class?
Mar 18th, 2008, 8:52pm
 
I have the following code;
Quote:
class imageButton extends Button{
 PImage img;
 String txt;
 
 imageButton(Panel p, String itxt, int ix, int iy,PImage iImg){
   super(ix, iy, 20, 20, p);
   txt=txt;
   img=iImg.get();//throws Null pointer
   println(iImg);//Null
   img=icons.get(16,0,32,16);//icons is created in the main prog, throws Null
 }
 void update(){
   fill(backC);
   edgeC=panelEdge;
   if (overRect(x,y,w,h)){
     edgeC=bOver;
     if(mLeft){
       pressed=true;
       
       edgeC=select;
     }
   }
   if(pressed==true){
     edgeC=select;
   }
   stroke(edgeC);
   rect(parent.sX+x,parent.sY+y,w,h);
   image(img,parent.sX+x+2,parent.sY+y+2);
   
 }
}


when its used, and i pass a PImage to the function, it doesn't work, iImg comes back as null, which means I can't use it elsewhere in the function. It works ok if I change the code to directly reference a PImage created in the main program, but obviously this isn't ideal. Is there a work around for this?
Re: Using a PImage in a class?
Reply #1 - Mar 18th, 2008, 10:21pm
 
You have to call iImg.loadPixels() before getting the values.
Re: Using a PImage in a class?
Reply #2 - Mar 19th, 2008, 10:45am
 
no, that doesn't work, iImg comes through the parameters as null, so running loadPixels on it results in a Null Pointer Exception.

I don't seem to be able to make it actually pass the PImage through the constructor properly.
Re: Using a PImage in a class?
Reply #3 - Mar 19th, 2008, 12:37pm
 
Maybe it would help if you post the whole code, to see what's wrong with imageButton call.
Re: Using a PImage in a class?
Reply #4 - Mar 19th, 2008, 1:03pm
 
Aaaahahha! I'm an idiot, that was the problem, not processing.

I was initialising my GUI before i'd loaded all my images! I hadn't noticed before because up till now, none of the other GUI code needed to load images on start up.

Oh well, thanks for the help anyway!

Martin
Page Index Toggle Pages: 1