虽然还没有弄明白~也知道其实用processing写的话方便,但是为了彻底搞明白,我还是选择了钻牛角尖。以下是官网方法。完全不理解。高人大牛前来点拨~叮!
Processing in Eclipse with Multiple ClassesTake a look at this example Processing sketch. The example is object-oriented and contains a class called "Stripe." In Processing, all classes are treated as "inner classes," meaning they are not individual entities unto themselves, but rather are classes inside of the larger PApplet. This is why you can do anything you can do in a PApplet inside of the Stripe class. You can draw onto the window calling rect() and access the PApplet variables such as width and height. To get this example working in Eclipse, it's perfectly reasonable to just copy it in as is and include the Stripe class code below setup() and draw() inside the parent PApplet class. However, the inner class methodology can quickly become unwieldy in the case of larger applications with lots and lots of classes.
And so we find ourselves in need of a means for creating separate Java classes in Eclipse that we can use in conjunction with a Processing-based application.
First thing first, we can put all the code that is not the Stripe class in a Java class like with the first example.
import processing.core.*;
public class MyProcessingSketch extends PApplet {
// An array of stripes
Stripe[] stripes = new Stripe[50];
public void setup() {
size(200,200);
// Initialize all "stripes"
for (int i = 0; i < stripes.length; i++) {
stripes = new Stripe();
}
}
public void draw() {
background(100);
// Move and display all "stripes"
for (int i = 0; i < stripes.length; i++) {
stripes.move();
stripes.display();
}
}
}
Second, we create a new class called Stripe.
FILE --> NEW --> CLASS. Name it "Stripe." Click "finish."
If we add the Processing code, Eclipse will give us a bunch of errors.
The errors are all on lines where we call a Processing function (such as rect()) or reference a Processing variable (such as width). This is because a Stripe is not a PApplet and knows nothing about PApplets! We might be tempted to have Stripe extend PApplet, but this would be a fatal error. A Stripe is not a PApplet. A Stripe is a rectangular object that we want to draw onto a PApplet. What PApplet do we want to draw onto? The PApplet that is MyProcessingSketch. Instead of extending PApplet, we simply want to tell a Stripe object about a MyProcessingSketch.
public class Stripe {
PApplet parent; // The parent PApplet that we will render ourselves onto
In the constructor, we initialize the parent:
Stripe(PApplet p) { parent = p;
And then anytime we want to call a Processing function, we access it via the parent variable.
// Draw stripe void display() { parent.fill(255,100); parent.noStroke(); parent.rect(x,0,w,parent.height); }
Here's the whole thing together:
import processing.core.PApplet;
public class Stripe {
float x; // horizontal location of stripe
float speed; // speed of stripe
float w; // width of stripe
boolean mouse; // state of stripe (mouse is over or not?)
PApplet parent; // The parent PApplet that we will render ourselves onto
Stripe(PApplet p) {
parent = p;
x = 0; // All stripes start at 0
speed = parent.random(1); // All stripes have a random positive speed
w = parent.random(10,30);
mouse = false;
}
// Draw stripe
void display() {
parent.fill(255,100);
parent.noStroke();
parent.rect(x,0,w,parent.height);
}
// Move stripe
void move() {
x += speed;
if (x > parent.width+20) x = -20;
}
}
Finally, the last change we have to make is how we call the constructor. Previously, we had:
stripes = new Stripe();
But now when we construct a Stripe object, we must pass in a reference to a PApplet. And here, the PApplet is this, a reference to ourselves, the PApplet we are right now, MyProcessingSketch.
stripes = new Stripe(this);
Here is the full project folder for download.
Another important note. The Processing "color" primitive does not exist in Java. In fact, in Processing a "color" is really just an integer (32 bits with red, green, blue, and alpha components). Processing translates "color" to "int", but Eclipse won't do that for you. So instead of saying:
color pink = color(255,200,200);
you should say:
int pink = color(255,200,200);and if you are in another class and have to refer to the "parent" PApplet:int pink = parent.color(255,200,200);
上面有图片的processing正点代码,下面黑体是eclipse的~再次声明,我只想弄明白~我知道没太必要,我闲的找抽~欢迎来抽! |