Writing Processing in Kotlin

Hi :slight_smile:

Iā€™ve been now working over one year in Kotlin and Iā€™m liking it a lot. I use OPENRNDR mostly these days, but I recorded four videos about using Processing with Kotlin instead of Java:




Here more videos from these series and my patreon page where I post about generative design, plotting, OPENRNDR, Processing, and more.

Have a nice weekend! (Or weekday if itā€™s no longer weekend :crazy_face: )

10 Likes

Wow these videos looks great @hamoid , Loved your work really !

I migrated processing-android completely from [java[(GitHub - processing/processing-android: Processing mode and core library to create Android apps with Processing) to Kotlin/Native and have implemented processing APIs for JVM(android) few months back.

thanks !

4 Likes

Iā€™ve also been writing Processing sketches in Kotlin for a while and am loving it!

The only thing thatā€™s a bit annoying are the fs for Floats and the need to cast from Ints sometimesā€¦ and the warnings about private functions and properties.

To be able to type the private members faster, I setup pval, pvar and pfun live templates in IntelliJ that autocomplete the whole thing.


And to fix the Floats, I ended up creating a base sketch class with some helper methods and properties like

val widthF: Float
        get() = width.toFloat()

Anyway, a small price to pay for all the great Kotlin features.

Cool tutorials @hamoid, also a good tip to make the import and switching between p3 and p4 versions really easy.

3 Likes

Nice tips @anoniim ! Thank you for sharing. I didnā€™t know about those live templates :slight_smile:

About floatsā€¦ true, things would be simpler if Processing used Double everywhere. Defaulting to floats is what makes it messy to convert Processing Java code to Kotlin automatically. I should try converting the pre-processed java file that is found in some folder instead of the .pde file. Probably works much better.

It seems like in some cases it can be also faster to work with Double.

1 Like

Updated working link:

Same program written with OPENRNDR here.

ps. I dislike bumping this thread to the top, but I dislike even more a non-editable broken link that people click on :slight_smile:

1 Like

Hi hi. Recently I posted the same program written in Processing/Java and OPENRNDR/Kotlin:

which loads all images in a folder, gets a slice of each image, sorts the slices by saturation and brightness, and draws them stacked.

Here is now the same program in Processing/Kotlin. See how short it is.

It shows that, unlike Kotlin programs above in this page, thereā€™s no need for a companion object at all to launch the PApplet but just one line invoking PApplet.main().

import processing.core.PApplet
import java.io.File

fun main() = PApplet.main(CCSPoster88::class.java.name)

class CCSPoster88 : PApplet() {
    override fun settings() = size(1200, 675)

    override fun setup() {
        background(0)
        val files =
            File("/home/funpro/www/Stammtisch/assets/img/large").listFiles()
        val d = height / files.size.toFloat()
        val slices = files.map {
            val img = loadImage(it.absolutePath)
            img[0, img.height / 2, img.width, d.toInt()]
        }.sortedBy { slice ->
            slice.pixels.sumOf {
                max(saturation(it), 255 - brightness(it)).toDouble()
            } / slice.pixels.size
        }
        stroke(0, 20f)
        slices.forEachIndexed { i, slice ->
            image(slice, 0f, i * d, width.toFloat(), d)
            line(0f, i * d, width.toFloat(), i * d)
        }
    }

    override fun draw() {}
}

One thing that may be surprising / confusing is that since PImage has a get method, it can be accessed just with square brackets in Kotlin. That was not really intentional: when the PImage.get was created Kotlin didnā€™t exist. Since Kotlin removes the get() part from any getBlaBla() java method, there is no method name left in this case :slight_smile: Instead of img.get(x, y, w, h) itā€™s just img[x, y, w, h] to get a crop of the PImage.

Happy 20 year Processing anniversary! :fireworks: :cake:

5 Likes

@hamoid hi Sir

why you stop updating this site ??

https://funprogramming.org/

1 Like

@jafal Do you ever post anything but spam?

I thought itā€™s a good question and I planned to answer by writing a blog post. Meanwhileā€¦ I donā€™t find it very easy to answer, but if I would put in a scale on one side my time investment and expenses (server etc) and on the other what I get out of it, itā€™s not very balanced. I know my videos have helped and made many people happy as I received many positive comments. But unfortunately I donā€™t see them bringing me many new opportunities or pushing me beyond my limits. I do enjoy creating such content very much. If I only got paid for itā€¦ In recent years I focused more on giving GLSL workshops and financially at least it was better. And since asking is free: Iā€™m open for meaningful jobs and collaborations involving creative coding :slight_smile: .

ps. Art is meaningful and makes the world a better place.

ps2. And to stay on topic: writing programs in Kotlin makes me happy :slight_smile:

5 Likes

Thank you Sir for kindness and your sweet answer

i have watched your videos over and over you have great and creative way in explanation the most important thing of your style that You make your listener break the barrier of fear and simplify things for them

I think opportunities are looking for people like you and not the other way around, you are the opportunity

I was honored to communicate with you and thank you for your kindness, and I am completely confident that you will find what suits you because you deserve the best

all respect to your honorable person

2 Likes