Loading...
Logo
Processing Forum
Hi,

I'm trying to run a method in a different class "on" a layer created by PGraphics and createGraphics.

I have just started to use Class and cannot sort out by myself now, so could anyone give some help?

What I'm trying now is.....

------------------------------------------------------------------------------------------------------------------
myClass myInstance;
PGraphics topLayer;

void setup()
{
           :
      myInstance= new myClass();
      topLayer= createGraphics(width,height,g.getClass(),getName());
}

void draw()
{
            :
      topLayer.beginDraw();
      topLayer.myInstance.myMethod();      // <- this line is the problem...
      topLayer.endDraw();
      image( topLayer, 0, 0 );
}
------------------------------------------------------------------------------------------------------------------

I know it means I need "myInstance.myMethod()" under "topLayer". But PGraphics class is something I cannot reach easily.

Is there any way I can run "myInstance.myMethod()" on the "topLayer"?


Thanks in advance,


P.S.
I refered this topic:

Replies(9)

Is there any way I can run "myInstance.myMethod()" on the "topLayer"?
No. Unless you sub-class PGraphics, perhaps. It is simpler to write:
Copy code
  1.       topLayer.beginDraw();
  2.       myInstance.myMethod(topLayer);
  3.       topLayer.endDraw();
  4.       image( topLayer, 0, 0 );
Less elegant, but much easier to implement...
Thanks a lot phi.lho!

Simpler and easier is more elegant actually, so it seems perfect.

Probably my question was a basic grammar question for you guys...

But can I ask just one more for how I can write if I want to send the data for the methods?

For example, if  I want to run....

myMethod(x,y);


I tried
Copy code
  1.       topLayer.beginDraw();
  2.       myInstance.myMethod(x,y)(topLayer);
  3.       topLayer.endDraw();
  4.       image( topLayer, 0, 0 );
but obviously it didn't work...


Could you tell me how I can write? It's more exact what I want to do actually.


Thanks again 
Ah, I see what you're missing.  I think PhiLho's example gave you the impression that there's an "automatic" way to have drawing commands go to some PGraphics object instead of the main surface; there isn't.  In his example, he was assuming that you re-wrote the methods of your class to take a PGraphics object as an argument.  For example:

Copy code
  1. class Foo {

  2.   // ...

  3.   void drawACircle(PGraphics pg, float x, float y, float r) {
  4.     pg.ellipse(x, y, r, r);
  5.   }

  6.   // ...

  7. }
Then in your program, you'd do something like:
Copy code
  1. PGraphics topLayer = createGraphics( // ... whatever

  2. Foo myFoo = new Foo();

  3. myFoo.drawACircle(topLayer, 10, 10, 5);
... which then causes myFoo to draw a circle on the topLayer object.  Basically, there's no "magical" way to change what surface you're drawing on; if you want to draw an object on different surfaces then you have to set it up that way yourself.

Oh, and because this will probably be your next question: the PGraphics object used as the main drawing surface for a sketch is called g.  So, if you wanted your Foo object to draw a circle on the main sketch display, you'd do:
Copy code
  1. myFoo.drawACircle(g, 10, 10, 5);

Thanks a lot adamcsmitty!

I was misunderstanding but now have got crystal clear idea! The code I was trying is now working fine!

Then, I hope it's my final question... is it possible I can get fadeout effect on PGraphics?

It should work normally by overwriting rectangle with alpha but in this case I don't want to give any colour to the background of PGraphics. I just want to fade my drawing on the toplayer.

I'm now trying to capture the screen every frames and load it as PImage with tint. I guess it's going to be ridiculous heavy...

Thanks many times
It should work normally by overwriting rectangle with alpha but in this case I don't want to give any colour to the background of PGraphics
Just use black or some gray grey.
mmm...I've been trying with using black as fill colour for the rectangle but still not sorting out.

It makes the background black/grey although I want to keep background as transparent.


I tried
Copy code
  1.       topLayer.beginDraw();
  2.       topLayer.noStroke();
  3.       topLayer.fill(0,50);
  4.       topLayer.rect(0,0,width,height);
  5.       myInstance.myMethod(x,y,topLayer);
  6.       topLayer.endDraw();
  7.       image( topLayer, 0, 0 );
or...
Copy code
  1.       topLayer.beginDraw();
  2.       myInstance.myMethod(x,y,topLayer);
  3.       topLayer.endDraw();
  4.       image( topLayer, 0, 0 );
  5.       
  6.       topLayer.beginDraw();
  7.       topLayer.noStroke();
  8.       topLayer.fill(0,50);
  9.       topLayer.rect(0,0,width,height);
  10.       topLayer.endDraw();
  11.       image(topLayer,0,0);
mmm.. could I have any advice?
I think topLayer.background(0, 50) will make your background black with an alpha of 50 (out of 255);
 
don't think you can fade out a PGraphics, as it only supports binary (on/off) transparency.  from PGraphics doc page:
" Note that transparency levels are binary: pixels are either complete opaque or transparent."
The doc. is outdated with regard to this... At least for the JAVA2D PGraphics.