问题
I've been working on a Processing project for a while, and now want to move it into Eclipse. I've installed Proclipse with my Eclipse environment.
I've a lot of files with the extension of ".pde". However the Proclipse files all ends with ".java". And there's a lot of dependency issues with all the pde files. How should I convert my project?
===============
Thanks everyone! There doesn't seem to be a one-button solution, I refactored all the code following an approach similar to George's answer. Plus changing all the file extensions from ".pde" to ".java".
回答1:
Jose's advice is pretty good. Proclipsing already makes creating a Processing project easy.
The easiest (but not cleanest way to get your Processing code running in eclipse would be taking these steps:
- Copy the code from the main tab onto the Proclipsing generated class which extends PApplet(removing the default/existing empty setup() and draw() functions) but still within class' scope ({})
- Make Processing functions from the pasted code(setup/draw/keyPressed/keyReleased/mousePressed/etc.) public (e.g.
public void setup(){//etc.
instead ofvoid setup(){//etc.
) - Paste the rest of the code from other tabs into the same java class generated by Proclipsing
- Append an 'f'(explicit float flag) to float type values(e.g.
3.0
becomes3.0f
) - If you're using libraries Proclipsing should help with that: right click the project and go to the Proclipsing project properties. If you've setup the correct path to Processing's folder(e.g. Documents/Processing), the extra libraries will be there so you just need to tick/enable them. Otherwise you will need to manually copy the .jar file of each library and paste into your project's lib folder, then right click the .jar files in eclipse and select Build Path > Add to Build Path
Update Here is a slightly simpler approach using Processing's Export Application feature. I will explain this workflow using the Daniel Shiffman's Boids example from Examples > Topics > Simulate > Flocking since it has multiple tabs and classes.
- Export the application. This will generate an application folder including a source folder with Flocking.java (as I mentioned above, Processing actually bundles all your tabs into a single .java class)
- Create a new Proclipsing project
- Paste the code from the Processing generated class into the Proclipsing generated class after the
package
statement.(a package is a folder can hold multiple classes, so use this to keep things tidy/nicely organized in folders based on what the classes do) - Update the static main method at the bottom to use the fully qualified class name (so the class name prefixed by the package name)
At this point hopefully most of the errors should have disappeared. Try to run your code as a Java Application now.
The problem is at this point you have one massive class which is still hard to maintain, probably the reason you're moving to eclipse in the 1st place. Now would be the time to refactor(restructure your code) and luckily eclipse has some great tools for that. If you see repetitive code, that's a great candidate for a function. You can try selecting that code , right clicking then choosing Refactor > Extract Method. The values that change in the repeated code can be extracted as arguments/parameters.
Inner classes should move to new .java files and if you use Processing specific functionalities in those classes you've got multiple options:
- Passing the PApplet as an argument to those classes (like the Jose's Processing in Eclipse article link suggests)
- Using PApplet's static methods(e.g.
PApplet.map()
instead ofmap()
) - Implementing PConstants in a class
- Passing the renderer(PGraphics) if a class only deals with drawing in Processing
Most importantly though you should familiarize yourself a little bit with Java (compiling a HelloWorld program from scratch which will shine a light on Processing's inner classes) and especially some OOP concepts(composition, inheritance and perhaps a few basic design patterns (such as Visitor or MVC) in the future. This of course, if you're new to these concepts :)
Update the simplified updated instructions for Proclipsing are now available as a video here. The first two minutes illustrate the basic process and the rest covers some of the refactoring concepts mentioned above.
回答2:
Check this link http://www.learningprocessing.com/tutorials/processing-in-eclipse/ here you can find a more detail explanation of everything concerning this topic.
The most important thing to know when moving from the Processing IDE to another one like Eclipse, is that in Processing all classes are treated as inner classes, they are classes inside of the larger PApplet.
First you need to import the Processing library inside you Eclipse project, then you have 2 ways to make you code work.
You can extend from PApplet in your main class and add all you Processing code there, including all your other clases like inner clases.
Or you could work those clases separately by calling the same instance of PApplet every time you want to accede Processing stuff.
来源:https://stackoverflow.com/questions/25509871/moving-processing-project-into-eclipse