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 & HelpIntegration › Processing/eclipse in a multiple class program
Page Index Toggle Pages: 1
Processing/eclipse in a multiple class program (Read 518 times)
Processing/eclipse in a multiple class program
May 5th, 2008, 11:15am
 
 Hi! I am working with processing using the eclipse IDE and my idea is to have a main class that extends PApplet and have some separate classes for the main to work with.
 Some classes should have drawing methods but as they don't extend PApplet I don't have access to the primitive methods like rect() or even print().

 How should I organize my code so I can acess these methods from classes outside the main like I do with the processing IDE?
The program will be just too big to put it all together in one file.
Re: Processing/eclipse in a multiple class program
Reply #1 - May 5th, 2008, 11:30am
 
As classes in the processing IDE normally are inner classes the PApplet object is still there. In eclipse you have to pass the main PApplet object to every class instance if you needed there:

Code:

import processing.core.*;
class MyClass{
PApplet p;
MyClass(PApplet p){
this.p=p;
}
public void test(){
p.ellipse(10,10,5,5);
}
}

class MyMainClass{
public void setup(){
MyClass mc = new MyClass(this);
mc.test();
}
}


Static methods like the most math stuff needs to call like this: PApplet.abs(). You can also pass only the main PGraphics object if you only need the drawing stuff in your class.

Code:

import processing.core.PGraphics;
class MyClass{
PGraphics g;
MyClass(PGraphics g){
this.g=g;
}
public void test(){
g.ellipse(10,10,5,5);
}
}

class MyMainClass{
public void setup(){
MyClass mc = new MyClass(g);
mc.test();
}
}

Re: Processing/eclipse in a multiple class program
Reply #2 - May 5th, 2008, 11:38am
 
you need to pass a reference of the PApplet extended class to your custom class.

(1) you can pass a reference of PApplet to your custom classes in the constructor for example. in setup of your PApplet extended class try this:
Code:

MyClass m;

void setup() {
m = new MyClass(this);
}

void draw() {
m.update();
}



then in MyClass
Code:

public class MyClass {
PApplet myPApplet;
public MyClass(PApplet thePApplet) {
myPApplet = thePApplet;
}

public void update() {
myPApplet.rect(0,0,10,10);
}

}


or

(2) you pass a reference to PApplet when you update your class, for example:
in your PApplet extended class

Code:

void draw() {
m.draw(this);
}


in your MyClass you would need a method draw(PApplet) then
Code:

public void draw(PApplet thePApplet) {
thePApplet.rect(0,0,10,10);
}


hope this helps. best,
andi
Re: Processing/eclipse in a multiple class program
Reply #3 - May 5th, 2008, 12:20pm
 
Thank you so much for the really quick and acurate reply guys!! you rock!!

  Thanks!
Page Index Toggle Pages: 1