Openrndr from Java

Hi all,

I’m coming to Openrndr from Processing. Love to see that Openrndr supports 32-bit color and a comprehensive Shape library.

But, I have to admit that I’m not loving Kotlin. Or maybe I just don’t love how the code-block-centric style of Program makes it hard to follow the control flow.

Are there any examples of using Openrndr from Java?

OpenRNDR APIs make heavy use of Kotlin’s “DSL” style, but you have flexibility here.
For example, when building a ShapeContour, you can use the “blocks style”:

val contour1 = contour { 
    moveTo(Vector2.ZERO)
    lineTo(100.0, 100.0)
}

or you can just call methods directly:

val builder = ContourBuilder(false)
builder.moveTo(Vector2.ZERO)
builder.lineTo(100.0, 100.0)
val contour2 = builder.result.first()

Hope this helps.

Is it maybe builder instead of cb?

ps. Welcome to the forum @mr_riptano :slight_smile:

1 Like

Here’s what I came up with as a fairly minimal setup and draw with 32bit color:

public class Test extends Program {
    RenderTarget rt;

    @Override
    public void setup() {
        super.setup();
        rt = RenderTargetKt.renderTarget(
                getWidth(),
                getHeight(),
                1.0,
                BufferMultisample.Disabled.INSTANCE,
                Session.Companion.getActive(),
                builder -> {
                    builder.colorBuffer(ColorFormat.RGBa, ColorType.FLOAT32);
                    return Unit.INSTANCE;
                }
        );
    }

    boolean isDrawn = false;

    @Override
    public void draw() {
        if (!isDrawn) {
            DrawerKt.isolatedWithTarget(drawer, rt, d -> {
                d.setFill(new ColorRGBa(0.0, 0.0, 1.0, 0.02, Linearity.UNKNOWN));
                drawer.setStroke(null);
                for (int i = 0; i < 100; i++) {
                    drawer.circle(70 + 5 * i, getHeight() / 2.0, 140.0);
                }
                return Unit.INSTANCE;
            });

            isDrawn = true;
        }
        drawer.image(rt.colorBuffer(0));
    }

    public static void main(String[] args) {
        var c = new Configuration();
        c.setWidth(500);
        c.setHeight(500);
        ApplicationKt.application(new Test(), c);
    }
}
1 Like

Thanks for sharing that. I find it useful to be able to compare how the Kotlin program would compare to the Java equivalent. I think the Kotlin version would look like this:

Imports...
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.ColorFormat
import org.openrndr.draw.ColorType
import org.openrndr.draw.isolatedWithTarget
import org.openrndr.draw.renderTarget
fun main() = application {
    configure {
        width = 500
        height = 500
    }
    program {
        val rt = renderTarget(width, height) {
            colorBuffer(ColorFormat.RGBa, ColorType.FLOAT32)
        }
        drawer.fill = ColorRGBa(0.0, 0.0, 1.0, 0.02)
        drawer.stroke = null
        drawer.isolatedWithTarget(rt) {
            for (i in 0 until 100) {
                drawer.circle(70.0 + 5 * i, height / 2.0, 140.0);
            }
        }
        extend {
            drawer.image(rt.colorBuffer(0))
        }
    }
}

Is the issue with control flow related only to extend { }?
(equivalent to void draw() { } in Processing)

Or also to other cases?

ps. I remember that it did feel weird for me at the beginning of using OPENRNDR the apparent “magic” of that { ... } syntax (coming from Processing and openFrameworks) but got used to it. It’s totally fine to use Kotlin without that feature, but OPENRNDR uses it in many places. When I convert Processing programs from Java to Kotlin the code is just much shorter, but the structure stays the same (so how easy or hard is to follow the flow doesn’t change I think).

@abe, fixed! thank you