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.
IndexProcessing DevelopmentLibraries,  Tool Development › Get started creating libraries
Pages: 1 2 
Get started creating libraries (Read 4283 times)
Get started creating libraries
Nov 25th, 2006, 3:27am
 
I’m trying to create a simple library for processing that does some fairly complex calculations. I would like to call up these calculations as functions in processing where I can input the value of some variables from processing PDE. I have looked at the howto.txt file but I didn’t find it helpful because I’ve never done this before. My experience with computer programming is limited to c++ so I’m new to java and trying to translate one of my c++ applications into java so that I can call it in processing from a library and share it with the community when it is finished. If some one can point me towards some sample code so I can see how libraries are written specifically ones that behave the way I’m describing mine to work.
Re: Get started creating libraries
Reply #1 - Nov 25th, 2006, 1:16pm
 
here is a great description.

http://shiffman.net/teaching/programming-from-a-to-z/threads/#libraryhttp://shiffman.net/teaching/programming-from-a-to-z/threads/#library

hope it helps.
Re: Get started creating libraries
Reply #2 - Nov 25th, 2006, 4:10pm
 
The libraries that come with Processing include their source code. You can use these as examples.

Casey
Re: Get started creating libraries
Reply #3 - Nov 25th, 2006, 9:02pm
 
thanks that was a big help.
Re: Get started creating libraries
Reply #4 - Nov 25th, 2006, 11:41pm
 
I am trying to create a function that people can call in processing like

myFunction(1,1);

when the fun the app the function would spit out "2"

I am trying to figure out how I would do this in java once I figure this out I should be fine to do what I need to do from that point I have something like this but eclips says has a red line under the class part of the statement. What do I have wrong?

It looks like this right now.


public class Add ([int width, [int height]]) {

}
Re: Get started creating libraries
Reply #5 - Nov 26th, 2006, 12:30am
 
Classes aren't functions. They contain them, just like in C++.

Code:

package mylib.myfunctionset;

public class myClass
{
public static int Add(int a, int b)
{
return(a+b);
}
}


and then in processing you could do:

Code:
import mylib.myfunctionset.*;

int a=myClass.Add(2,3);


Now this is a special case where you're just wanting to define functions, not objects, hence the static keyword.
Re: Get started creating libraries
Reply #6 - Nov 26th, 2006, 2:45am
 
Ok great that was exactly what I wanted to know
Re: Get started creating libraries (error)
Reply #7 - Nov 26th, 2006, 3:35am
 
I keep getting this error:

java.lang.UnsupportedClassVersionError: pFem/FemCalc (Unsupported major.minor version 49.0)
at java.lang.ClassLoader.defineClass0(Native Method)

I am using eclipse and I have a package called pFem and a class called FemCalc and a function called Add

This is the code

package pFem;

public class FemCalc {

  public static int Add(int a, int b)
  {
    return(a+b);
  }
 
}


This is what I'm typing in processing.

import pFem.*;

int a= FemCalc.Add(2,3);

I'm not sure what this error is from does any one have any ideas?
Re: Get started creating libraries
Reply #8 - Nov 26th, 2006, 9:04am
 
hi,
you get this error, because you compiled the code with a java version higher than 1.4. you will need to compile your library with java version 1.4 or lower. in eclipse you can set this in the menu project->properties->Java Compiler
Re: Get started creating libraries
Reply #9 - Nov 26th, 2006, 3:04pm
 
You just need to add a couple of switches to your javac command (should work with 1.4 and 1.5) :

-source 1.3 -target 1.1

Re: Get started creating libraries
Reply #10 - Nov 26th, 2006, 5:11pm
 
Thank you so much I've got it to work finaly!
one more quick question.
Reply #11 - Nov 26th, 2006, 10:57pm
 
ok I'm starting to understand this. I do have one more question. I want to be able to have data stored in multi-dimensional arrays go back and forth from processing to the library. For example use a loop in processing to dump data into an array that will be loaded into the function inside the java library.

this is the java code

package pFem;

public class FemCalc {

public static int arr[][];

   public static int Add(int a, int b, int arr[][])
   {
   arr[1][1] = a+b;
   return(arr[1][1]);
   }

}


This is what I am doing in processing


import pFem.*;

 
 FemCalc.arr[1][1] = 1;
 int a= FemCalc.Add(2,3);
 
print (a);

any help would be great.
Re: Get started creating libraries
Reply #12 - Nov 26th, 2006, 11:05pm
 
also how would I have a function with a for loop but rather then returning one single value having it return a multi-dimensional array the user could access from processing.
Re: Get started creating libraries
Reply #13 - Nov 26th, 2006, 11:10pm
 
which arr[][] are you wanting to use? You've defined 2.. one within FemCalc, and onother within femCalc.add.. one will shadow the other...

If you want to pass an arrayarray into Add, you need to pass it from in processing, currently you're only passing 2 ints, and no [][]...

If you don't then I think you may have some trouble, since you need to define the size of the arrays before using them, and that tends to only be possible if you actually create a FemCalc object, and then the static parts won't work as expected.

Re: Get started creating libraries
Reply #14 - Nov 26th, 2006, 11:12pm
 
frankb wrote on Nov 26th, 2006, 11:05pm:
also how would I have a function with a for loop but rather then returning one single value having it return a multi-dimensional array the user could access from processing.


public static int[][] myFunc(int a, int b)
{
 //do stuff, return an int[][]
}
Pages: 1 2