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 › Multiple .java files in a sketch
Page Index Toggle Pages: 1
Multiple .java files in a sketch (Read 611 times)
Multiple .java files in a sketch
Sep 15th, 2005, 3:05am
 
For code reusability, I'm writing each class in my simple particle/physics library as a seperate java file within my processing folder, added to the sketch through the sketch->add file.. menu choice. However, when I try to use things like the PImage datatype in files other than the "main" file for the sketch, I get errors like: Type "PImage" was not found.  The same occurs when I try to use functions from the processing API.  How do I deal with this? I imagine I have to set up a situation where a parent variable within the new class contains a pointer to the main PApplet, and then use parent.sphere() and etc, but I'm a bit fuzzy on the details. Help?

Cheers,
Ryan Spicer
Re: Multiple .java files in a sketch
Reply #1 - Sep 15th, 2005, 1:45pm
 
you need to add:

import processing.core.*;

to the top of any java files that need the PXxxx stuff. or imports for any of the other libraries that you might be using.

when creating your other object, say it's called Particle, you need to pass the PApplet in the constructor and do that parent thing. so for instance:

Code:
// in the first tab

Particle p;

void setup() {
 p = new Particle(this);
}
void draw() {
 p.draw();
}


// in your other java file

public class Particle {
 PApplet parent;

 public Particle(PApplet parent) {
   this.parent = parent;
 }
 public void draw() {
   parent.line(20, 20, 80, 80);
 }
}
Re: Multiple .java files in a sketch
Reply #2 - Sep 15th, 2005, 2:45pm
 
Thanks for the info, Fry. That's just what I needed. Smiley
Page Index Toggle Pages: 1