NCF参数化建筑论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 13231|回复: 6
打印 上一主题 下一主题

[技术探索] 用eclipse写processing建立多个classes

[复制链接]
跳转到指定楼层
1m
发表于 2011-7-9 00:37:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
虽然还没有弄明白~也知道其实用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的~再次声明,我只想弄明白~我知道没太必要,我闲的找抽~欢迎来抽!
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享分享
2m
发表于 2011-7-9 15:26:16 | 只看该作者
按java语法来就是啦 不明白看看java的oop一章 processing里面只有一个PApplet类 所以不好用嘛
3m
发表于 2011-7-9 19:53:10 | 只看该作者
不错!我一开始条件反射的想extend,后面看明白了。
4m
发表于 2011-7-11 10:09:49 | 只看该作者
深奥啊,不懂得啊
5m
发表于 2011-12-30 20:32:08 | 只看该作者
好的,强大
6m
发表于 2012-3-18 09:57:09 | 只看该作者
不错的帖子!!!
7m
发表于 2012-4-3 23:52:24 | 只看该作者
能翻译下就号了

小黑屋|手机版|NCF参数化建筑论坛 ( 浙ICP备2020044100号-2 )    辽公网安备21021102000973号

GMT+8, 2024-11-23 20:01 , Processed in 0.065057 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表