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.
IndexProgramming Questions & HelpSyntax Questions › Objects in separate files
Page Index Toggle Pages: 1
Objects in separate files (Read 799 times)
Objects in separate files
Aug 27th, 2009, 4:51am
 
My Java tutors were always quite emphatic about how objects should always be kept in their own files. This seems like a hell of a stupid newbie question, but is this even possible in Processing

The main Objects tutorial doesn't mention the word 'file' anywhere, and the obvious way of doing it (including a .pde file with the same name as the class it defines) doesn't seem to work at all - 'Cannot find a class or type named "whatever"'.

Thanks for any help - sure I can just include all the objects in the same text file, but do I really need to
Re: Objects in separate files
Reply #1 - Aug 27th, 2009, 5:23am
 
I don't think you should get any errors if you don't store a class in a separate file in Processing, but it's still good practice, particularly in terms of code readability.

In order to do this simply create a new tab for the class in the processing IDE: each tab is saved as a separate file in the sketch folder.  Note that you can put any code into a separate tab (e.g. functions) and your sketch should still work just fine.  Also note that Processing isn't exactly the same as pure Java - any class you write is treated as a subclass of Processing, so not everything behaves as you might expect from Pure Java.
Re: Objects in separate files
Reply #2 - Aug 27th, 2009, 5:51am
 
Also, with more full-featured IDEs, you're going to have file browsers that keep track of all your folders and classes and stuff for easy switching between them. This makes it almost stupid to not put all your classes in separate files. But Processing is different, and the stripped down IDE is designed to make it easy to code quickly with a simple interface. I always put all of my classes in separate tabs, as blindfish suggested, and that will achieve the same effect as what your teachers are wanting.
Re: Objects in separate files
Reply #3 - Aug 27th, 2009, 8:24am
 
Some precisions on blindfish's good info:
  • By "subclass of Processing", you must understand "nested class of the main Processing class" (which is named as the sketch name).
  • When you create a new tab, by default (no extension) it creates a .pde file, so its content will just be added inside the main class.
  • If you give a .java extension, it will compile the files without pre-processing them (no color types nor literals, for example), and the classes will not be nested. Side advantage: you can use the Java 1.5 syntax there (and double literals, etc.). Little inconvenience: beside the lack of pre-processing, you don't have direct access to Processing functions, you have to get a PApplet instance and use it.
Re: Objects in separate files
Reply #4 - Aug 31st, 2009, 3:56am
 
Thanks a lot everyone, it makes *far* more sense now. I guess if I'd managed to find this bit of the Reference on the environment, I'd've known that.

I must say, I find the Tab menu controls remarkably unintuitive - I'd expect to be able to right-click on a tab to bring up options like renaming or deleting it, even if the makers have decided the whole thing needs to be de-emphasised to avoid confusing newbies...
Page Index Toggle Pages: 1