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 › Can I subclass PGraphics
Page Index Toggle Pages: 1
Can I subclass PGraphics? (Read 381 times)
Can I subclass PGraphics?
Mar 16th, 2010, 10:33am
 
Hi, all.  I've been using Processing for ~1 year now, slowly taking steps into the wider Java world (e.g., using Eclipse now).

I'm trying to add some functionality to PGraphics, especially to PGraphics3D.  My first thought was to simply subclass PGraphics.  However, this seems to run afoul of createGraphics(), which (as far as I can tell) returns a PGraphics2D or PGraphics3D depending on your choice of renderer.  If I try, e.g....

Code:
class QPGraphics extends PGraphincs {
   // blah blah blah
}

void setup() {
   QPGraphics qpg = (QPGraphics) createGraphics(100, 100, P2D);
}


...I get a ClassCastException.  Based on what I know of java so far, that makes sense to me: the compiler obviously can't guarantee that two subclasses of the same parent class are going to play well together.

But, the RawDXF library has no problem casting a createGraphics() to a subclass of PGraphics.  I've looked through the source to RawDXF and I can't find what it is about RawDXF that makes it special.  (And I've tried RawDXF myself and it works fine, so it's not some weird me-specific issue.)

So, what am I missing?  Is there a way I can subclass PGraphics and have the subclass work happily with createGraphics()?
Re: Can I subclass PGraphics?
Reply #1 - Mar 16th, 2010, 10:50am
 
I faced this very same issue for the Picking library. You can browse the source code here :
http://code.google.com/p/processing-picking-library/source/browse/trunk/Picking/...

I extended PGraphics3D :

Code:
package picking;

public class Buffer extends PGraphics3D {
}


and just called PApplet.createGraphics() specifying my own subclass.

Code:
public class Picker {
 
 protected PApplet parent;
 public Buffer buffer;
 
 public Picker(PApplet parent) {
   buffer = (Buffer) parent.createGraphics(parent.width, parent.height, "picking.Buffer");
   buffer.callCheckSettings();
 }
}


Hope it helps.
Re: Can I subclass PGraphics?
Reply #2 - Mar 16th, 2010, 10:56am
 
antiplastik wrote on Mar 16th, 2010, 10:50am:
Hope it helps.

It does, very much.  Thank you!
Page Index Toggle Pages: 1