openrndr
openrndr copied to clipboard
Add example using openrndr from Java
Motivation
Edwin asked for this
Proposed Solution
package jbe.openrndr.run;
import java.nio.ByteBuffer;
import java.security.SecureRandom;
import org.openrndr.ApplicationKt;
import org.openrndr.Configuration;
import org.openrndr.Program;
import org.openrndr.color.ColorRGBa;
import org.openrndr.extra.noise.SimplexNoise1DKt;
import static java.lang.Math.abs;
public class OpenrndrJavaDemo extends Program {
private int seed;
@Override
public void setup() {
super.setup();
seed = ByteBuffer.wrap(SecureRandom.getSeed(4)).getInt();
}
@Override
public void draw() {
drawer.clear(ColorRGBa.Companion.getPINK());
drawer.setFill(ColorRGBa.Companion.getWHITE());
drawer.circle(drawer.getBounds().getCenter(),
abs(SimplexNoise1DKt.simplex(seed, getSeconds())) * getHeight() * 0.5);
}
public static void main(String[] args) {
var c = new Configuration();
c.setWidth(1200);
c.setHeight(1200);
ApplicationKt.application(new OpenrndrJavaDemo(), c);
}
}
Detailed Design
This is a short example similar to the first one from the Guide that illustrates a few principles:
- openrndr tends to prefer closures to inheritance, but this is not as idiomatic in Java as in Kotlin. Structuring around Program setup and draw methods lets us offer a structure familiar to Java users (even more familiar if they come from Processing).
- Kotlin companion objects are exposed to Java via a .Companion static field
- Top-level functions are exposed to Java as static methods in a class with Kt appended, so Application's functions are found in ApplicationKt; SimplexNoise1D's functions are found in SimplexNoise1DKt.
Open Questions
No response
Help make it happen!
- [X] I am willing to submit a PR to implement this change.
- [ ] I am willing to submit a PR to implement this change, but would need some guidance.
- [ ] I am not willing to submit a PR to implement this change.
Here is a direct translation of the Kotlin template that does not require https://github.com/openrndr/openrndr/pull/270:
import kotlin.Unit;
import org.openrndr.ApplicationBuilderKt;
import org.openrndr.ExtensionStage;
import org.openrndr.color.ColorRGBa;
import org.openrndr.draw.ColorTransformsKt;
import org.openrndr.draw.FontMapKt;
import static java.lang.Math.cos;
import static java.lang.Math.sin;
import static org.openrndr.draw.ColorBufferJVM.loadImage;
import static org.openrndr.draw.LoadFontKt.loadFont;
public class TemplateProgram {
public static void main(String[] args) {
ApplicationBuilderKt.application(build -> {
build.configure(c -> {
c.setWidth(768);
c.setHeight(576);
return Unit.INSTANCE;
});
build.program((p, continuation) -> {
var image = loadImage("data/images/pm5544.png", null, null);
var font = loadFont(p,
"data/fonts/default.otf",
64.0,
FontMapKt.getDefaultFontmapCharacterSet(),
p.drawer.getContext().getContentScale());
p.extend(ExtensionStage.BEFORE_DRAW, userDraw -> {
p.drawer.getDrawStyle().setColorMatrix(ColorTransformsKt.tint(ColorRGBa.Companion.getWHITE().shade(0.2)));
p.drawer.image(image);
p.drawer.setFill(ColorRGBa.Companion.getPINK());
p.drawer.circle(cos(p.getSeconds()) * p.getWidth() / 2.0 + p.getWidth() / 2.0,
sin(0.5 * p.getSeconds()) * p.getHeight() / 2.0 + p.getHeight() / 2.0, 140.0);
p.drawer.setFontMap(font);
p.drawer.setFill(ColorRGBa.Companion.getWHITE());
p.drawer.text("OPENRNDR", p.getWidth() / 2.0, p.getHeight() / 2.0);
return Unit.INSTANCE;
});
return Unit.INSTANCE;
});
return Unit.INSTANCE;
});
}
}
Closing this issue. OPENRNDR has become more dependent on Kotlin language features and support for Java is not in enough in demand to justify the effort.