We are about to switch to a new forum software. Until then we have removed the registration on this forum.
firstly is this possible?
I am writing a program that has a bunch of different visualisations written in classes and I want the program to select a different random visualisation based on a timer. Furthermore I need the code to select slight variations within each visualisation once it is playing from a group. I assumed loading the classes into an array and selecting which one to use was the answer but I'm starting to think this is not the case.
Any pointers would be much appreciated, I'm not particularly clued up on how to use classes n objects beyond the basics.
Cheers Rich
Answers
interface
called Visual and have all those classesimplements
that.Yea I think there are multiple data types. They are all pretty different.
What do you mean by an empty interface? I started to have a look at nested classes and got totally lost!
this is just two of them...
OR... without having an array just call the classes
see
http://forum.processing.org/two/discussion/10678/menu-in-processing#latest
Thats similar to what im doing currently, each visualisation, which started as a separate sketch is a class in its own tab. I was hoping for a way to have the program select between them using an array but maybe its better to just use a number of if(....) commands like in the link.
I currently have thousands of lines of code we have been controlling with a midi pad and now need to make intelligent enough to run on its own.... which turns out to be really tough haha!
By empty
interface
I've meant it literally:Then you can make the classes you wanna store in the same container
implements
that:Something like this:
and then....?
?
Since all classes ultimately inherit from Java's Object class then you can use an Object collection (e.g. array or list) to store objects of any type.
The only restriction is that you when you retrieve an object an object from the collection you must see what class it is and cast it appropriately. See the code below.
This is not recommended for regular use but is useful if you want to simulate dynamic data typing.
For normal use Java introduced typed collections (generics) to avoid having to test and cast objects.
Instead of an empty interface, I would make a real one.
What all these objects have in common? Most likely they have something like a display() method, called when we want to show them.
Thus, the interface is significant, and offers an uniform way of handling them.
I have an example at https://github.com/PhiLhoSoft/Processing/tree/master/_SmallPrograms/DrawingInSequence
The code is old, before generics were supported in Processing, I use an untyped ArrayList but it should be typed with Drawer (as shown in the comment before the declaration).
I should update the sketch...
amazing! this looks like the route i wona go. It's going to need a lot of reading up again... if anyone could quickly explain the way to extend and implement classes that would be ace. I find the info in the processing reference a bit lacking if you don't understand everything!
At the moment the classes don't have too much in common but I am adding more functions as I get my head around the code. I've been producing visuals for a year now but with no time to code it "properly" so everything is a bit cobbled together.
Our next event is in a fortnight so there may not be time to get it all sorted but this a long way to helping me understand where to go next.
I can upload my full program if that would help you see whats going on and what needs to go where for the instances and classes...
for basic java language concepts then you can just use the java documentation:
https://docs.oracle.com/javase/tutorial/java/IandI/index.html
Updated the sketch to properly use generics...
Interfaces are kind of blueprint: they tell that any class implementing them must define the methods they have.
The advantage is that you can make a collection typed on that interface, where you can put any class that implement it.
And since the class must implement these methods, you are sure you can call them after getting them out of the collection.
To add on all the above. I used to have things like (out of my head):