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.
Page Index Toggle Pages: 1
writing classes (Read 1383 times)
writing classes
Mar 11th, 2007, 11:18pm
 
Hello

Starting to create a small application in processing which uses video and this is the first time I really need to start splitting things into classes, the first I need being a webcam capture class.  Here is the main sketch invoking the class:

Webcam_capture v_camimage;

void setup()
{
 size(640, 480, P3D);
 background(255);
 v_camimage = new Webcam_capture();
}


and here is the class:

class Webcam_capture
{
   import processing.video.*;
   PApplet p;
   Capture v_capture;
   PImage v_image;
   int v_width, v_height, v_xpos, v_ypos;
 
 Webcam_capture()
 {
   //dv video name
   String webcam = "IIDC FireWire Video";
   
   //width and height stored as a variable
   v_width = 318;
   v_height = 256;

   //capture the camera
   v_capture = new Capture(p, webcam, v_width, v_height);
   v_capture.framerate(30);
   
   //calculate position for video on screen according to window size and video size
   v_xpos = (width / 2) - (v_width / 2);
   v_ypos = (height / 2) - (v_height / 2);
 }

 //capture event function
 void captureEvent(Capture v_capture)
 {
   //read the latest image from the capture event
   v_capture.read();
 }

 //draw function (this loops - like an exitframe in director)
 void draw()
 {
   //set the image to be the latest image from capture event
   v_image = image(v_capture, v_xpos, v_ypos);
   return v_image;
 }

}

I seem to be mainly getting a problem with the line:
v_capture = new Capture(p, webcam, v_width, v_height);
in my original webcam capture sketch I have this where p is, here I've copied it from an example I found online but I expect there are several issues, can anyone point me in the right direction?

a+
gar
Re: writing classes
Reply #1 - Mar 14th, 2007, 8:00pm
 
Hello

Still stuck on this but it seems to be the line new capture() is causing the problem.  

v_capture = new Capture(this, webcam, v_width, v_height);

When using 'this' as the first parameter of new capture(), 'this' seems to refer to the applet itself.  When new capture() is in a class in my application what should I use?  Is it 'this'?  Or is it something in 'this' (i.e. this.that)?

Thanks in advance.

a+
gar
Re: writing classes
Reply #2 - Mar 14th, 2007, 8:46pm
 
You need to pass the papplet into your class:

Code:
 void setup()
{
size(640, 480, P3D);
background(255);
v_camimage = new Webcam_capture(this);
}


and here is the class:

class Webcam_capture
{
import processing.video.*;
PApplet p;
Capture v_capture;
PImage v_image;
int v_width, v_height, v_xpos, v_ypos;

Webcam_capture(PApplet parent)
{
//dv video name
String webcam = "IIDC FireWire Video";
p=parent;
//width and height stored as a variable
v_width = 318;
v_height = 256;

//capture the camera
v_capture = new Capture(p, webcam, v_width, v_height);
v_capture.framerate(30);

//calculate position for video on screen according to window size and video size
v_xpos = (width / 2) - (v_width / 2);
v_ypos = (height / 2) - (v_height / 2);
}

//capture event function
void captureEvent(Capture v_capture)
{
//read the latest image from the capture event
v_capture.read();
}

//draw function (this loops - like an exitframe in director)
void draw()
{
//set the image to be the latest image from capture event
v_image = image(v_capture, v_xpos, v_ypos);
return v_image;
}

}
Re: writing classes
Reply #3 - Mar 14th, 2007, 10:53pm
 
Quote:
You need to pass the papplet into your class:



ahhh that makes sense, I thought it needed something like that but I would have spent hours figuring out how to do it.  Thanks for that.

a+
gar
Page Index Toggle Pages: 1