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 › constucting object-libraries
Page Index Toggle Pages: 1
constucting object-libraries (Read 3814 times)
constucting object-libraries
Nov 7th, 2006, 12:35am
 
hello there, i read some stuff about the library construction for the processing but i think there is a leek about the object construction libraries.
for example:
i would like to create an object Node, with the constructor Node(Object data, Node next) i make the class file i make it .jar i put in the library folder i try to import but then an error appears i will post it tomorrow.
any help or tutorial?
Re: constucting object-libraries
Reply #1 - Nov 7th, 2006, 3:27pm
 
There is a little bit of info about making Processing libraries at my site:

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

But most of the info you need should be in the howto.txt in the libraries folder.

What is the error you are getting?

Dan
Re: constucting object-libraries
Reply #2 - Nov 10th, 2006, 11:20pm
 
thank you for your response...
check out my steps

1. this is my  SLinkedList.java file i compile and get the
Node.class, SLinkedList.class and ListEmptyException.class files:
Quote:
package SLinkedList;

import java.io.*;

class SLinkedList {

private Node firstNode,lastNode;


public SLinkedList(){


firstNode = lastNode = null;

}


public Node getFirst(){


return firstNode;

}


public Node getLast(){


return lastNode;

}


public boolean isEmpty(){


return(firstNode==null);

}


public void insertFirst(Object newItem){


if (isEmpty()) firstNode = lastNode = new Node(newItem,null);


else firstNode = new Node(newItem,firstNode);

}


public void insertLast(Object newItem){


if (isEmpty()) firstNode = lastNode = new Node(newItem,null);


else lastNode = lastNode.next = new Node(newItem,null);

}


public Object removeFirst() throws ListEmptyException {


Object removeItem;




if (isEmpty()) throw new ListEmptyException ("Empty list.");


removeItem=firstNode.item;


if (firstNode.equals(lastNode)) firstNode = lastNode = null;


else firstNode = firstNode.next;


return removeItem;

}


public Object removeLast() throws ListEmptyException {


Object removeItem;




if (isEmpty()) throw new ListEmptyException ("Empty list.");


removeItem=lastNode.item;


if (firstNode.equals(lastNode)) firstNode = lastNode = null;


else {



Node current = firstNode;



while (current.next != lastNode) current=current.next;



lastNode = current;



current.next = null;


}





return removeItem;

}


public void printList() throws ListEmptyException{


if (isEmpty()) throw new ListEmptyException ("Empty list.");


else {



Node current=firstNode;



while(current != null) {




System.out.println(current.item.toString()+" ");




current = current.next;



}



System.out.println("\n");


}

}
}
class Node{


Object item;

Node next;


public Node(){


this(null,null);

}


public Node(Object it, Node n){


item = it;


next = n;

}


public void setItem(Object newItem){


item = newItem;

}


public Object getItem(){


return item;

}


public void setNext(Node n){


next = n;

}


public Node getNext(){


return next;

}
}
class ListEmptyException extends RuntimeException {

public ListEmptyException (String err){


super(err);

}
}



2.i go to the directory which contains the .class files and run Quote:
jar cvf SLinkedList.jar Node.class,ListEmptyException.class, SLinkedList.class

and i get the SLinkedList.jar file with the META-INF in it

3. I go to the processing\lybraries folder i create a folder  named SLinkedList and a sub-folder named library and put the  SLinkedList in the SLinkedList\library\SLinkedList.jar

4. i open processing go to import library i select slinkedlist but no text appears in the editor...i type Quote:
import Slinkedlist.*;
i get an error i type other different thing and even when it finds the library when i try to create a Node object i get an other error
HELLPPPPPPP
thnx
Re: constucting object-libraries
Reply #3 - Nov 11th, 2006, 12:54pm
 
If that's the jar command you're running, I think you compiled it wrong.

You have to have "-d ." in your javac line to get it to build a dir structure with the class files in.

So you'll end up with SLinkedList\SlinkedList.class, SLinkedList\Node.class etc, then your jar line should be

jar cvf SLinkedList.jar SLinkedList\*.*
Re: constucting object-libraries
Reply #4 - Nov 12th, 2006, 6:27pm
 
i dont think this gonna work..
have you tried???
Re: constucting object-libraries
Reply #5 - Nov 12th, 2006, 9:13pm
 
Yes I've tried, it's really not that hard...

Code:
c:\jdk\bin\javac -source 1.3 -target 1.1 -d . -classpath   
c:\processing-0121\lib\core.jar SLinkedList.java

