Loading...
Logo
Processing Forum
Hey im trying to make a class that controls the creation of video objects in my sketch.

Is it possible to pass a string to make up part of the required syntax?

for example.

instead of

               
Copy code
  1. myMovie = new Movie(this, "totoro.mov");

but 

               
Copy code
  1. Name = new input(this, w, h, FPS);

where Name, input, w, h , FPS are variables passed through the constructor during the creation of the object.

Here is my code i have written so far.

               
Copy code
  1. class videostuff{
  2. String Name;
  3. String input;
  4. int FPS;
  5. int w;
  6. int h;
  7. videostuff(String _Name, String _input, int _FPS, int _Width, int _Height ){
  8. w = _Width;
  9. h = _Height;
  10. FPS = _FPS;
  11. Name = _Name;
  12. input = _input;
  13. input Name;
  14. Name = new input(this, w, h, FPS);
  15. }
  16. }

Replies(6)

When we speak about Object Oriented Programming, at leat in Java, you can pass whatever Object or datatype to another class as long as you treat that data with appropiate methods ans access the Object contents and/or member fields as it is supposed to be accessed.

There is no problem with the thing you are trying to accomplish however there is a mistake in the constructor you give:
In your class you have a String called Name.

You should not (must not) use the same variable name to create an input object.

So these lines:
Copy code
  1. Name = _Name;
  2. input Name = new input(this,w,h,FPS);
  3. // see the problem?
It might give you a compilation or runtime error and your program may not work at all because both objects (A String and an input) are called exactly the same. I think this:

Copy code
  1. Name = _Name;
  2. input inputName = new input( this, w, h, FPS);

Should work just fine.

Don't worry about the Objects you pass to methods and constructors, they'll work as long as you use them propperly.
you forgot about

Copy code
  1.  input = _input;
  2.  input Name;
  3.  Name = new input(this, w, h, FPS);
tbh i actually think that what he is trying to do is to send the class name and variable name as arguments which is not only silly and ridiculous but also (As far as i know) impossible
If you're right, then the Java "Class" class works fine to get a class' name and store it inside a String. It, of course, is not impossible.
no i mean i think he is trying to do it the other way around, turn the string into a class and variable name, look at his code again
There are at least two ways to do that.
The first one is to use reflection, ie. transform the name of a class to a class instance. This is complex, slow and prone to errors (if the name is incorrect).
The second one, simpler, is possible if you know in advance all classes the library can use. You can do something like:
Copy code
  1. if (input.equals("Movie"))
  2.   instance = new Movie(this, param);
  3. else if (input.equals("Video"))
  4.   instance = new Video(this, param);
although instance must be of a super-type of all possible classes, or just an Object.

For the record, Processing uses the first solution in createGraphics(). You can take a look at the source code (PApplet.java) to see how it is done. I think it is done this way to allow custom PGraphics instances (I used this to make specialized PDF graphics). Here, the common super-class is PGraphics, it works only if the new class extends it.
hmmm interesting stuff