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 & HelpPrograms › Drawing object methods to a PGraphics object
Page Index Toggle Pages: 1
Drawing object methods to a PGraphics object (Read 652 times)
Drawing object methods to a PGraphics object
Nov 2nd, 2009, 8:33am
 
I'm drawing to an offscreen PGraphics buffer, and trying to figure out how to call my own classes/objects so that they draw to that buffer.  For example, say I have an Airplane class with a draw() method.  Normally, I would use:

airplane.draw();

I'm trying this, to get airplane to draw to the PGraphics object instead:

PGraphics pg;
pg.airplane.draw();

This throws an error "cannot be resolved or is not a field".

Is there a way to do this, or do PGraphics objects take only built-in Processing objects and methods?
Re: Drawing object methods to a PGraphics object
Reply #1 - Nov 2nd, 2009, 8:50am
 
Scott Murray wrote on Nov 2nd, 2009, 8:33am:
pg.airplane.draw();
[...] do PGraphics objects take only built-in Processing objects and methods

That's the way around: PGraphics doesn't take objects and methods but have fields and methods that can be used in any object.

You can use your global PGraphics, if unique to your sketch, or you can pass a PGraphics to draw on to your objects.
The latter is slightly cleaner, so let's see how to do it.
Code:
class Airplane
{
 PGraphics pg;
 Airplane(PGraphics surfaceToDrawOn)
 {
   pg = surfaceToDrawOn;
 }
 // or use a setter:
 void setSurfaceToDrawOn(PGraphics surface)
 {
    pg = surface;
 }
 // [...] (your stuff)
 void display() // Or draw() or whatever
 {
   pg.beginDraw();
   // [...] draw on pg
   pg.endDraw();
 }
}
Re: Drawing object methods to a PGraphics object
Reply #2 - Nov 2nd, 2009, 8:57am
 
Of course!  Just a different way of thinking about the problem.  Thanks, PhiLho.
Page Index Toggle Pages: 1