Processing触ってみた

諸般の事情でちょっとだけProcessingを触ってみました。


Source code: Balls.pde

Processingはグラフィカルな表現に特化したプログラミング言語で、Javaを元にしています。かなり単純化されているので、レゴブロックを組み立てるようにガチャガチャと作り上げられます。Processingの開発者の言う「ソフトウェア・スケッチブック」というコンセプトが実現されている感じを受けました。

本家のリファレンス教材が丁寧に作られてあったり、良質な学習サイトもたくさんあり、すんなりと理解できました。

参考サイト

[sourcecode language=”java”]
int num = 130;
color bg = color(#FFFFFF);

Ball[] ballList = new Ball[num];

// コンストラクタのように最初に実行される関数
void setup() {
size(465, 465);
noStroke();
smooth();
for(int i=0; i<num; i++) {
ballList[i] = new Ball();
}
}

// EnterFrameのように毎フレーム実行される関数
void draw() {
fill(bg, 0xFF*0.2);
rect(0, 0, width, height);
ballList[frameCount%num].reset(mouseX, mouseY);
for(int i=0; i<num; i++) {
ballList[(i+frameCount)%num].update();
}
}

// MouseDownのようにマウスボタンが押下げられると実行される関数
// クリックする毎に背景色を変える
void mousePressed(){
if(bg == -1){
bg = color(#000000);
}else{
bg = color(#FFFFFF);
}
fill(bg);
rect(0, 0, width, height);
}

// インナークラス
class Ball{
float x = 0;
float y = 0;
float dx = 0;
float dy = 0;
int count = 0;
float r = 80;
color c;
Ball(){
reset(0,0);
}
void reset(float tx,float ty){
float rotation = random(0, 2*PI);
x = tx;
y = ty;
dx = cos(rotation)*3;
dy = sin(rotation)*3;
count = 255;
c = color(random(0, 255), random(0, 255), random(0, 255));
}
void update(){
count -= 2;
count = max(count, 0);
x += dx;
y += dy;
fill(c, count*0.4*random(0.3, 1));
ellipse(x, y, r, r); // 円を描画
ellipse(x, y, r/1.62, r/1.62); // 円を描画
}
}
[/sourcecode]