I'm making an Android app using Processing, and have decided to port my code to Eclipse. I guess there are a lot of differences between Processing and pure Java, because right off the bat I am gett开发者_Go百科ing errors that make little sense to me. For example:
int inactiveThreshold = 300 + int(random(-100, 100));
int randomPointInterval = int(random(300, 500));
I get the error "Syntax error on token "int", delete this token"
As for the random function, I extend PApplet and have imported processing.core.* so I don't see why that wouldn't work properly.
Also getting errors on anything referencing a color that I created:
back.drawBackground("shadow", backgroundColor);
back.drawBackground("front", backgroundColor);
"Color cannot be resolved to a type", despite initializing backgroundColor as an integer:
int backgroundColor = color(100, 100, 120);
There's also a function in the main class that handles mouse movement, and when I make a reference to it says that the function is undefined.
These errors are just in the main class, so I figured I should tackle that before anything else. I've mainly been following this for porting the code:
http://www.learningprocessing.com/tutorials/processing-in-eclipse/
I haven't done much in pure Java so there are probably a lot of differences that Processing's precompiler handles that I'm not noticing.
For int casting: do
int i = (int)(valueToCase);
For randoms:
java.util.Random rand = new java.util.Random();
// can't do random(min,max) - instead, do
int randNum = rand.nextInt(max-min) + min;
Any time you see java.blah.ClassName, you can do this so it's not cluttered with java.blah qualifiers.
import java.blah.ClassName;
ClassName cn = new ClassName...
I would be surprised if it works very well to be honest. Processing is not optimized screen devices and that will cause you some problems. Also far as I understand it runs on the Java virtual machine on top of the JVM. This is quite different from running on top of the Dalvik VM that is used in Android.
However there seems to be quite a bit of support for it. I would check that community directly. http://wiki.processing.org/w/Android
Turns out there's a nice tutorial on how to get a Processing app to run in a pure Java environment (assuming that's what you want): http://processing.org/learning/eclipse/
In short, you:
import processing.core.PApplet;
then pass a reference to PApplet into any classes where you want to use "native" processing functions. That way, where you previously said:
fill(255,100);
you can now say:
pApplet.fill(255, 100);
I should also mention there's an active Adroid processing forum here: https://forum.processing.org/android-processing -- there are people there who have already gone down this road.
精彩评论