Previously I was excited to use java8 syntax in processing ide using Sampottingers development version of processing. However things are moving apace, and if you are prepared to build your own copy of latest version, you will find support for java11 syntax. So my first example becomes:-
import hype.*;
void setup() {
size(640, 640);
H.init(this);
H.background(#242424);
var pool = new HDrawablePool(100);
pool.autoAddToStage()
.add(new HRect(50))
.onCreate(
((var obj) -> {
var d = (HDrawable)obj;
d.strokeWeight(1)
.stroke(#999999)
.fill(#202020)
.loc((int)random(width), (int)random(height));
}
)
)
.requestAll();
H.drawStage();
noLoop();
}
void draw() {
}
Note we do not need to pre-declare pool above since it is only use in the setup scope. This is not true for second example that becomes:-
import hype.H;
import hype.HCanvas;
import hype.HDrawable;
import hype.HDrawablePool;
import hype.HRect;
import hype.extended.behavior.HOscillator;
import hype.extended.behavior.HTimer;
import hype.extended.colorist.HPixelColorist;
HDrawablePool pool;
HPixelColorist colors;
HCanvas canvas;
void setup() {
H.init(this);
H.background(0xff000000);
H.use3D(true);
colors = new HPixelColorist("gradient.jpg");
canvas = new HCanvas(P3D).autoClear(true);
H.add(canvas);
pool = new HDrawablePool(1000);
pool.autoParent(canvas)
.add(new HRect().rounding(10))
.onCreate((var obj) -> {
int i = pool.currentIndex();
var d = (HDrawable) obj;
d.noStroke()
.size((int) random(40, 80), (int) random(60, 80))
.loc((int) random(width), (int) random(height))
.anchorAt(H.CENTER)
.obj("xo", new HOscillator()
.target(d)
.property(H.X)
.relativeVal(d.x())
.range(-(int) random(5, 10), (int) random(5, 10))
.speed(random(.005f, .2f))
.freq(10)
.currentStep(i)
)
.obj("ao", new HOscillator()
.target(d)
.property(H.ALPHA)
.range(50, 255)
.speed(random(.3f, .9f))
.freq(5)
.currentStep(i)
)
.obj("wo", new HOscillator()
.target(d)
.property(H.WIDTH)
.range(-d.width(), d.width())
.speed(random(.05f, .2f))
.freq(10)
.currentStep(i)
)
.obj("ro", new HOscillator()
.target(d)
.property(H.ROTATION)
.range(-180, 180)
.speed(random(.005f, .05f))
.freq(10)
.currentStep(i)
)
.obj("zo", new HOscillator()
.target(d)
.property(H.Z)
.range(-400, 400)
.speed(random(.005f, .01f))
.freq(15)
.currentStep(i * 5)
);
}
)
.onRequest((var obj) -> {
var d = (HDrawable) obj;
d.scale(1).alpha(0).loc((int) random(width), (int) random(height), -(int) random(200));
var xo = (HOscillator) d.obj("xo");
xo.register();
var ao = (HOscillator) d.obj("ao");
ao.register();
var wo = (HOscillator) d.obj("wo");
wo.register();
var ro = (HOscillator) d.obj("ro");
ro.register();
var zo = (HOscillator) d.obj("zo");
zo.register();
}
)
.onRelease((var obj) -> {
var d = (HDrawable) obj;
var xo = (HOscillator) d.obj("xo");
xo.unregister();
var ao = (HOscillator) d.obj("ao");
ao.unregister();
HOscillator wo = (HOscillator) d.obj("wo");
wo.unregister();
var ro = (HOscillator) d.obj("ro");
ro.unregister();
var zo = (HOscillator) d.obj("zo");
zo.unregister();
}
);
new HTimer(50)
.callback((var obj) -> {
pool.request();
}
);
}
void draw() {
for (var d : pool) {
d.loc(d.x(), d.y() - random(0.25f, 1), d.z());
d.noStroke();
d.fill(colors.getColor(d.x(), d.y()));
if (d.z() > -10 && d.z() < 10) {
d.fill(0xffFFFFCC); // if the z axis hits this range, change fill to light yellow
}
if (d.y() < -40) {
pool.release(d);
}
}
H.drawStage();
}
void settings() {
size(640, 640, P3D);
}
So you get to save a bit of typing, still not as elegant as ruby though.