FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Integration
(Moderators: fry, REAS)
   Can i use p5 methods to external classes and back?
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Can i use p5 methods to external classes and back?  (Read 614 times)
homesick_alien

WWW
Can i use p5 methods to external classes and back?
« on: Nov 26th, 2004, 3:00pm »

Hi there,
 
i was wondering if i can write a class, where i could use some of the processing methods, such as
 
class MakeRect {
  // ...
  void draw() {
    rect(30, 20, 55, 55);  
  }
}
 
and then use the draw() method from a p5 sketch.
 
MakeRect tester = new MakeRect();
tester.draw();
 
Of course, class MakeRect doesn't compile, as it can't find method "rect". Is there any kind of solution?
 
trip

triparepa WWW Email
Re: Can i use p5 methods to external classes and b
« Reply #1 on: Nov 28th, 2004, 11:04pm »

I suppose that you are talking about using Processing as a library in a Java program. There are a few threads that go over Processing integration, you should look at this one:
 
http://processing.org/discourse/yabb/board_general_action_displ_ay_num_1072722668.html
 
hope that helps,
 
c.
 
homesick_alien

WWW
Re: Can i use p5 methods to external classes and b
« Reply #2 on: Nov 29th, 2004, 5:55pm »

thank you for replying. I read the thread you told me, but didn't try to manage how to do what i want.
maybe i am not clear enough, so here is a better example (i think!):
 
I want to do something like a utility, that i can use in each of my p5 sketches. You can say it's a small library.
So, from inside processing, i would like to write something like:
 
DrawTools dt = new DrawTools();
dt.drawStar();
 
 
 
and obviously, the DrawTools class should have a method drawStar that just draws a star, *using some of  
processing nice methods*, such as line(), fill() etc.
 
so, is this possible or not?
 
thanx again!
 
homesick_alien

WWW
Re: Can i use p5 methods to external classes and b
« Reply #3 on: Nov 30th, 2004, 1:08pm »

xmmm, after reading many posts here, i think that what i need is wait for version 0.70 - or am i completely wrong
 
TomC

WWW
Re: Can i use p5 methods to external classes and b
« Reply #4 on: Nov 30th, 2004, 1:33pm »

If MakeRect is inside the Processing editor, it should compile and run as you expect.  This works for me in 0069 - does it not work for you?
 
Code:

 
class MakeRect {  
  // ...  
  void draw() {  
    rect(30, 20, 55, 55);  
  }  
}  
 
MakeRect tester = new MakeRect();  
tester.draw();  
 

 
Or do you mean to put code in the 'code' folder inside of a sketch, and access drawing functions from there?  It can be done, but the class doing the drawing must know about BApplet (the main processing class).
 
Try making a code folder in your sketch folder, and creating a file called MakeRect.java which contains the following:
 
Code:

 
class MakeRect {  
  BApplet p; // p for parent
  MakeRect(BApplet parent) {
    p = parent;
  }
  void draw() {  
    p.rect(30, 20, 55, 55);  // p.whatever() - the drawing functions belong to BApplet
  }  
}  
 

 
And then the code in the Processing editor is just:
 
Code:

MakeRect tester = new MakeRect(this);  
tester.draw();  

 
Is that what you meant?
 
In general, try to keep everything inside the Processing environment unless it is getting too big.  Yes, this will be easier to manage in future versions, but it does work right now.
 
on edit: corrected mistake identified below
« Last Edit: Nov 30th, 2004, 3:32pm by TomC »  
homesick_alien

WWW
Re: Can i use p5 methods to external classes and b
« Reply #5 on: Nov 30th, 2004, 3:17pm »

ok TomC, thanx a lot - the second one was what i meant.
 
Now it works fine - firstly i thought i had to put the .class file in the 'code' folder,not the source, and it was impossible to compile. :/
 
So, to make things clear, *all* the functionality we have from inside processing, may be used from external programs just by calling methods for a BApplet object?
 
thanks again!
 
PS. Maybe you want to edit your post: " MakeRect tester = new MakeRect(this);" in case someone is confused - you forgot the 'this' keyword
 
TomC

WWW
Re: Can i use p5 methods to external classes and b
« Reply #6 on: Nov 30th, 2004, 3:35pm »

You can put a .class file in there too, but that requires setting up a Java development environment which knows about the Processing classes.  Better just to drop the source in, if you have it.
 
You can't quite use *all* the Processing features.  I don't think the color type will work, for instance, or the function-style casts.  You might also need to be slightly more careful with float constants.  (These differences between Java and Processing are the same issues madmerv is having with an online Processing IDE).
 
homesick_alien

WWW
Re: Can i use p5 methods to external classes and b
« Reply #7 on: Dec 15th, 2004, 3:11pm »

Well, i have a new problem:
 
The constructor of this external class has these arguments:
 
public Parser(String filename, BApplet parent) {
 
When i try to create a new object (from processing), i have to write the full path, i mean something like  
 
Parser p = new Parser("F:\\processing-0068\\sketchbook\\default\\linkage2\\data\\file.t xt", this);
 
 
and NOT just
 
Parser p = new Parser("file.txt", this);
 
I also guess that this is the reason to get a "Loading Java Applet Failed.." when trying to publish
 
Any help please??
 
TomC

WWW
Re: Can i use p5 methods to external classes and b
« Reply #8 on: Dec 15th, 2004, 3:27pm »

Can you show us what you're doing with the filename?  If it's in the data folder you should be able to use your BApplet instance to get hold of it for you.
 
homesick_alien

WWW
Re: Can i use p5 methods to external classes and b
« Reply #9 on: Dec 15th, 2004, 4:27pm »

Thank you for the quick reply!
 
I'm trying to make a file reader, so i'm using a StreamTokenizer object:
 
public Parser(String filename, BApplet parent) {
b=parent;
 
try {
 FileInputStream in = new FileInputStream(filename);
 Reader r = new BufferedReader(new InputStreamReader(in));
 StreamTokenizer st = new StreamTokenizer(r);
....
}
« Last Edit: Dec 15th, 2004, 4:28pm by homesick_alien »  
TomC

WWW
Re: Can i use p5 methods to external classes and b
« Reply #10 on: Dec 15th, 2004, 5:40pm »

Have you tried just using parent.loadStrings()?
 
If you must have a stream and not just some Strings, you could try parent.loadStream(filename) which you could then give to your BufferedReader.
 
homesick_alien

WWW
Re: Can i use p5 methods to external classes and b
« Reply #11 on: Dec 18th, 2004, 12:53pm »

so close
 
InputStream in = parent.loadStream(filename);
Reader r = new BufferedReader(new InputStreamReader(in));
StreamTokenizer st = new StreamTokenizer(r);
 
I changed the first line, but now I get a "java.lang.NullPointerException at BApplet.loadStream(BApplet.java:3191)"
 
 
homesick_alien

WWW
Re: Can i use p5 methods to external classes and b
« Reply #12 on: Dec 18th, 2004, 1:32pm »

i can't see why, but i think i found the problem.
 
in my sketch, i was creating the object outside the setup() function. when i tried to firstly declare the object and then call the constructor from within the setup(), everything worked fine..
 
TomC, thank you a lot for your help and time
 
Pages: 1 

« Previous topic | Next topic »