# Writing Processing in Kotlin

**URL:** https://discourse.processing.org/t/writing-processing-in-kotlin/3957
**Category:** Processing
**Created:** [September 29, 2018, 4:42pm UTC](https://discourse.processing.org/t/writing-processing-in-kotlin/3957 "2018-09-29T16:42:12Z")
**Posts on this page:** 10
**Page:** 3

<div class="post-metadata">

### Author: ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)
#### Post date: [March 19, 2021, 9:46pm UTC](https://discourse.processing.org/t/writing-processing-in-kotlin/3957/41 "2021-03-19T21:46:02Z")

</div>

Hi 🙂

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:

https://player.vimeo.com/video/526158244  
https://player.vimeo.com/video/526159990  
https://player.vimeo.com/video/526162077  
https://player.vimeo.com/video/526164267

Here [more videos](https://vimeo.com/channels/p5idea) from these series and my [patreon page](https://www.patreon.com/funprogramming) where I post about generative design, plotting, OPENRNDR, Processing, and more.

Have a nice weekend! (Or weekday if it’s no longer weekend 🤪 )

---

<div class="post-metadata">

### Author: ![AdityaRana](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/adityarana/32/18501_2.png) [@AdityaRana](https://discourse.processing.org/u/AdityaRana)
#### Post date: [March 30, 2021, 7:45am UTC](https://discourse.processing.org/t/writing-processing-in-kotlin/3957/42 "2021-03-30T07:45:33Z")

</div>

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

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

thanks !

---

<div class="post-metadata">

### Author: ![anoniim](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/anoniim/32/12009_2.png) [@anoniim](https://discourse.processing.org/u/anoniim)
#### Post date: [April 7, 2021, 8:30pm UTC](https://discourse.processing.org/t/writing-processing-in-kotlin/3957/43 "2021-04-07T20:30:37Z")

</div>

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 `f`s 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.

 ![Screen Shot 2021-04-07 at 21.40.54](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/ea089dbfb572347fe9ea9a2d8e538472a0fdf892.png)  
 ![Screen Shot 2021-04-07 at 21.41.10](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/81ce7d5146ef696c2ce29eedf544311c5fbf5fd3.png)

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

```auto
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.

---

<div class="post-metadata">

### Author: ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)
#### Post date: [April 8, 2021, 7:04am UTC](https://discourse.processing.org/t/writing-processing-in-kotlin/3957/44 "2021-04-08T07:04:59Z")

</div>

Nice tips @anoniim ! Thank you for sharing. I didn’t know about those [live templates](https://www.youtube.com/watch?v=ffBeoE6NBSs) 🙂

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](https://stackoverflow.com/questions/21445662/in-what-situations-is-it-better-to-use-a-float-over-a-double-in-java).

---

<div class="post-metadata">

### Author: ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)
#### Post date: [May 10, 2021, 7:42am UTC](https://discourse.processing.org/t/writing-processing-in-kotlin/3957/45 "2021-05-10T07:42:22Z")

</div>

Updated working link:

[Same program written with OPENRNDR here](https://github.com/hamoid/openrndr-template/blob/master/src/main/kotlin/apps/p5/p09_circlePacking.kt).

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

---

<div class="post-metadata">

### Author: ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)
#### Post date: [August 10, 2021, 3:11pm UTC](https://discourse.processing.org/t/writing-processing-in-kotlin/3957/46 "2021-08-10T15:11:59Z")

</div>

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

> <https://twitter.com/hamoid/status/1423527174435442691>

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()`.

```auto
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 🙂 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! 🎆 🍰

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [August 10, 2021, 3:20pm UTC](https://discourse.processing.org/t/writing-processing-in-kotlin/3957/47 "2021-08-10T15:20:16Z")

</div>

@hamoid hi Sir

why you stop updating this site ??

[https://funprogramming.org/](https://funprogramming.org/)

---

<div class="post-metadata">

### Author: ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)
#### Post date: [August 12, 2021, 6:15pm UTC](https://discourse.processing.org/t/writing-processing-in-kotlin/3957/48 "2021-08-12T18:15:25Z")

</div>

@jafal Do you ever post anything but spam?

---

<div class="post-metadata">

### Author: ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)
#### Post date: [August 12, 2021, 7:51pm UTC](https://discourse.processing.org/t/writing-processing-in-kotlin/3957/50 "2021-08-12T19:51:26Z")

</div>

> [@jafal](#):
>
> why you stop updating this site ??

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 🙂 .

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

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

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [August 12, 2021, 8:32pm UTC](https://discourse.processing.org/t/writing-processing-in-kotlin/3957/51 "2021-08-12T20:32:00Z")

</div>

> [@hamoid](#):
>
> I thought it’s a good question and I planned to answer by writing a blog post

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

[Previous page](https://discourse.processing.org/t/writing-processing-in-kotlin/3957.md?page=2)
