We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am using a touch interface library someone in my lab wrote. I have never used Processing in this way and his library uses a variable who's type is the name of the sketch. For example, if I named my sketch "Foo" I would need to use type Foo:
Foo applet;
Later in the code it is referenced a few times, here is an example:
applet.touchDown(ID, xPos, yPos, xWidth, yWidth);
It works (admittedly I don't know the details of how it does) but I was wondering if there was a way to make this work independently of the name of the sketch.
Answers
"Foo" is the class name, "applet" is the name of an object, instance of Foo. You don't have to understand how the method (such as touchDown) works. Meanwhile, they all have nothing to do with the name of sketch.
Take a random sketch, export it, look at the generated .java file. It shows that Processing generates a Java class named after the sketch name.
That's why you have some constraints on these names: they cannot start with a number, they cannot have spaces, you should avoid special characters, etc.
So, basically, you need this name to reference the generated applet: if you use two applets, you have to distinguish them, for example.
You can
PApplet applet;
of course, but then you have only a generic applet, you cannot use functions defined in it, only the Processing functions.
luhai, it does not work when it has a different name than the sketch.
PhiLho, originally it used PApplet. The problem was that this meant it had to be cast as the name of the sketch ("Foo" or whatever) instead of the code I pasted above. For example:
((Foo)applet).touchDown(ID, xPos, yPos, xWidth, yWidth);
As this appeared a number of times (more than one function), it seemed using "Foo" instead of PApplet made sense so that only one line of code had to be changed when copy / pasting the code from the library example. I was hoping to remove this last dependency on the sketch name.
Edit: I should probably point out just for clarity that applet is initialized as:
applet = this;
in setup()
I'm not very sure of it. But once I've read that a ".java" file ought to be named after the solely public interface/class within it! L-)
Well, at least, when there's 1 public interface/class in there! :-j
Besides 1 public interface/class, we could have others w/ the so-called "default" access modifier. That is, w/o using any keyword!
"I'm not very sure of it"
If there is a public class or interface (or enum?) in the .java file, the file must have the same name, indeed. Otherwise, a file can contain a protected or package protected class / interface / etc.
@asimes yes, that's what I meant: if you use a generic PApplet, you only have a generic Processing behavior. If you need to call applet-specific methods, you need the name of the specific applet, makes sense, no?
Now, you can create an interface defining a behavior common to several applets, and make the applets to implement this interface. So you can different applets / implementations and need to name only the interface, not the individual applets.
It depends on your real need, I am not sure to understand why you are annoyed to use the name of the sketch.
The library is intended to be downloaded and ready to play with. It is not a big deal to use the name of the sketch, but it would have been nice to not have to modify anything copy / pasting from the example code provided.
I've made a test unit and it shows that we can have as many top interfaces, classes and enums as we want,
as I was rightly thinking so! >-) Well, as long as they use that "default" package access! 8-X
Besides that, we can have either 1 or none
public
top interface/class/enum. :-\"Check that out for yourselves: :D
"Multi_Top_Class.pde":
"MultiTop.java":
@asimes -> AFAIK, generic libraries demand a PApplet reference of the current sketch to be passed on them. :-B
Like this 1, for example:
Doing so, an external library doesn't need to know the sketch's file name.
As long as it uses that reference to interface itself w/ the sketch! >:/