c:\jdk\bin\jar -cf SLinkedList.jar SlinkedList\*.class


Then put SLinkedList.jar into processing-0121\libraries\SLinkedList\library\

And it'll work fine.
Re: constucting object-libraries
Reply #6 - Nov 14th, 2006, 11:16am
 
when i type your command in cmd line i get some errors about the versions etc...
i dont know the reason to type the classpath caz i dont use any core library in my source as you can see..
anyway i compiles
it imports the library BUTTTTTT
when i try to create a Node object in a processing sketch guess what I GET AN ERROR!!!
it says something about that it can access caz is default ...i dont get it...
so i tried to add the public statement before the class Node {......}
when i try to compile it i get an error (from the javac) that the Node should be delcared in a seperate file ...
ok
i do it
then i try to compile SlinkedList.java (without the Node source in it) and guess what AN ERROR!it says that it can find the Node.class although it is in the same directory...
what else to say
can you please SEND my the SLinkedList.jar file YOU compiled to see if it works???
Re: constucting object-libraries
Reply #7 - Nov 14th, 2006, 2:16pm
 
The one I built will do the same that the one you built does, because the code in it is saying the same thing.

I've no idea why you want to use this particular linked list as a library, but it'd be much much easier to just use that .java file as part of the sketch... create a tab called "SLinkedList.java" and paste the contents of the file (minus the package and import lines into it, and then try to create a Node object.
Re: constucting object-libraries
Reply #8 - Nov 15th, 2006, 2:49pm
 
i think libraries founded so people dont have to copy paste the code again again to new files...
i thnk you anyway!!!
do you know where can i find any more help???
Smiley
Re: constucting object-libraries
Reply #9 - Nov 16th, 2006, 12:41pm
 
Nope. Libraries are founded to make life simpler. A well written library should stop the programmer from having to think about the mechanics of what he is doing and get on with the rest of their work. And it gives you a tiny file bursting with code to share.

Your code works. Always type "void setup(){}" at the top of your code if you're looking for exceptions, I don't know why but if you don't it can break some types of code.
Code:

void setup(){}

class SLinkedList {
private Node firstNode,lastNode;
public SLinkedList(){
firstNode = lastNode = null;
}
public Node getFirst(){
return firstNode;
}
public Node getLast(){
return lastNode;
}
public boolean isEmpty(){
return(firstNode==null);
}
public void insertFirst(Object newItem){
if (isEmpty()) firstNode = lastNode = new Node(newItem,null);
else firstNode = new Node(newItem,firstNode);
}
public void insertLast(Object newItem){
if (isEmpty()) firstNode = lastNode = new Node(newItem,null);
else lastNode = lastNode.next = new Node(newItem,null);
}
public Object removeFirst() throws ListEmptyException {
Object removeItem;
if (isEmpty()) throw new ListEmptyException ("Empty list.");
removeItem=firstNode.item;
if (firstNode.equals(lastNode)) firstNode = lastNode = null;
else firstNode = firstNode.next;
return removeItem;
}
public Object removeLast() throws ListEmptyException {
Object removeItem;
if (isEmpty()) throw new ListEmptyException ("Empty list.");
removeItem=lastNode.item;
if (firstNode.equals(lastNode)) firstNode = lastNode = null;
else {
Node current = firstNode;
while (current.next != lastNode) current=current.next;
lastNode = current;
current.next = null;
}
return removeItem;
}
public void printList() throws ListEmptyException{
if (isEmpty()) throw new ListEmptyException ("Empty list.");
else {
Node current=firstNode;
while(current != null) {
System.out.println(current.item.toString()+" ");
current = current.next;
}
System.out.println("\n");
}
}
}
class Node{
Object item;
Node next;
public Node(){
this(null,null);
}
public Node(Object it, Node n){
item = it;
next = n;
}
public void setItem(Object newItem){
item = newItem;
}
public Object getItem(){
return item;
}
public void setNext(Node n){
next = n;
}
public Node getNext(){
return next;
}
}
class ListEmptyException extends RuntimeException {
public ListEmptyException (String err){
super(err);
}
}

Incidentally, Java already provides a LinkedList class. Plus many Processing users use Vector and ArrayList quite effectively. To implement a more complex linked list you could simply use classes that refer to other instances of said classes and the collection would take care of the group as a whole.

If you're having trouble compiling from the command line you might try dowloading Eclipse. It's free, a bit daunting at first but a much easier way of building libraries and there's a thread on the board for total beginners.
Page Index Toggle Pages: 1